pexea12
pexea12

Reputation: 1209

nginx redirect to other application if 404

I am running two Flask applications on my server. The first one has an nginx config in /etc/nginx/sites-available/alpha-project:

server {
    listen 80;
    location / {
        include uwsgi_params;
        uwsgi_pass 127.0.0.1:3033;
    }
}

Therefore, whenever I visit mywebsite.com, I can use this application (named alpha-project).

Now I have another flask applicaton called beta-project with the config file /etc/nginx/sites-available/beta-project:

server {
    listen 80;
    location /beta {
        include uwsgi_params;
        uwsgi_pass 127.0.0.1:3034;
    }
}

I want to use my beta-project whenver I visit mywebsite.com/beta. However when I visited mywebsite.com/beta, the server always returned 404. It seemed like I was still using the alpha application.

How can I make nginx redirect to my beta application?

Upvotes: 0

Views: 115

Answers (1)

Tarun Lalwani
Tarun Lalwani

Reputation: 146610

Your problem is that both servers don't have a virtual name. You need to either combine them into one config

server {
    listen 80;
    location / {
        include uwsgi_params;
        uwsgi_pass 127.0.0.1:3033;
    }

    location /beta {
        include uwsgi_params;
        uwsgi_pass 127.0.0.1:3034;
    }
}

Or you need to use two separate virtual server names

server {
    listen 80;
    server_name servera;
    location / {
        include uwsgi_params;
        uwsgi_pass 127.0.0.1:3033;
    }
}

server {
    listen 80;
    server_name serverb;
    location / {
        include uwsgi_params;
        uwsgi_pass 127.0.0.1:3033;
    }
}

Then you can reach main server at http://servera/ and other one at http://serverb/beta. Both servera and serverb names should resolve to the IP using /etc/hosts or the DNS names

Upvotes: 1

Related Questions