@ServerEndpoint("/websocket.ws/{userId}")
me.add(new UrlSkipHandler("^/websocket.ws", false));
Nginx 线上配置:
server { listen 80; listen [::]:80; listen 443 ssl http2; listen [::]:443 ssl http2; ssl_certificate /usr/local/nginx/conf/ssl/xxx.crt; ssl_certificate_key /usr/local/nginx/conf/ssl/xxx.key; ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; ssl_ciphers TLS......; ssl_prefer_server_ciphers on; ssl_session_timeout 10m; ssl_session_cache builtin:1000 shared:SSL:10m; ssl_buffer_size 1400; add_header Strict-Transport-Security max-age=15768000; ssl_stapling on; ssl_stapling_verify on; server_name xxx.com www.xxx.com; access_log /xxx.com_nginx.log combined; index index.html index.htm index.jsp; root /xxx.com/webapp; if ($host != www.xxx.com) { return 301 $scheme://www.xxx.com$request_uri; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ { expires 30d; access_log off; } location ~ .*\.(js|css)?$ { expires 7d; access_log off; } location ~ /(\.user\.ini|\.ht|\.git|\.svn|\.project|LICENSE|README\.md) { deny all; } location /websocket.ws { proxy_pass http://127.0.0.1:8099/websocket.ws; proxy_connect_timeout 4s; proxy_read_timeout 300s; proxy_send_timeout 10s; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; } location ~ { proxy_pass http://127.0.0.1:8099; include proxy.conf; } }
线上连接一直报404,本地开发环境是正常的
=======================================================
解决了,将 ~ 改 ^~,^~ 开头表示uri以某个常规字符串开头。
location ^~ /websocket.ws/ { proxy_pass http://127.0.0.1:8099; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; }
如下正确配置:
server { listen 80; listen [::]:80; listen 443 ssl http2; listen [::]:443 ssl http2; ssl_certificate /usr/local/nginx/conf/ssl/xxx.crt; ssl_certificate_key /usr/local/nginx/conf/ssl/xxx.key; ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; ssl_ciphers TLS......; ssl_prefer_server_ciphers on; ssl_session_timeout 10m; ssl_session_cache builtin:1000 shared:SSL:10m; ssl_buffer_size 1400; add_header Strict-Transport-Security max-age=15768000; ssl_stapling on; ssl_stapling_verify on; server_name xxx.com www.xxx.com; access_log /xxx.com_nginx.log combined; index index.html index.htm index.jsp; root /xxx.com/webapp; if ($host != www.xxx.com) { return 301 $scheme://www.xxx.com$request_uri; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ { expires 30d; access_log off; } location ~ .*\.(js|css)?$ { expires 7d; access_log off; } location ~ /(\.user\.ini|\.ht|\.git|\.svn|\.project|LICENSE|README\.md) { deny all; } location ^~ /websocket.ws/ { proxy_pass http://127.0.0.1:8099; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; } location ~ { proxy_pass http://127.0.0.1:8099; include proxy.conf; } }
proxy_set_header Upgrade $http_upgrade;
前面插一行试试