0

I've read a lot of instructions since yesterday about this issue but all of them have similar steps. However I followed step by step but still can't get everything Ok.

Actually I can make Flask+Gunicorn+supervisor working but Nginx is not working well.

I connect my remote cloud server with SSH and I'm not deploying the site on my computer.

Nginx is installed correctly because when I visit the site via the domain name (aka. example.com) it shows the Nginx welcome page.

I use supervisor to start Gunicorn and the configuration is

[program:myapp]
command=/home/fh/test/venv/bin/gunicorn -w4 -b 0.0.0.0:8000 myapp:app
directory=/home/fh/test
startsecs=0
stopwaitsecs=0
autostart=false
autorestart=false
stdout_logfile=/home/fh/test/log/gunicorn.log
stderr_logfile=/home/fh/test/log/gunicorn.err

here I bind the server to port 8000 and I don't actually know what does 0.0.0.0 stand for but I think it doesn't mean the localhost because I can visit the site via http://example.com:8000 and it works well.

Then I tried to use Nginx as a proxy server.

I deleted /etc/nginx/sites-available/default' and '/etc/nginx/sites-enabled/default/ and created /etc/nginx/sites-available/test.com and /etc/nginx/sites-enabled/test.com and symlink them.

test.com

server {
        server_name www.penguin-penpen.com;
        rewrite ^ http://example/ permanent;
}

# Handle requests to example.com on port 80
server {
        listen 80;
        server_name example.com;

        # Handle all locations
        location / {
                # Pass the request to Gunicorn
                proxy_pass http://127.0.0.1:8000;

                # Set some HTTP headers so that our app knows where the request really came from
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
}

To my understanding, what Nginx do is when I visit http://example.com it passes my request to http://example.com:8000.

I'm not quite sure that I should use proxy_pass http://127.0.0.1:8000 here because I don't know whether should Nginx pass the request to localhost But I 've tried to change it to 0.0.0.0:8000 but it still doesn't work.

Can anyone help?

jinglei
  • 101
  • 3

1 Answers1

0

What nginx does is for the location / will pass the request to the proxy_pass value. In this case will send the request to http://127.0.0.0:8000 meaning your gunicorn has to listen on that interface. Usually gunicorn listens on localhost such as

 gunicorn --bind 127.0.0.1:8000 myapp:app

That is because you don't want external users to connect to gunicorn directly but rather trough nginx.

This is a sample nginx conf that works fine with gunicorn started as above

   location / {
    proxy_read_timeout 120;
    proxy_pass  http://127.0.0.1:8000;
    proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
    proxy_redirect off;
    proxy_buffering off;
    proxy_set_header        Host            $host;
    proxy_set_header        X-Real-IP       $remote_addr;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header        X-Forwarded-Proto $scheme;
}
silviud
  • 2,687
  • 2
  • 18
  • 19