我需要在 rest 服务上使用 nginx 将 http 重定向到 https 连接,并且需要在 postman 或 soapui 上对其进行测试

我需要在 rest 服务上使用 nginx 将 http 重定向到 https 连接,并且需要在 postman 或 soapui 上对其进行测试

这是我的 nginx.conf 文件:我应该做哪些更改才能使其正常工作以及如何获取证书

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
     server {
    listen       80;

    server_name hostname_of_virtual_machine http://ipaddress:port;


    return 301 https://$ipaddress:port$request_uri;
}


# Settings for a TLS enabled server.

    server {
        listen       443 ssl http2 default_server;
        listen       [::]:443 ssl http2 default_server;
        server_name  hostname_of_virtual_machine;
        root         /usr/share/nginx/html;

        #ssl_certificate "/etc/pki/nginx/server.crt";
        #ssl_certificate_key "/etc/pki/nginx/private/server.key";
        #ssl_session_cache shared:SSL:1m;
        #ssl_session_timeout  10m;
        #ssl_ciphers PROFILE=SYSTEM;
        #ssl_prefer_server_ciphers on;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

}

答案1

  1. 首先你需要删除配置代码include /etc/nginx/conf.d/*.conf;

注意:以下示例使用 Ubuntu 20.04 LTS

  1. 转到/etc/nginx/sites-available并创建一个新文件myapp01并将您的配置放在那里。

cd /etc/nginx/sites-available

sudo vi myapp01

请参阅以下片段:

upstream appname-server {
    server 127.0.0.1:8080;
}

server {
    root /var/www/html;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;

    server_name appname.com;

    access_log /var/log/nginx/appname-access.log;
    error_log /var/log/nginx/appname-error.log;

    location / {
        proxy_pass http://appname-server;
        proxy_http_version 1.1;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_read_timeout 5m;
        proxy_send_timeout 5m;
}


    listen 443 ssl;
    ssl_certificate /path/to/your/ssl/cert.pem;
    ssl_certificate_key /path/to/your/ssl/cert_key.pem;
}


server {
    if ($host = appname.com) {
        return 301 https://$host$request_uri;
    }


    server_name appname.com;
    listen 80;
    return 404;
}
  1. 不要忘记添加include /etc/nginx/sites-enabled/*;nginx.conf(感谢流浪者104以作通知)

     http {
    
         ##
         # Basic Settings
         ##
    
         sendfile on;
         tcp_nopush on;
         tcp_nodelay on;
         keepalive_timeout 65;
         types_hash_max_size 2048;
    
         include /etc/nginx/mime.types;
         default_type application/octet-stream;
    
         ##
         # SSL Settings
         ##
    
         ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
         ssl_prefer_server_ciphers on;
    
         ##
         # Logging Settings
         ##
    
         access_log /var/log/nginx/access.log;
         error_log /var/log/nginx/error.log;
    
         ##
         # Gzip Settings
         ##
    
         gzip on;
    
         ##
         # Virtual Host Configs
         ##
    
         include /etc/nginx/conf.d/*.conf;
         include /etc/nginx/sites-enabled/*;
     }
    
  2. 接下来,转到/etc/nginx/sites-enabled并创建符号链接myapp01,参考下面的说明。

转至已启用站点 cd /etc/nginx/sites-enabled/

为 myapp01 创建符号链接 ln -s /etc/nginx/sites-available/myapp01 .

  1. 之后,使用 测试您的 nginx 配置sudo nginx -t。如果一切成功,请继续执行步骤 5。

  2. 重新加载 nginxsudo systemctl reload nginx

希望它能帮助你,加油。

相关内容