我需要的是
Client <--wss---> nginx <--wss--> server
我正在做一個 wss 代理,我已經讓它在 Apache 上工作了,但我想切換到 nginx,現在這是 apache 配置:
<LocationMatch "/statistics">
ProxyPass wss://127.0.0.1:23456
ProxyAddHeaders Off
ProxyPreserveHost On
RequestHeader set Host %{HTTP_HOST}s
RequestHeader set X-Forwarded-For %{REMOTE_ADDR}s
</LocationMatch>
在 nginx 上我使用這個配置/etc/nginx/nginx.conf
server {
listen 444 ssl;
listen [::]:444 ssl;
server_name domain.com www.domain.com;
root /usr/share/nginx/html;
ssl_certificate "/etc/letsencrypt/live/domain/fullchain.pem";
ssl_certificate_key "/etc/letsencrypt/live/domain/privkey.pem";
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 10m;
ssl_ciphers PROFILE=SYSTEM;
ssl_prefer_server_ciphers on;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
proxy_ssl_server_name on;
ssl_protocols TLSv1.2 TLSv1.3;
location /statistics {
proxy_pass https://127.0.0.1:23456;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_ssl_name $host;
proxy_ssl_session_reuse off;
proxy_ssl_certificate "/etc/letsencrypt/live/domain/fullchain.pem";
proxy_ssl_certificate_key "/etc/letsencrypt/live/domain/privkey.pem";
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
我的網站在 Nginx 上加載良好,沒有任何 SSL 錯誤,不起作用的是帶有 wss 的 websocket,帶有 ws 的 websocket 也可以工作!
我用於wscat
測試連線運行:
wscat -c wss://kaveneger.ir:23456
Connected (press CTRL+C to quit)
>
意味著我的端點很好(實際上並不需要這個測試,因為它在 apache 中工作,但無論如何...)
現在我用 nginx 來做這件事
wscat -c wss://domain.com:444/statistics
error: Unexpected server response: 404
> %
Nginx 日誌
/var/log/nginx/access.log
123.123.123.123 - - [27/Feb/2023:21:53:15 +0000] "GET /statistics HTTP/1.1" 404 0 "-" "-" "-"
/var/log/nginx/error.log
2023/02/27 21:53:49 [crit] 70632#70632: *14 SSL_read() failed (SSL: error:0A000126:SSL routines::unexpected eof while reading) while keepalive, client: 123.123.123.123, server: 0.0.0.0:444
我的作業系統是 Rocky Linux 9,nginx 版本:nginx/1.20.1
我已經搜索了很多並嘗試了我找到的所有解決方案,但沒有任何效果!
更新
如果我使用location /
而不是它就可以了location /statistics
!