通过Nginx反向代理使用Node.js保护Websockets的安全-错误301

时间:2020-06-10 04:47:37

标签: javascript node.js ssl nginx websocket

嗨,我之前一直在尝试使用Apache进行此操作,但没有成功。我决定改用Nginx。

我正在尝试建立以下内容,

客户端<-wss-> Nginx <-ws-> Node.js

似乎很简单,但是我没有成功。我不断收到错误301。

我的客户端方面很简单

const connection = new WebSocket('wss://' + location.host + '/ws');

服务器端是

const ws = new WebSocket.Server({port: 8080});

Nginx配置文件是

server {
    server_name example.com;
    listen 443 ssl; # managed by Certbot                                                                                                                                                                                                         
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot                                                                                                                                                                       
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot                                                                                                                                             
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot                                                                                                                                                                        
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot                                                                                                                                                                  

    location / {                                                                                                                                                                                                                                         
        proxy_pass http://localhost:3000;                                                                                                                                                                                                    
    }

    location /ws {                                                                                                                                                                                                                                       
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;                                                                                                                   
        proxy_set_header Host $host;                                                                                                                                                                                                                                                                                                                                                                                                                                                              
        proxy_http_version 1.1; # Needed                                                                                                                                                                                                             
        proxy_set_header Upgrade $http_upgrade; # Needed                                                                                                                                                                                             
        proxy_set_header Connection "upgrade"; # Needed                                                                                                                                                                                                                                                                                                                                                                                                                                           
        proxy_pass http://localhost:3000;
    }
}
server {
     if ($host = example.com) {                                                                                                                                                                                                              
          return 301 https://$host$request_uri;
     } # managed by Certbot

     listen 80;

     server_name example.com;
     return 404; # managed by Certbot
}                                                                                                                                                                                                                  

我已经看到了许多有关websockets配置设置的文章,而我所没有的绝对可以。但是,无论我多么努力,它都不起作用。

1 个答案:

答案 0 :(得分:0)

我知道了,

问题是我将Websocket设置为端口8080,但是我的proxy_pass被设置为端口3000。

解决方案是使它们都在同一端口上。

对于应用服务器

const ws = new WebSocket.Server({port: 3001});

并使Nginx在/ ws下具有相同的端口,

proxy_pass http://localhost:3001;