mercredi 5 octobre 2016

python - How to deploy Flask+Gunicorn+Nginx+supervisor on a cloud server?

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 http://ift.tt/2dTolry;
        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?




Aucun commentaire:

Enregistrer un commentaire