我是如此的迷失和新的建设 NGINX 自己,但我希望能够启用安全的网络套接字没有一个额外的层。
我不想在 websocket 服务器上启用 SSL,但是我想使用 NGINX 为整个系统添加一个 SSL 层。
每个网页都说我做不到,但我知道我可以!感谢任何人(我自己)可以告诉我如何!
不要害怕,因为一群勇敢的操作系统程序员已经用崭新的 译自: 美国《科学》杂志网站(http://github.com/yaoweibin/nginx _ tcp _ xy _ module)原文地址: http://github.com/yaoweibin/nginx _ tcp _ xy _ module 原文地址: http://github.com/yaoweibin/yaoweibin解决了这个问题
写于2012年8月,所以如果你来自未来,你应该做你的家庭作业。
假设您正在使用 CentOS:
init.d/nginx
yum install pcre pcre-devel openssl openssl-devel
同样,CentOS 假设:
cd /usr/local/
wget 'http://nginx.org/download/nginx-1.2.1.tar.gz'
tar -xzvf nginx-1.2.1.tar.gz
cd nginx-1.2.1/
patch -p1 < /path/to/nginx_tcp_proxy_module/tcp.patch
./configure --add-module=/path/to/nginx_tcp_proxy_module --with-http_ssl_module
make
make install
可选项:
sudo /sbin/chkconfig nginx on
如果要重用旧的配置文件,请记住首先复制它们。
重要提示: 您需要在 conf 的最高级别创建一个 tcp {}指令。确保它不在你的 http {}指令中。
tcp {}
http {}
下面的示例配置显示了一个上游 websocket 服务器,以及 SSL 和 Non-SSL 的两个代理。
tcp { upstream websockets { ## webbit websocket server in background server 127.0.0.1:5501; ## server 127.0.0.1:5502; ## add another server if you like! check interval=3000 rise=2 fall=5 timeout=1000; } server { server_name _; listen 7070; timeout 43200000; websocket_connect_timeout 43200000; proxy_connect_timeout 43200000; so_keepalive on; tcp_nodelay on; websocket_pass websockets; websocket_buffer 1k; } server { server_name _; listen 7080; ssl on; ssl_certificate /path/to/cert.pem; ssl_certificate_key /path/to/key.key; timeout 43200000; websocket_connect_timeout 43200000; proxy_connect_timeout 43200000; so_keepalive on; tcp_nodelay on; websocket_pass websockets; websocket_buffer 1k; } }
只需要注意到 nginx 现在在1.3.13版本中支持 Websockets:
location /websocket/ { proxy_pass http://backend_host; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_read_timeout 86400; }
您还可以检查 Nginx 变更日志和 WebSocket 代理文档。
Pankaj Malhotra 撰写的一篇好的、简明的文章讨论了如何使用 NGINX 实现这一点,并提供了 给你。
NGINX 的基本配置转载如下:
map $http_upgrade $connection_upgrade { default upgrade; '' close; } upstream appserver { server 192.168.100.10:9222; # appserver_ip:ws_port } server { listen 8888; // client_wss_port ssl on; ssl_certificate /path/to/crt; ssl_certificate_key /path/to/key; location / { proxy_pass http://appserver; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; } }
这对我很有效:
location / { # redirect all HTTP traffic to localhost:8080 proxy_pass http://localhost:8080; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # WebSocket support proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; }
借自: https://github.com/nicokaiser/nginx-websocket-proxy/blob/df67cd92f71bfcb513b343beaa89cb33ab09fb05/simple-wss.conf
对我来说,这取决于 proxy_pass的位置设置。我需要转而使用 HTTPS 协议,并在节点服务器端设置有效的 SSL 证书。这样,当引入外部节点服务器时,只需更改 IP,其他所有内容都保持相同的配置。
proxy_pass
我希望这对某些人有所帮助... ... 我一直盯着这个问题... ... 唉... ..。
map $http_upgrade $connection_upgrade { default upgrade; '' close; } upstream nodeserver { server 127.0.0.1:8080; } server { listen 443 default_server ssl http2; listen [::]:443 default_server ssl http2 ipv6only=on; server_name mysite.com; ssl_certificate ssl/site.crt; ssl_certificate_key ssl/site.key; location /websocket { #replace /websocket with the path required by your application proxy_pass https://nodeserver; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; proxy_http_version 1.1; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_intercept_errors on; proxy_redirect off; proxy_cache_bypass $http_upgrade; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-NginX-Proxy true; proxy_ssl_session_reuse off; } }
用于.net core 2.0 Nginx 和 SSL
location / { # redirect all HTTP traffic to localhost:8080 proxy_pass http://localhost:8080; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # WebSocket support proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $http_connection; }
这招对我很管用
使用 nginx/1.14.0
我有一个 websocket-server 在端口8097上运行,用户从端口8098上连接到 wss,nginx 只是解密内容并将其转发到 websocket 服务器
所以我有这个配置文件(在我的例子中是 /etc/nginx/conf.d/default.conf)
/etc/nginx/conf.d/default.conf
server { listen 8098; ssl on; ssl_certificate /etc/ssl/certs/domain.crt; ssl_certificate_key /root/domain.key; location / { proxy_pass http://hostname:8097; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_read_timeout 86400; } }
如果要在测试环境中添加 SSL,那么可以使用 mkcert。下面我提到了 GitHub 的 URL。 Https://github.com/filosottile/mkcert 下面我还提到了反向代理的示例 nginx 配置。
mkcert
server { listen 80; server_name test.local; return 301 https://test.local$request_uri; } server { listen 443 ssl; server_name test.local; ssl_certificate /etc/nginx/ssl/test.local.pem; ssl_certificate_key /etc/nginx/ssl/test.local-key.pem; location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header X-Client-Verify SUCCESS; proxy_set_header Host $http_host; proxy_set_header X-NginX-Proxy true; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_pass http://localhost:3000; proxy_redirect off; proxy_buffering off; } }