解決 Nginx 400 Bad Request 問題(WebSocket)


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

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

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

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.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM