WebSocket Nginx 线上404

  1. @ServerEndpoint("/websocket.ws/{userId}")
  1. me.add(new UrlSkipHandler("^/websocket.ws", false));

Nginx 线上配置:

  1. server {
  2.   listen 80;
  3.   listen [::]:80;
  4.   listen 443 ssl http2;
  5.   listen [::]:443 ssl http2;
  6.   ssl_certificate /usr/local/nginx/conf/ssl/xxx.crt;
  7.   ssl_certificate_key /usr/local/nginx/conf/ssl/xxx.key;
  8.   ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
  9.   ssl_ciphers TLS......;
  10.   ssl_prefer_server_ciphers on;
  11.   ssl_session_timeout 10m;
  12.   ssl_session_cache builtin:1000 shared:SSL:10m;
  13.   ssl_buffer_size 1400;
  14.   add_header Strict-Transport-Security max-age=15768000;
  15.   ssl_stapling on;
  16.   ssl_stapling_verify on;
  17.   server_name xxx.com www.xxx.com;
  18.   access_log /xxx.com_nginx.log combined;
  19.   index index.html index.htm index.jsp;
  20.   root /xxx.com/webapp;
  21.   if ($host != www.xxx.com) {  return 301 $scheme://www.xxx.com$request_uri;  }
  22.   location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico){
  23.     expires 30d;
  24.     access_log off;
  25.   }
  26.   location ~ .*\.(js|css)?{
  27.     expires 7d;
  28.     access_log off;
  29.   }
  30.   location ~ /(\.user\.ini|\.ht|\.git|\.svn|\.project|LICENSE|README\.md) {
  31.     deny all;
  32.   }
  33.   location /websocket.ws {
  34.     proxy_pass http://127.0.0.1:8099/websocket.ws;
  35.     proxy_connect_timeout 4s; 
  36.     proxy_read_timeout 300s; 
  37.     proxy_send_timeout 10s; 
  38.     proxy_set_header Upgrade $http_upgrade;
  39.     proxy_set_header Connection "upgrade";
  40.   }
  41.   location ~ {
  42.     proxy_pass http://127.0.0.1:8099;
  43.     include proxy.conf;
  44.   }
  45.   
  46. }

线上连接一直报404,本地开发环境是正常的

=======================================================

解决了,将 ~ 改 ^~,^~ 开头表示uri以某个常规字符串开头。

  1. location ^~ /websocket.ws/ {
  2.     proxy_pass http://127.0.0.1:8099;
  3.     proxy_http_version 1.1;
  4.     proxy_set_header Upgrade $http_upgrade;
  5.     proxy_set_header Connection "upgrade";
  6. }

如下正确配置:

  1. server {
  2.   listen 80;
  3.   listen [::]:80;
  4.   listen 443 ssl http2;
  5.   listen [::]:443 ssl http2;
  6.   ssl_certificate /usr/local/nginx/conf/ssl/xxx.crt;
  7.   ssl_certificate_key /usr/local/nginx/conf/ssl/xxx.key;
  8.   ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
  9.   ssl_ciphers TLS......;
  10.   ssl_prefer_server_ciphers on;
  11.   ssl_session_timeout 10m;
  12.   ssl_session_cache builtin:1000 shared:SSL:10m;
  13.   ssl_buffer_size 1400;
  14.   add_header Strict-Transport-Security max-age=15768000;
  15.   ssl_stapling on;
  16.   ssl_stapling_verify on;
  17.   server_name xxx.com www.xxx.com;
  18.   access_log /xxx.com_nginx.log combined;
  19.   index index.html index.htm index.jsp;
  20.   root /xxx.com/webapp;
  21.   if ($host != www.xxx.com) {  return 301 $scheme://www.xxx.com$request_uri;  }
  22.   location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico){
  23.     expires 30d;
  24.     access_log off;
  25.   }
  26.   location ~ .*\.(js|css)?{
  27.     expires 7d;
  28.     access_log off;
  29.   }
  30.   location ~ /(\.user\.ini|\.ht|\.git|\.svn|\.project|LICENSE|README\.md) {
  31.     deny all;
  32.   }
  33.   location ^~ /websocket.ws/ {
  34.     proxy_pass http://127.0.0.1:8099;
  35.     proxy_http_version 1.1;
  36.     proxy_set_header Upgrade $http_upgrade;
  37.     proxy_set_header Connection "upgrade";
  38.   }
  39.   location ~ {
  40.     proxy_pass http://127.0.0.1:8099;
  41.     include proxy.conf;
  42.   }
  43.   
  44. }



评论区

杜福忠

2021-11-30 18:37

proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
前面插一行试试

杜福忠

2021-11-30 18:38

https://jfinal.com/share/2471

tbynet

2021-11-30 19:12

@杜福忠 加了不行,还是404

杜福忠

2021-11-30 20:47

@tbynet 把其他的配置先全部去掉,单独调试websocket,本地也可以安装Nginx做调试

tbynet

2021-11-30 21:30

@杜福忠 已经解决了,是nginx的websocket路由的没匹配上

热门反馈

扫码入社