在 nginx 服务器 rhel 上部署 django 应用程序 - 400 bad request Request Header or cookie too large

Posted

技术标签:

【中文标题】在 nginx 服务器 rhel 上部署 django 应用程序 - 400 bad request Request Header or cookie too large【英文标题】:Deploying django application on nginx server rhel - 400 bad request Request Header or cookie too large 【发布时间】:2018-04-09 08:21:06 【问题描述】:

我目前正在尝试使用 nginx 在 REHL 7.4 服务器上部署 Django 应用程序。我遵循了这些教程:

https://simpleisbetterthancomplex.com/tutorial/2017/05/23/how-to-deploy-a-django-application-on-rhel.html

https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04

virtualenv 和 nginx 服务器似乎没问题。但是我正在努力解决两个错误:

由于 worker_connections 参数值(以下是日志),我得到了 500 错误:

13494#0: *1021 1024 worker_connections 在连接上游时不够用,客户端:192.168.1.33,服务器:192.168.1.33,请求:“GET /Syc/login HTTP/1.0”,上游:“http://192.168.1.33:80/Syc/login ", 主机: "192.168.1.33"

要么我将 worker_connections 值增加到 > 4096,然后我得到一个 400 错误,就像在这个线程 400 Bad Request - request header or cookie too large

以下是我的 nginx.conf 和 app.conf,如果有配置错误请告诉我,提前感谢任何帮助。

nginx.conf:

include /usr/share/nginx/modules/*.conf;

events 
    worker_connections 1024;

# set open fd limit to 30000
worker_rlimit_nofile 30000;

http 
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;
    
    server 
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;
        large_client_header_buffers 4 32k;
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / 
        

        error_page 404 /404.html;
            location = /40x.html 
        

        error_page 500 502 503 504 /50x.html;
            location = /50x.html 
        
    

app.conf

upstream app_server 
    server unix:/opt/sycoma/gunicorn.sock fail_timeout=0;


server 
    listen 80;
    server_name 192.168.1.33;  # <- insert here the ip address/domain name
    large_client_header_buffers 4 16k;
    keepalive_timeout 5;
    client_max_body_size 4G;

    access_log /opt/sycoma/logs/nginx-access.log;
    error_log /opt/sycoma/logs/nginx-error.log;

    location /static/ 
        alias /opt/sycoma/venv/Sycoma/Syc/static/;
    

    location /media/ 
        alias /opt/sycoma/venv/Sycoma/media/;
    

    location / 
        try_files $uri @proxy_to_app;
    

    location @proxy_to_app 
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect off;
      proxy_pass http://192.168.1.33;
    

【问题讨论】:

【参考方案1】:

尝试删除/评论该行:

proxy_set_header Host $http_host;

或增加large_client_header_buffers

【讨论】:

第一个(删除 proxy_set_header ...)对我有用。但为什么?干扰这个的 RHEL 是什么?在 CentOS 和 Ubuntu 下,该行可以正常工作。

以上是关于在 nginx 服务器 rhel 上部署 django 应用程序 - 400 bad request Request Header or cookie too large的主要内容,如果未能解决你的问题,请参考以下文章

Linux-rhel6.4部署nginx

部署Django到云服务器(Centos+Nginx+Mysql+Uwsgi+Python3)操作篇

Django+ uwsgi+nginxf服务部署

Red Hat Enterprise 8.5 A key deployment Install Nginx

Red Hat Enterprise 8.5 A key deployment Install Nginx

LNMP部署Nginx+FastCGINginx高级技术