简体   繁体   English

Nginx反向代理WebSocket

[英]Nginx reverse proxying websockets

Is there a way to compile nginx to handle reverse proxying websockets requests? 有没有一种方法可以编译nginx来处理反向代理websockets请求? I'm building a realtime app in Node.JS and need a web server on top of this. 我正在Node.JS中构建一个实时应用程序,并且在此之上需要Web服务器。 Everything I've read says Nginx cannot reverse proxy websockets requests, so I'm a little confused on how I should approach this problem. 我读过的所有内容都说Nginx无法撤消代理websockets请求,因此我对应该如何解决此问题有些困惑。

I was thinking about just implementing all server logic in Node, but there are some problems with this approach 我本来只是想在Node中实现所有服务器逻辑,但是这种方法存在一些问题

1) PHP - I need a way to serve PHP files 2) Static files - I really like that nginx is very fast for static files. 1)PHP-我需要一种提供PHP文件的方法2)静态文件-我真的很喜欢nginx对于静态文件非常快。 There are some modules for this, though, so this problem isn't too big. 但是,有一些模块可以解决这个问题,所以这个问题不会太大。 3) When I need to update my Node app, I would like to be able to restart that part separately from the main server. 3)当我需要更新Node应用程序时,我希望能够与主服务器分开重新启动该部分。 Also, if the Node app crashes for some reason, I don't want the whole web server to go down! 另外,如果Node应用由于某种原因崩溃,我不希望整个Web服务器崩溃!

The simplest solution would be to setup virtual hosts in nginx for multiple subdomains and run each service on a separate one. 最简单的解决方案是在nginx中为多个子域设置虚拟主机,然后在单独的服务上运行每种服务。 That way you don't have to worry about distinguishing websockets requests from standard http requests on the same port. 这样,您不必担心将websockets请求与同一端口上的标准http请求区分开。

Also, you can run php behind nginx using PHP-FPM but that's quite a challenge to get working, and for that reason Apache may be a better choice. 另外,您可以使用PHP-FPM在nginx后面运行php,但这是一个很大的挑战,因此,Apache可能是一个更好的选择。

I am doing something similar, here is the nginx server config( file: MY_DOMAIN.tk.conf on: /etc/nginx/config.d/) that work for me, with: 我正在做类似的事情,这是对我有用的Nginx服务器配置(文件:/etc/nginx/config.d/上的MY_DOMAIN.tk.conf),具有:

  • let's encrypt tls cert for ssl 让我们为SSL加密TLS证书
  • php7.2 reverse proxy php7.2反向代理
  • nodejs reverse proxy for websockets server running on port 8000. 在端口8000上运行的websockets服务器的nod​​ejs反向代理。

     map $http_upgrade $connection_upgrade { default upgrade; '' close; } upstream appserver { server localhost:8000; # appserver_ip:ws_port } server { server_name MY_DOMAIN.tk; root /var/www/html; index index.php; location /ws { proxy_pass http://appserver; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; } location / { try_files $uri $uri/ /index.php?$args; } location ~ \\.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php7.2-fpm.sock; } client_max_body_size 128m; # add_header Strict-Transport-Security "max-age=15768000" always; listen 443 ssl; # managed by Certbot ssl_certificate /etc/letsencrypt/live/MY_DOMAIN.tk/fullchain.pem; # managed by Certbot ssl_certificate_key /etc/letsencrypt/live/MY_DOMAIN.tk/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 } server { if ($host = MY_DOMAIN.tk) { return 301 https://$host$request_uri; } # managed by Certbot server_name MY_DOMAIN.tk; listen 80; return 404; # managed by Certbot } 

by doing this then you can conect to wss://MY_DOMAIN.tk/ws 通过这样做,您可以连接到wss://MY_DOMAIN.tk/ws

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

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