[英]PHP Site and NodeJS Websocket Application behind the same Nginx server/config
I have a development server that is running Nginx and serving a PHP website. 我有一个运行Nginx并服务于PHP网站的开发服务器。 Below is the working nginx config. 以下是有效的nginx配置。
cat /etc/nginx/nginx.conf
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
# multi_accept on;
}
http {
client_max_body_size 100M;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
gzip on;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
This works perfectly to serve the PHP site; 这可以完美地服务于PHP网站。 however, I would also like Nginx to run a Node based websockets server. 但是,我也希望Nginx运行基于Node的websockets服务器。
What changes do I need to make to this Nginx config to receive websockets traffic as well? 我还需要对此Nginx配置进行哪些更改以同时接收WebSocket流量?
I created a new file at: 我在以下位置创建了一个新文件:
vi /etc/nginx/sites-enabled/somesite.com
and put this in it: 并放入其中:
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
upstream websocket {
server localhost:3000;
}
server {
listen 8080;
server_name somesite.com;
access_log /var/log/nginx/websocket.access.log;
location / {
proxy_pass http://websocket;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
This seems to work. 这似乎有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.