nginx 400 Bad Request -请求头或cookie太大

6vl6ewon  于 2023-11-17  发布在  Nginx
关注(0)|答案(6)|浏览(494)

我收到了一个400 Bad Request请求头或cookie太大,从nginx与我的Rails应用程序。重新启动浏览器修复了这个问题。我只在我的cookie中存储了一个字符串id,所以它应该很小。
我在哪里可以找到nginx错误日志?我看了nano/opt/nginx/logs/error.log,但它没有任何相关内容。
我试着设置以下和没有运气:

  1. location / {
  2. large_client_header_buffers 4 32k;
  3. proxy_buffer_size 32k;
  4. }

字符串
nginx.conf

  1. #user nobody;
  2. worker_processes 1;
  3. #error_log logs/error.log;
  4. #error_log logs/error.log notice;
  5. #error_log logs/error.log info;
  6. #pid logs/nginx.pid;
  7. events {
  8. worker_connections 1024;
  9. }
  10. http {
  11. passenger_root /home/app/.rvm/gems/ruby-1.9.3-p392/gems/passenger-3.0.19;
  12. passenger_ruby /home/app/.rvm/wrappers/ruby-1.9.3-p392/ruby;
  13. include mime.types;
  14. default_type application/octet-stream;
  15. sendfile on;
  16. keepalive_timeout 65;
  17. client_max_body_size 20M;
  18. server {
  19. listen 80;
  20. server_name localhost;
  21. root /home/app/myapp/current/public;
  22. passenger_enabled on;
  23. #charset koi8-r;
  24. #access_log logs/host.access.log main;
  25. # location / {
  26. # large_client_header_buffers 4 32k;
  27. # proxy_buffer_size 32k;
  28. # }
  29. # location / {
  30. # root html;
  31. # index index.html index.htm;
  32. # client_max_body_size 4M;
  33. # client_body_buffer_size 128k;
  34. # }
  35. #error_page 404 /404.html;
  36. # redirect server error pages to the static page /50x.html
  37. #
  38. error_page 500 502 503 504 /50x.html;
  39. location = /50x.html {
  40. root html;
  41. }
  42. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  43. #
  44. #location ~ \.php$ {
  45. # proxy_pass http://127.0.0.1;
  46. #}
  47. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  48. #
  49. #location ~ \.php$ {
  50. # root html;
  51. # fastcgi_pass 127.0.0.1:9000;
  52. # fastcgi_index index.php;
  53. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  54. # include fastcgi_params;
  55. #}
  56. # deny access to .htaccess files, if Apache's document root
  57. # concurs with nginx's one
  58. #
  59. #location ~ /\.ht {
  60. # deny all;
  61. #}
  62. }
  63. # another virtual host using mix of IP-, name-, and port-based configuration
  64. #
  65. #server {
  66. # listen 8000;
  67. # listen somename:8080;
  68. # server_name somename alias another.alias;
  69. # location / {
  70. # root html;
  71. # index index.html index.htm;
  72. # }
  73. #}
  74. # HTTPS server
  75. #
  76. #server {
  77. # listen 443;
  78. # server_name localhost;
  79. # ssl on;
  80. # ssl_certificate cert.pem;
  81. # ssl_certificate_key cert.key;
  82. # ssl_session_timeout 5m;
  83. # ssl_protocols SSLv2 SSLv3 TLSv1;
  84. # ssl_ciphers HIGH:!aNULL:!MD5;
  85. # ssl_prefer_server_ciphers on;
  86. # location / {
  87. # root html;
  88. # index index.html index.htm;
  89. # }
  90. #}
  91. }


这是我在Firebug中存储cookie的代码和cookie的屏幕截图。我使用Firebug检查存储的会话,我发现New Relic和jQuery也存储cookie;这可能是cookie大小超过的原因吗?
x1c 0d1x的数据

  1. def current_company
  2. return if current_user.nil?
  3. session[:current_company_id] = current_user.companies.first.id if session[:current_company_id].blank?
  4. @current_company ||= Company.find(session[:current_company_id])
  5. end

vxqlmq5t

vxqlmq5t1#

这就是错误所显示的-Request Header Or Cookie Too Large。你的一个头很大,nginx拒绝了它。
使用large_client_header_buffers是正确的。如果您查看文档,您会发现它仅在httpserver上下文中有效。将其提升到服务器块,它将正常工作。

  1. server {
  2. # ...
  3. large_client_header_buffers 4 32k;
  4. # ...
  5. }

字符串
顺便说一下,默认的缓冲区数量和大小是48k,所以你的坏头必须是超过8192字节的那个。在你的例子中,所有那些cookie(合并组合成一个头)都超过了limit。那些mixpanel cookie特别大。

cwxwcias

cwxwcias2#

通过添加

  1. server {
  2. ...
  3. large_client_header_buffers 4 16k;
  4. ...
  5. }

字符串

nr7wwzry

nr7wwzry3#

关于上面的答案,但有client_header_buffer_size需要提到:

  1. http {
  2. ...
  3. client_body_buffer_size 32k;
  4. client_header_buffer_size 8k;
  5. large_client_header_buffers 8 64k;
  6. ...
  7. }

字符串

sg24os4d

sg24os4d4#

我得到的错误几乎每600请求网页抓取时.首先,假设代理服务器或远程Ngix限制.我已经试图删除所有的cookie和其他浏览器的解决方案,一般由相关职位谈,但没有运气.远程服务器不在我的控制.
在我的例子中,我犯了一个错误,一次又一次地向httpClient对象添加新的头。在定义了一个全局httpclient对象之后,添加了一次头,问题就不会再出现了。这是一个小错误,但不幸的是,我们没有试图理解问题,而是跳到了stackoverflow:)有时候,我们应该尝试自己理解问题。

jm2pwxwz

jm2pwxwz5#

在我的情况下(Cloud Foundry / NGiNX buildpack),原因是指令proxy_set_header Host ...,删除这行后,nginx变得稳定:

  1. http {
  2. server {
  3. location /your-context/ {
  4. # remove it: # proxy_set_header Host myapp.mycfdomain.cloud;
  5. }
  6. }
  7. }

字符串

olqngx59

olqngx596#

在我的例子中,我通过清除网站的该高速缓存解决了这个问题。其中一个cookie太大,导致了这个问题。

相关问题