Fork me on GitHub

解决 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

posted @ 2019-04-17 20:14  花儿笑弯了腰  阅读(11831)  评论(0编辑  收藏  举报