大网数据广告活动
服务器运维问答

Nginx proxy pass to localhost Gunicorn returns 50x error unexpectedly

· 52CE 我爱测

I have a very basic set up with nginx installed with a django app running on a gunicorn server. The gunicorn conf is simple and below: exec gunicorn myapp.wsgi:application \ --workers 5 --reload I've set up nginx server to proxy_…

问题原文

原帖链接 · EazyC · CC BY-SA 3.0

I have a very basic set up with nginx installed with a django app running on a gunicorn server. The gunicorn conf is simple and below:

exec gunicorn myapp.wsgi:application \
    --workers 5
    --reload

I've set up nginx server to proxy_pass all incoming traffic on the default server to 127.0.0.1:8000. However, when I go to the IP address of the server, I see the default nginx 404 screen. So I am assuming that the traffic was proxy passed by the nginx server but somehow was not properly routed and no response was given back which forces nginx to return a 404. What exactly am I doing wrong on either the gunicorn or nginx configuration here? For reference, below is my full nginx.conf file as well:

user nginx; worker_processes auto; 

error_log /var/log/nginx/error.log; 
pid /run/nginx.pid;

    # Load dynamic modules. See /usr/share/nginx/README.dynamic. include /usr/share/nginx/modules/*.conf;

    events {
        worker_connections 1024; }

    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;

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
        include /etc/nginx/sites-available/*

    upstream app_server {
        server 127.0.0.1:8000 fail_timeout=0;
}

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        proxy_pass http://127.0.0.1:8000;
        }

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

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

    }

EDIT: After more closely looking into the error message in this setup I realize it's not a 404 and it's actually a 50x so something must definitely be wrong in the way the 2 services are communicating with each other. Going to the gunicorn endpoint alone produces the django app with no problems. As does going to the default nginx splace screen before adding the proxy_pass parameter. Any help, what exactly am I missing. Thanks

已采纳解答

原帖链接 · EazyC · CC BY-SA 3.0

Solved my own issue. Basically for fedora based solutions running nginx you must open up access to upstream connections to all users that are not root. You can do that with this command

setsebool -P httpd_can_network_connect 1

Obviously run above as root. Further info can be found on this other question: https://stackoverflow.com/questions/23948527/13-permission-denied-while-connecting-to-upstreamnginx

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-06-28)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。