How i can set for a visitor host to use a specific root path in nginx?

Visitor with IP will use

root /srv/web1

all other Visitors will use

root /srv/web2
  • 2
    So what have you tried so far?
    – Drifter104
    Oct 5, 2015 at 9:01
  • @Drifter104 I have try with if ($remote_addr = 1.1,1,1) { root ... } ... this wont work.
    – nenad007
    Oct 6, 2015 at 15:10

1 Answer 1


While this question is easily answered by simply doing a bit of research and reading the documentation (preferably starting from the Beginner's Guide and then How nginx processes a request), I will answer it anyway.

I assume you have a server with more than one network interface and IP address, where the address is assigned to one interface and other addresses to their respective interfaces.

Nginx is very flexible in this regard, in fact, there always is a server block that is used as default when nginx can't match any other server block by server_name: that is either the first server block of the file or the one which specifies default_server in its listen directive. And remember that you can specify an IP in the listen directive.

That means, in your case:

server {
    listen 80 default_server;
    root /srv/web2;
server {
    root /srv/web1;

As you can see in the example above, the first server block listens on all interfaces on port 80, while the second block listens only on the interface, on port 80. Therefore, anyone visiting your site through the network interface will receive files from /srv/web1, independently from the Host field in the HTTP request (that is, the domain name written in the URL bar).

Another similar behavior can be obtained by putting the IP address in the server_name directive, like so:

server {
    listen 80;
* Note that this last server block only catches requests to the literal IP address and not to domain names.

I hope this answers your question, but next time please put additional information about your current setup and please do some research.


To obtain the described behavior you can use the geo module as explained in this question:

geo $client {
    default web2; web1;
server {
    root /srv/$client;
This config uses the geo module to set a variable ($client) based on the client's IP address. That variable gets set to either the default web2 or the specific web1 and then is substituted in the root directive.

Note that the geo directive must be inside the http block and not server blocks; the snippet above should work as-is, but I don't guarantee anything since I did not test it.

  • sorry my questions was not well written, I need a solution that specific visitors are server by a different root path, means visitor with IP will be server by /srv/web1 and all other by /srv/web2
    – nenad007
    Oct 6, 2015 at 15:08
  • @nenad007 I updated my answer (down below, 'Update' section). If it works for you, feel free to accept the answer. And regarding your other comment about if($remote_addr...), it doesn't work because you cannot put a root directive inside an if block. Read If Is Evil.
    – LaXiS
    Oct 6, 2015 at 18:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.