简体   繁体   English

将Nginx用作反向代理时,我是否需要为expressjs中的纯websocket应用程序进行路由?

[英]When using Nginx as a reversed proxy, do I need to do routing for pure websocket application in expressjs?

Hard to write a proper title. 很难写一个适当的标题。

I have a pure websocket application which is based on an express server. 我有一个基于Express服务器的纯websocket应用程序。 In development mode the express server does some simple routing to access the html, js and png files. 在开发模式下,快速服务器执行一些简单的路由来访问html,js和png文件。 Basically it is a one page app that only handle websocket protocol. 基本上,这是一个仅处理websocket协议的一页应用程序。 But in production mode, I delegate all that to Nginx so I don't do any routing in express production. 但是在生产模式下,我将所有工作委托给Nginx,因此在快速生产中不做任何路由。 I was expecting Nginx will find all these files on its "root" directory. 我期望Nginx在其“根”目录中找到所有这些文件。 I get a "Cannot Get /" error however. 我收到“无法获取/”错误。

TL;DR version: TL; DR版本:

When I use Nginx to serve static contents for a one page app, do I need to do anything in express routing? 当我使用Nginx为一页应用程序提供静态内容时,我是否需要在快速路由中做任何事情? If so, how? 如果是这样,怎么办?

UPD (my Nginx settings, can find another more detail question here ): UPD (我的Nginx设置,可以在此处找到另一个更详细的问题):

upstream upstream_project {
    server 127.0.0.1:8888;
    keepalive 64;
}

server {
        listen 0.0.0.0:80 default_server;
        listen [::]:80 default_server ipv6only=on;

        root /usr/local/share/html;
        index index.html index.htm;

        # Make site accessible from http://localhost/
        server_name ws_server;

        location / {
                try_files $uri $uri/ index.html;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $http_host;
                proxy_set_header X-NginX-Proxy true;

                proxy_redirect off;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
                proxy_pass http://upstream_project;
                proxy_read_timeout 240s;
        }
}

UPDATED : 更新时间

I have found in Nginx log that the client is trying to access the server via ipv6. 我在Nginx日志中发现客户端正在尝试通过ipv6访问服务器。 See this: 看到这个:

    kevent() reported that connect() failed (61: Connection refused) while
 connecting to upstream, client: ::1, server: ws_server, request: "GET / 
HTTP/1.1", upstream: "http://127.0.0.1:8888/", host: "localhost"

I remove the ipv6 server listen line ( listen [::]:80 default_server ipv6only=on; ) and have to change the try_files line to use file name explicitly. 我删除了ipv6服务器侦听行( listen [::]:80 default_server ipv6only=on; ),并且不得不更改try_files行以显式使用文件名。

try_files $uri $uri/app.js ;

Then I can get my app working. 然后,我可以使我的应用程序正常工作。 But I don't understand. 但是我不明白。 Why do I have to do this? 为什么我必须这样做?

I still can't access the static png file from the subdirectory of "root" folder. 我仍然无法从“根”文件夹的子目录访问静态png文件。 Any help would be appreciated. 任何帮助,将不胜感激。

NGINX supports WebSocket by allowing a tunnel to be set up between a client and a backend server. NGINX通过允许在客户端和后端服务器之间建立隧道来支持WebSocket。 For NGINX to send the Upgrade request from the client to the backend server, the Upgrade and Connection headers must be set explicitly, as in this example: 为了使NGINX将升级请求从客户端发送到后端服务器,必须显式设置Upgrade和Connection标头,如以下示例所示:

location /wsapp/ {
    proxy_pass http://wsbackend;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
}

Take a look on this NGINX guide for more details and examples. 请查看此NGINX指南,以获取更多详细信息和示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM