[SOLVED] Nginx Location Directive fail

Issue

This Content is from Stack Overflow. Question asked by aledruetta

I’m trying this nginx.conf:

http {
    server {
        listen 80;

        location / {
            root /usr/share/nginx/html/;
        }

        location /pages/ {
            root /usr/share/nginx/html/static/;
        }
    }
}

Inside /usr/share/nginx/html/static/pages/ there is a file page1.html.

But, when I do GET /pages/page1.html, I receive this error: [error] 31#31: *2 open() "/usr/share/nginx/html/pages/page1.html" failed (2: No such file or directory), client: 172.17.0.1, server: , request: "GET /pages/page1.html HTTP/1.1", host: "localhost:8080".

Nginx is a dockerized container with port 8080 mapped to 80.

Why doesn’t Nginx concatenate /static/ in the path?



Solution

Based on recommendation made by @palindromeotter33, this is the answer:

http {
    server {
        listen 80;

        location / {
            root /usr/share/nginx/html/;
        }

        location /pages/ {
            alias /usr/share/nginx/html/static/pages/;
        }
    }
}


This Question was asked in StackOverflow by aledruetta and Answered by aledruetta It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?