简体   繁体   中英

Run Flask application with Nginx and Gunicorn

it is the first time that I deploy a webapp and I really need of your help. I want to run a Flask web app on a server using Nginx and Gunicorn. I found this tutorial but I can not to run the application correctly. I tried also other ways found in internet, but nothing. This is my current /etc/nginx/sites-available/test.conf file

server {
listen 80;
server_name hello.itu24.com;

root /home/ubuntu/test;

location / {
    proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://127.0.0.1:8000;    
    }
}

Instead this is my app /home/ubuntu/test.py

from flask import Flask
from werkzeug.contrib.fixers import ProxyFix
app = Flask(__name__)

@app.route('/')
def test():
    return "Hello world!"

app.wsgi_app = ProxyFix(app.wsgi_app)

if __name__ == '__main__':
    app.run()

Then I run the following command

ubuntu@ace:~$ sudo service nginx reload
* Reloading nginx configuration nginx                           [ OK ] 
ubuntu@ace:~$ sudo gunicorn -b h 127.0.0.1:8000 test:app

Then if I go to IP address of the machine, since I am connecting from another machine, I can see the Nginx page. But if I add the port in the address IP:8000 no page is found. What am I doing wrong?

Nginx probably has another site defaulting to port 80. Make sure you've deleted the default.conf symlink from /etc/nginx/sites-enabled . Check the nginx.conf to make sure it doesn't have a server configured in it.

Your gunicorn command doesn't look quite right either. You need to use gunicorn -b 127.0.0.1:8000 test:app from your app's directory.

You are binding the gunicorn server only to the local interface 127.0.0.1, so it is not available from outside. You have to use nginx as the reverse proxy at port 80.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM