如何使用Nginx设置安全的WebSocket

时间:2019-04-16 00:07:05

标签: nginx https websocket

我有一个在端口9000上运行的Web服务器,我想使其在端口80上可用,而且我想在端口9021上提供一个Websocket连接。如果我在{'cse_image':...}上运行此服务器,则一切正常。但是当我转到http时,无法连接网络套接字。

这是我的Nginx配置:这发出警告:

https

nginx: [warn] conflicting server name "oyun.net" on 0.0.0.0:443, ignored

这是浏览器错误:

server {
     listen 443 ssl;
     server_name          oyun.net;
     ssl_certificate      /etc/key.pem
     ssl_certificate_key  /etc/key2.pem
     listen 80;
     location / {
         proxy_pass http://localhost:9000
     }
}

server {
     listen 443 ssl;
     server_name          oyun.net;
     ssl_certificate      /etc/key.pem
     ssl_certificate_key  /etc/key2.pem
     listen 9021;
     location / {
        proxy_pass http://localhost:9000;
        proxy_http_version 1.1;
        proxy_set_header upgrade $http_upgrade;
        proxy_set_header connection "upgrade";
        proxy_set_header x-real-ip $remote_addr;
        proxy_set_header host $host;
        proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;
     }
 }

1 个答案:

答案 0 :(得分:0)

我已经为socket.oyun.net创建了新的认证,并且该配置有所帮助

server {
        listen 80;
        server_name oyun.net;
        return 301 https://oyun.net$request_uri;
}

server {
        listen 443 ssl;
        server_name oyun.net;
        ssl_certificate         /etc/letsencrypt/live/oyun.net/fullchain.pem;
        ssl_certificate_key     /etc/letsencrypt/live/oyun.net/privkey.pem;

        location / {
                proxy_pass http://localhost:9000;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        }

}
server {
        listen 9021 ssl;
        server_name socket.oyun.net;
        ssl_certificate         /etc/letsencrypt/live/socket.oyun.net/fullchain.pem;
        ssl_certificate_key     /etc/letsencrypt/live/socket.oyun.net/privkey.pem;


        location / {

                proxy_pass http://localhost:9000;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
                proxy_set_header X-Real-Ip $remote_addr;
                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;


        }

}