1

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?

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks uzsolt. A quick question... when you say Nginx logs, do you mean access logs? In my access logs, I am able to see only the /common path. Is there any field that I can enable which will show the root path as well?
1

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 /.

1 Comment

You don't need location = /index.html because the location / expression contains it. See my answer.
0

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;
    }
} 

3 Comments

I tried your configuration, but still the same error.
I think your solution below will pollute the html directory. It seems not a good way.
Well, it is just for my own understanding and simplification of the issue that I am facing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.