Preslav Rachev
Preslav Rachev

Reputation: 4003

Nginx resolves the wrong route

I have an nginx-based configuration, where the root of the domain should lead to a static page (a splash page) and anything else should be proxied to an internally accessible machine. The configuration looks roughly as follows:

server {
    listen 80;

    location = / {
        root /var/www;
    }

    location ~ / {
        location /robots.txt {
            root /var/www;
        }   
        proxy_pass http://127.0.0.1:9091;
        proxy_set_header Host $host;
    }

}

The problem is that if the second block of code exists, the first one stops being taken into consideration. In other words, nginx starts looking for an index.html file on the 9091 instance, which does not exist. If the proxy_pass block is commented, then the first part goes into effect.

As far as the documentation is concerned, this should not be the case. If the root of my domain is called, Nginx should stop searching after the first block, since it is explicit. Yet, this is not the case.

What should be done here? I do not want to merge the splash page code, with the rest.

Upvotes: 0

Views: 673

Answers (3)

Alexey Ten
Alexey Ten

Reputation: 14354

I guess you have index directive somewhere and this is how index is works.

It should be noted that using an index file causes an internal redirect, and the request can be processed in a different location.

Your first location matches, but then index module cause internal redirect to /index.html and request ends up in second location block.

I would write something like this:

server {
    listen 80;
    root /var/www;

    location = /index.html {
    }

    location = /robots.txt {
    }

    location / {
        proxy_pass http://127.0.0.1:9091;
        proxy_set_header Host $host;
    }
}

Upvotes: 0

cnst
cnst

Reputation: 27218

Your config looks very weird, but there is no indication that it shouldn't work as you seem to intend.

Perhaps you could try something like this? Else, please provide your full configuration (perhaps your cut-down example is missing something important that we should know about).

server {
    listen 80;
    root /var/www;
    location = / {
    }
    location = /index.html {
    }
    location = /robots.txt {
    }
    location / {
        proxy_pass http://127.0.0.1:9091;
        proxy_set_header Host $host;
    }
}

Upvotes: 1

Tan Hong Tat
Tan Hong Tat

Reputation: 6864

Try this:

Replace splash.html to your splash page filename.

# Set root directory for requests
root /var/www;

# Rewrite / to /splash.html
rewrite ^/$ /splash.html break;

location = /splash.html { }

location = /robots.txt { }

location ~* / {
    proxy_pass http://127.0.0.1:9091;
    proxy_set_header Host $host;
}

Upvotes: 0

Related Questions