3

I got this Flask app that I'm running with gunicorn on 127.0.0.1:5002 on this shared server. I got a nginx instance for myself that I can add custom configs to. The structure of the nginx directory is as such:

~/.nginx
    | conf.d
    | nginx.conf
    | [...]

Where nginx.conf contains the following:

pid       /media/sdl1/home/USERNAME/.nginx/pid;
error_log /media/sdl1/home/USERNAME/.nginx/error.log;

worker_processes     1;
worker_rlimit_nofile 65535;
events {
    worker_connections 8192;
}

http {
    include        /etc/nginx/mime.types;
    default_type   application/octet-stream;
    access_log     off;
    sendfile       on;
    tcp_nopush     on;
    real_ip_header X-Forwarded-For;

    client_max_body_size  1g;
    client_body_temp_path /media/sdl1/home/USERNAME/.nginx/client;
    fastcgi_temp_path     /media/sdl1/home/USERNAME/.nginx/fastcgi;
    uwsgi_temp_path       /media/sdl1/home/USERNAME/.nginx/uwscgi;
    scgi_temp_path        /media/sdl1/home/USERNAME/.nginx/scgi;

    include conf.d/*.conf;
}

Now that points to the conf.d directory which contains a file called 000-default-server.conf and an empty directory called 000-default-server.d

The 000-default-server.conf contains this:

server {
    listen      8080;
    server_name EXAMPLE.com;
    root        /media/sdl1/home/USERNAME/www/$host/public_html;
    index       index.html index.php;

    autoindex            on;
    autoindex_exact_size off;
    autoindex_localtime  on;
    error_log  /media/sdl1/home/USERNAME/.nginx/error.log  debug;

    # Pass files that end in .php to PHP
    location ~ \.php$ {
        fastcgi_read_timeout 1h;
        fastcgi_send_timeout 10m;

        include      /etc/nginx/fastcgi_params;
        fastcgi_pass unix:/media/sdl1/home/USERNAME/.nginx/php/socket;
    }

    location /apps/whatsmyuserinfo {
        proxy_redirect          off;
        proxy_pass_header       Server;
        proxy_set_header        Host            $http_host;
        proxy_set_header        X-Real-IP       $remote_addr;
        proxy_set_header        X-Scheme        $scheme;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        client_max_body_size    10m;
        client_body_buffer_size 128k;
        proxy_connect_timeout   90;
        proxy_send_timeout      90;
        proxy_read_timeout      90;
        proxy_buffers           32 4k;
        proxy_pass              http://127.0.0.1:5002/;
    }

    # Deny access to anything starting with .ht
    location ~ /\.ht {
        deny  all;
    }

    include conf.d/000-default-server.d/*.conf;
}

With personal information replaced with less revealing values, those values are in all caps.

Now that's the general setup of nginx. Here's where my problem appears. If I use

curl -i 127.0.0.1:5002

through SSH on the server to see if gunicorn is properly serving the app, the app is properly served. If I do it externally, through EXAMPLE.com/apps/whatsmyuserinfo I get a 502 Bad Gateway error. The nginx error log could possibly shed some light on it, here is what it looks like for a single request to EXAMPLE.com/apps/whatsmyuserinfo on debug mode.

"GET / HTTP/1.0
Host: EXAMPLE.com
X-Real-IP: 10.0.0.1
X-Scheme: http
X-Forwarded-For: 188.176.255.124, 10.0.0.1
Connection: close
X-Host: EXAMPLE.com
X-Forwarded-Proto: http
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Cookie: __utma=18102207.768668361.1377199324.1377199324.1377199324.1; __utmz=18102207.1377199324.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)
Cache-Control: max-age=0

"
2013/08/23 19:00:01 [debug] 31195#0: *14 http cleanup add: 0000000001F44D30
2013/08/23 19:00:01 [debug] 31195#0: *14 get rr peer, try: 1
2013/08/23 19:00:01 [debug] 31195#0: *14 socket 10
2013/08/23 19:00:01 [debug] 31195#0: *14 epoll add connection: fd:10 ev:80000005
2013/08/23 19:00:01 [debug] 31195#0: *14 connect to 127.0.0.1:5002, fd:10 #15
2013/08/23 19:00:01 [debug] 31195#0: *14 http upstream connect: -2
2013/08/23 19:00:01 [debug] 31195#0: *14 posix_memalign: 0000000001F3D850:128 @16
2013/08/23 19:00:01 [debug] 31195#0: *14 event timer add: 10: 90000:1377284491613
2013/08/23 19:00:01 [debug] 31195#0: *14 http finalize request: -4, "/apps/whatsmyuserinfo?" a:1, c:2
2013/08/23 19:00:01 [debug] 31195#0: *14 http request count:2 blk:0
2013/08/23 19:00:01 [debug] 31195#0: *14 http run request: "/apps/whatsmyuserinfo?"
2013/08/23 19:00:01 [debug] 31195#0: *14 http upstream check client, write event:1, "/apps/whatsmyuserinfo"
2013/08/23 19:00:01 [debug] 31195#0: *14 http upstream recv(): -1 (11: Resource temporarily unavailable)
2013/08/23 19:00:01 [debug] 31195#0: *14 http upstream request: "/apps/whatsmyuserinfo?"
2013/08/23 19:00:01 [debug] 31195#0: *14 http upstream process header
2013/08/23 19:00:01 [error] 31195#0: *14 connect() failed (111: Connection refused) while connecting to upstream, client: 10.0.0.1, server: EXAMPLE.com, request: "GET /apps/whatsmyuserinfo HTTP/1.0", upstream: "http://127.0.0.1:5002/", host: "EXAMPLE.com"
2013/08/23 19:00:01 [debug] 31195#0: *14 http next upstream, 2
2013/08/23 19:00:01 [debug] 31195#0: *14 free rr peer 1 4
2013/08/23 19:00:01 [debug] 31195#0: *14 finalize http upstream request: 502
2013/08/23 19:00:01 [debug] 31195#0: *14 finalize http proxy request
2013/08/23 19:00:01 [debug] 31195#0: *14 free rr peer 0 0
2013/08/23 19:00:01 [debug] 31195#0: *14 close http upstream connection: 10
2013/08/23 19:00:01 [debug] 31195#0: *14 free: 0000000001F3D850, unused: 48
2013/08/23 19:00:01 [debug] 31195#0: *14 event timer del: 10: 1377284491613
2013/08/23 19:00:01 [debug] 31195#0: *14 reusable connection: 0
2013/08/23 19:00:01 [debug] 31195#0: *14 http finalize request: 502, "/apps/whatsmyuserinfo?" a:1, c:1
2013/08/23 19:00:01 [debug] 31195#0: *14 http special response: 502, "/apps/whatsmyuserinfo?"
2013/08/23 19:00:01 [debug] 31195#0: *14 http set discard body
2013/08/23 19:00:01 [debug] 31195#0: *14 xslt filter header
2013/08/23 19:00:01 [debug] 31195#0: *14 HTTP/1.1 502 Bad Gateway

Most relevant parts of that being:

2013/08/23 19:00:01 [debug] 31195#0: *14 http upstream recv(): -1 (11: Resource temporarily unavailable)
2013/08/23 19:00:01 [error] 31195#0: *14 connect() failed (111: Connection refused) while connecting to upstream, client: 10.0.0.1, server: EXAMPLE.com, request: "GET /apps/whatsmyuserinfo HTTP/1.0", upstream: "http://127.0.0.1:5002/", host: "EXAMPLE.com"
2013/08/23 19:00:01 [debug] 31195#0: *14 HTTP/1.1 502 Bad Gateway

Can anyone see what is happening here, because I don't understand why it refuses to work. I've tried making a new server in the nginx config specifically for the app and having the location be /, I've tried getting rid of most of the proxy settings and just leaving a few like proxy_pass, I've tried creating an upstream in the config and so on.

Full disclosure: I also posted this on stackoverflow, but was told to post on here aswell because it's more likely I would get help here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.