解決 Nginx 400 Bad Request 問題(WebSocket)

400 Bad Request 是一種 HTTP 錯誤狀態碼。HTTP/1.1 對 400 Bad Request的定義主要是:html

  • 語義有誤,當前請求沒法被服務器理解
  • 請求參數有誤
  • 丟包致使異常

Google 了一番,不少說是請求頭或 cookie 過大引發的,調整  client_header_buffer_size 與 large_client_header_buffers 大小,可是並無解決問題。通過排查發現 GET 請求的時候沒有問題,POST 的時候就會返回 400 Bad Request 錯誤,最後發現是 websocket 配置成了全局 ,單獨配置 websocket 的地址便可。 linux

server {
         listen 80;
         # https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/linux-nginx?view=aspnetcore-2.2 
         location / {
                proxy_pass         http://localhost:5000;
                proxy_http_version 1.1;
                proxy_set_header   Upgrade $http_upgrade;
                proxy_set_header   Connection keep-alive;
                proxy_set_header   Host $host;
                proxy_cache_bypass $http_upgrade;
                proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header   X-Forwarded-Proto $scheme;
         }
         # https://www.nginx.com/blog/websocket-nginx/
         location /hqHub {
              proxy_pass http://localhost:5000;
              proxy_http_version 1.1;
              proxy_set_header Upgrade $http_upgrade;
              proxy_set_header Connection "upgrade";
           }
       }
 
 server {
         listen 8080;
         location / {
            proxy_pass            http://127.0.0.1:6800/;
            auth_basic            "Restricted";
            auth_basic_user_file  /etc/nginx/conf.d/.htpasswd;
          }
       }

REFER:
http://nginx.org/en/docs/http/websocket.html
https://blog.csdn.net/zhuyiquan/article/details/78707577
https://xiaomingyang.com/2018/04/17/centos-nginx-error.htmlnginx

相關文章
相關標籤/搜索