yarek
yarek

Reputation: 12064

why nginx location returns 404 error?

Here is my /usr/local/nginx/conf/nginx.conf:

server {
        listen       8080;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }

        location /test{
                root /var/www/test;
                index index.html index.htm;
        }

When I type in: http://localhost:8080/ : I got the nginx welcome page : good

When I type in http://localhost:8080/test : I got a 404 error (I created index.html and index.htm inside /var/www/test)

PS I did reload with :

/usr/local/nginx/sbin/nginx -s reload

Upvotes: 2

Views: 6296

Answers (2)

AMAL MOHAN N
AMAL MOHAN N

Reputation: 1622

The issue here is that, nginx searches for the folder that we added as location in the root we provided, location \test and root /var/www/test in this case. As /test is not in /var/www/test it's showing error. If used /var/www instead it will work fine as nginx uses root + location.

To confirm, you're facing the same issue check the nginx error log. If you haven't changed the log location then the following command will do the job.

sudo tail -n 20 /var/log/nginx/error.log

and if it shows a message like below ,you can confirm the issue is same.

[error] 7480#7480: *1 open() "/var/www/test/test" failed (2: No such file or directory), client: XXX.XXX.XX.XX, server: , request: "GET /app HTTP/1.1", host: "XX.XX.XXX.XX"

to resolve this issue change the code as below,


server {

    listen       8080;
    server_name  localhost;

    location / {
        root   html;
        index  index.html index.htm;
    }

    location /test {
        root /var/www;
        index index.html index.htm;
    }

after the changes, restart the nginx service

sudo systemctl restart nginx

Upvotes: 1

Indent
Indent

Reputation: 4967

Remove /test to root directive. You need just indicate /var/www

server {
    listen       8080;
    server_name  localhost;

    location / {
        root   html;
        index  index.html index.htm;
    }

    location /test {
        root /var/www;
        index index.html index.htm;
    }

Upvotes: 2

Related Questions