配置NGINX以使用SSL包装非SSL服务

时间:2019-03-14 15:50:55

标签: sockets ssl nginx

我们有一个Web服务器和与该Web服务器并排运行的许多节点服务。这些服务全部使用套接字,Web服务器还具有一个套接字,浏览器然后使用NGINX的反向代理功能与所有套接字进行通信。

现在,我们要设置NGINX,以便它可以处理传入的SSL(端口443)请求,但是Web服务器和套接字保留在端口80(http / ws)上,基本上可以安全地完成配置。

我们已经安装了证书(目前是自签名的),我可以使它的网络服务器正常工作,但是所有套接字通信都会出错。

2019/03/14 10:27:31 [error] 14279#14279: *2 connect() failed (111: Connection refused) while connecting to upstream, client: ::1, server: _, request: "GET /web_app_socket/?EIO=3&transport=polling&t=Mbz1xMB HTTP/2.0", upstream: "http://127.0.0.1:3001/web/socket.io/?EIO=3&transport=polling&t=Mbz1xMB", host: "localhost", referrer: "https://localhost/"

在客户端,我得到了:

GET https://localhost/liveview/?EIO=3&transport=polling&t=Mbz1vtE 502

这是我在用于Web服务器套接字的NGINX的default配置文件中拥有的内容:

    location /web_app_socket/ { ### route the websockets of the web app
        #Configure proxy to pass data to upstream service
        proxy_pass http://web_app/web/socket.io/;
        #HTTP version 1.1 is needed for sockets
        proxy_http_version 1.1;
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header Referer $http_referer;
        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_set_header X-NginX-Proxy true;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        proxy_read_timeout 600s;
        proxy_connect_timeout 600s;
    }

我尝试了许多配置,以至于我迷失了应该做的正确方法(或者,NGINX是否可以这样做?)。

1 个答案:

答案 0 :(得分:0)

是,已解决此问题。我刚刚删除了proxy_redirect off;,就可以了。

现在,它看起来像这样:

    location /web_app_socket/ { ### route the websockets of the web app
        #Configure proxy to pass data to upstream service
        proxy_pass http://web_app/web/socket.io/;
        #HTTP version 1.1 is needed for sockets
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header Referer $http_referer;
        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_set_header X-NginX-Proxy true;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        proxy_read_timeout 600s;
        proxy_connect_timeout 600s;
    }