简体   繁体   中英

Forwarding Nginx port to gunicorn instance

I was following this tutorial to setup my flask server.

https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-gunicorn-and-nginx-on-ubuntu-18-04#step-6-%E2%80%94-securing-the-application

When I got to step 6 I see that they are setting flask for the whole url but I would like to point it to a specific port.

This is the code I have for my nginx which points. This currenly produces a 404.

server {
        listen 5000;
        server_name site.com;

        location / {
                include proxy_params;
                proxy_pass http://unix:/home/user/project/project.sock;
        }
}

All the other files are the same as the tutorial. I have tried to modify the .sock file but it seems like it was generated automatically and it can't be modified. In addition I need to find a way for nginx to handle this before I worry about handling it from gunicorn.

My end goal is to have nginx foward requests to flask running when a request is sent to 0.0.0.0:5000 and have all other requests 0.0.0.0 , 0.0.0.0/* be handled by nginx.

Any help to undestand all this is really appreciated got lost at this point.

EDIT

my nginx configuration in sites-available

server {
     server_name domain www.domain; 
     location / {
         include proxy_params; 
         proxy_pass http://127.0.0.1:8080/; 
     }
}

If you want flask to be open to a port instead of a file you should override [service] to

[service]
...
ExecStart=/home/sammy/myproject/myprojectenv/bin/gunicorn --workers 3 --bind 127.0.0.1:8000 -m 007 wsgi:app

And change your nginx config to proxy_pass http://127.0.0.1:8000/;

This way you can have access to port 8000 for checking how are working gunicorn and flask . Remember to be careful with firewall rules to secure port 8000 . For a good discussion on which one is better you can try: gunicorn + nginx: Server via socket or proxy?

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