nginx的同一个端口配置支持http与https协议

1.需求

http://www.baidu.com:5000
https://www.baidu.com:5000
请求自定义端口的http 跟https,都一样的页面

2.nginx配置

vim /opt/lucky/nginx/conf/vhosts/baidu.conf

upstream gatewayservice {
          server  127.0.0.1:9000  weight=1;    #反代tomcat服务
     }

server {

        listen       5000 ssl;  #自定义的监听端口
        server_name  www.baidu.com;  #改成你的域名  
        ssl_certificate  /opt/lucky/nginx/conf/keys/tgchat111/server.crt; 
        ssl_certificate_key /opt/lucky/nginx/conf/keys/tgchat111/server.key;
        ssl_protocols       TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
        ssl_ciphers         HIGH:!aNULL:!MD5;
        set $redirectMode gatewayservice;

        location / {
               proxy_set_header Host $host:$server_port;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;


                if ($scheme = 'https') {
                  proxy_pass http://gatewayservice;     
                  break;
                }


                try_files $uri $uri/ @http_to_https;  
                index  index.html index.htm;
        }

       location @http_to_https {
          proxy_set_header Host $host:$server_port;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header X-Forwarded-Proto $scheme;
          proxy_pass http://gatewayservice;
       }


        error_page 497 = @http_to_https;


        error_page 405 =200 $uri;
        error_page 404 /404.html;
            location = /40x.html {
        }
        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
}

你可能感兴趣的:(http,nginx,https)