Rahul Soni
Rahul Soni

Reputation: 4968

Nginx configuration location

I have the following Nginx configuration file...

server {
    listen 80;
    server_name 127.0.0.1 localhost;
    location = /index.html {
            root /etc/nginx/html/app1;
            index index.html;
    }

    location / {
            root /etc/nginx/html/app1;
            index index.html;
    }

    location /common/ {
            root /etc/nginx/html/common;
    }
}

And the folder structure is like so...

html\app1

html\common

When I try to browse...

http://localhost/ > Works

http://localhsot/index.html > Works

http://localhost/common/somefile.txt > Doesn't work

What am I missing?

Upvotes: 1

Views: 5244

Answers (3)

uzsolt
uzsolt

Reputation: 6027

You should use alias instead of root:

server {
  listen 80;
  server_name 127.0.0.1 localhost;

  location / {
    root /etc/nginx/html/app1;
    index index.html;
  }

  location /common {
    alias /etc/nginx/html/common;
  }
}

If you use root in common the 127.0.0.1/common/somefile.txt will try /etc/nginx/html/common/common/somefile.txt (notice the two common). If you check nginx's logs you can see it.

Upvotes: 2

Rahul Soni
Rahul Soni

Reputation: 4968

I am adding my own answer since I finally got it working. Posting it here, so it might help others...

server {
listen 80;
server_name 127.0.0.1 localhost;
location = /index.html {
        root /etc/nginx/html/app1;
        index index.html;
}

location / {
        root /etc/nginx/html/app1;
        index index.html;
}

location ^~ /common/ {
        root /etc/nginx/html;
}
}

Basically, the way Nginx was trying was /etc/nginx/html/common/common. Removing the common from root worked. Also found that http://localhost:8888/common/ needed to have a trailing /.

Upvotes: 1

Jianjian Yang
Jianjian Yang

Reputation: 1234

Because it firstly match the location /. You can do it like this:

server {
    listen 80;
    server_name 127.0.0.1 localhost;
    location = /index.html {
            root /etc/nginx/html/app1;
            index index.html;
    }

    location / {
            root /etc/nginx/html/app1;
            index index.html;
    }

    location ^~ /common/ {
            root /etc/nginx/html/common;
    }
} 

EDIT: Yeah. It seems some complicated. You can do it like this:
First, you need create a new server:

server {
    listen 80;
    server_name common.com;   # A virtual host
    root /etc/nginx/html/common;
}

Then, you need modify the config above like this:

server {
    listen 80;
    server_name 127.0.0.1 localhost;
    location = /index.html {
            root /etc/nginx/html/app1;
            index index.html;
    }

    location / {
            root /etc/nginx/html/app1;
            index index.html;
    }

    location ^~ /common/ {
             rewrite ^/common(/.*)$  $1  break; # rewrite the /common/
             proxy_set_header Host common.com;  # it will requests common.com which the server of 127.0.0.1. then will match the above server.
             proxy_pass http://127.0.0.1;
    }
} 

Upvotes: 0

Related Questions