nginx https代理http问题

目前有http://IP:8080的站点、https://IP:9443的站点
想通过nginx一个端口(比如1016端口)同时代理,实现:
https://ip:1016/a ->http://IP:8080的站点
https://ip:1016/b ->https://IP:9443的站点

nginx配置文件如下:

server {
                listen                                  1016 ssl;
                server_name                             192.168.3.22; #用户服务器的
                ssl_certificate                         /etc/nginx/server.crt;
                ssl_certificate_key                     /etc/nginx/server.key;
                ssl_session_timeout                     5m;
                ssl_protocols                           TLSv1 TLSv1.1 TLSv1.2;
                ssl_ciphers                             ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
                ssl_prefer_server_ciphers               on;
                location  /a/ {
                                proxy_pass     http://192.168.3.22:8080/;
                                proxy_redirect     off;
                proxy_set_header   Host             $host;
                proxy_set_header   X-Real-IP        $remote_addr;
                proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
                        }
                location  /docker/ {
                                proxy_pass     https://192.168.3.22:9443/;
                        }

                }


但是https://ip:1016/a%E8%AE%BF%E9%97%AE%E6%97%A0%E6%95%88%EF%BC%8C%E5%BA%94%E8%AF%A5%E5%92%8B%E9%85%8D%E7%BD%AE

img

img

  • 给你找了一篇非常好的博客,你可以看看是否有帮助,链接:nginx http强制跳转https 配置相关信息
  • 除此之外, 这篇博客: nginx部署http和https配置,https转发http中的 https端口 转发给内部http端口 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • 注意:云服务器安全组开放18080 和 8082端口权限

        #HTTPS server
        server {
            listen 18080 ssl;
            server_name *.yourdomain.com; #需要将yourdomain.com替换成证书绑定的域名。
    
            ssl_certificate cert/xxx.pem;  #需要将cert-file-name.pem替换成已上传的证书文件的名称。
            ssl_certificate_key cert/xxx.key; #需要将cert-file-name.key替换成已上传的证书密钥文件的名称。
            ssl_session_timeout 5m;
            ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
            #表示使用的加密套件的类型
            ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #表示使用的TLS协议的类型
            ssl_prefer_server_ciphers on;
            location / {
                # 转发到 http 服务器中
                proxy_pass http://localhost:8082;
                #proxy_redirect http:// https://;
                #add_header         Cache-Control    no-store;
                #proxy_set_header   Host             $host;
                #proxy_set_header   X-Real-IP        $remote_addr;
                #proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
            }
        }
    

    注意:访问时一定要用域名+端口访问,因为证书是绑定了域名的,如果不用域名而是直接用ip当我这个端口,会报安全异常警告的,说白了就是没用上证书,如下:
    在这里插入图片描述

    上面这个配置重启nginx后,浏览器访问 https://www.yourdomain.com:18080/test 会转发给域名地址内部的http 8080端口
    在这里插入图片描述