Redirect default (80) port to 5000 - Flask + NGINX + Ubuntu

Viewed 2885

I'm successfully able to run a flask app on my IP:5000 path. A simple Hello World program that shows the output on my browser. Now, what I would like to do is to configure NGINX with a proxy so that if I access only IP which apparently runs on a default port 80, it should navigate to port 5000 and show output of my application.

In other words... This is working : IP:5000 -> Output = Hello world This isn't working: IP -> This site can’t be reached

The server settings that I want to add would be something like this.

server {
    listen       80;
    server_name  MY_IP;

    location / {
        proxy_pass http://127.0.0.1:5000;
    }
}

However, I'm not sure where to add this? Should it be inside http block inside /etc/nginx/nginx.conf?

Updates: Based on the answers given below, I've managed to do the following.

enter image description here enter image description here

I did restart nginx after this. However, I'm still facing the same issue. App works on IP:5000 but does not work on IP

3 Answers

Put the working blocks in a file with any_name.conf inside the folder named /etc/nginx/conf.d and it will be loaded automatically.

You will need to restart your nginx.

update: What are you using to serve flask? if you are using uwsgi, then you should use configurations like this:

include uwsgi_params;
uwsgi_pass unix:path_to_your.sock;

Other options for uwsgi_pass are:

uwsgi_pass localhost:9000;    #normal
uwsgi_pass uwsgi://localhost:9000;   
uwsgi_pass suwsgi://[2001:db8::1]:9090; #uwsgi over ssl

If you are using gunicorn to serve your flask app, then your current configs should be fine, check if your app is running and if you can get your index page or not using 5000 port, then check for other problems. your configs looks good, maybe it's a problem on flask not being run?

The configuration you have mentioned should be in a separate file, assume example.com.conf under /etc/nginx/conf.d. You can put all the configuration in /etc/nginx/nginx.conf and it'll work, it's just that for readability we create separate configuration files which would be auto included when you add it inside conf.d.

Ok, the problem is fixed. As @senaps and @Mukanahallipatna had mentioned, I created the new configuration file under conf.d.

However, the most imp step that I was missing was this part mentioned in the below link.

It is recommended that you enable the most restrictive profile that will still allow the traffic you've configured. Since we haven't configured SSL for our server yet, in this guide, we will only need to allow traffic on port 80.

Reference Link

sudo ufw allow 'Nginx HTTP'

Now, everything is working fine.

Related