php - 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. Below is the working nginx config.
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; however, I would also like Nginx to run a Node based websockets server.
What changes do I need to make to this Nginx config to receive websockets traffic as well?
解决方案
我在以下位置创建了一个新文件:
vi /etc/nginx/sites-enabled/somesite.com
并将其放入其中:
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";
}
}
这似乎有效。
推荐阅读
- android - 如何解决“被 Play Protect 屏蔽”的问题
- sql-server - 我无法恢复我的数据库 SQL Server Management Studio 无法识别我的 .bak 文件
- java - 在tomcat docker上部署java REST服务
- c# - 单击 MVVM 中的父网格时从文本框中删除焦点
- angular - AgGrid Cell Renderer init() 函数在 onCellValueChanged() 上调用了两次,应该只调用一次
- android - Facebook 深度链接在 Android 上的错误行为
- java - 无法让android创建通知
- google-cloud-data-fusion - 创建系统计算配置文件
- rest - 下游验证失败时的 Http 状态码
- angular - 如何让不同的 MAT-TAB 共享相同的 HTML 代码