【问题标题】:Secured Websockets with Nodejs over Nginx Reverse Proxy - Error 301通过 Nginx 反向代理使用 Nodejs 保护 Websockets - 错误 301
【发布时间】:2020-09-29 10:12:04
【问题描述】:

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

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

client Nginx Nodejs

似乎是一件简单的事情,但我没有取得任何成功。我不断收到错误 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 配置设置的帖子,而我没有的帖子绝对应该可以工作。但是,无论我怎么努力,它都不起作用。

【问题讨论】:

    标签: javascript node.js ssl nginx websocket


    【解决方案1】:

    我想通了,

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

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

    对于应用服务器

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

    并让 Nginx 在 /ws 下拥有相同的端口,

    proxy_pass http://localhost:3001;
    

    【讨论】:

      猜你喜欢
      • 2011-08-09
      • 2013-02-18
      • 2013-05-25
      • 1970-01-01
      • 1970-01-01
      • 2019-11-26
      • 1970-01-01
      • 2014-05-18
      • 2019-05-04
      相关资源
      最近更新 更多