Luis Liz
Luis Liz

Reputation: 1959

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/; 
     }
}

Upvotes: 0

Views: 2997

Answers (1)

edilio
edilio

Reputation: 1868

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?

Upvotes: 1

Related Questions