Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nginx location 404 not found

Here are my nginx configure files.

On the default.conf, the first location is used to access /usr/share/nginx/html directory, it is ok while I access http://47.91.152.99. But when I add up a new location for directory /usr/share/nginx/public directory, nginx return me a 404 page while I access http://47.91.152.99/test.

So, what is the matter? Am I misuse the directive of nginx?

/etc/nginx/nginx.conf

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}


/etc/nginx/conf.d/default.conf
server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    location ^~ /test/ {
        root /usr/share/nginx/public;
        index index.html index.htm;
    }
    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}
like image 634
jamesxu-e.g. Avatar asked Dec 12 '16 10:12

jamesxu-e.g.


People also ask

How do I fix nginx 404 not found?

We also provided you the methods to fix the “404 Not Found Error” by utilizing the Nginx configuration file and the other online tools such as Check My Links, W3C Check Link, and the Broken Link Checker.

How does nginx location work?

The location directive within NGINX server block allows to route request to correct location within the file system. The directive is used to tell NGINX where to look for a resource by including files and folders while matching a location block against an URL.

How do I fix 404 not found?

The simplest and easiest way to fix your 404 error code is to redirect the page to another one. You can perform this task using a 301 redirect. What's 301, you may ask? It's a redirect response code that signals a browser that the content has been transferred to another URL.


4 Answers

The following erroneous block (in your case);

 location ^~ /test/ {
     root /usr/share/nginx/public;
     index index.html index.htm;
 }

is telling nginx to look for directory 'test' into the folder (root) /usr/share/nginx/public. If there's no 'test' folder in that root, it will return 404. To paliate to your problem, i suggest you try using alias instead of root. Like so:

 location ^~ /test/ {
     alias /usr/share/nginx/public;
     index index.html index.htm;
 }

Also, just for kicks, index directive can be set generally so you don't have to re-write it all the time... like so;

 server {
     listen       80;
     server_name  localhost;

     root   /usr/share/nginx/html;
     index  index.html index.htm;

     error_page   500 502 503 504  /50x.html;

     location / { }

     location ~^/test/ {
         alias /usr/share/nginx/public;
     }

     location = /50x.html {
         root   /usr/share/nginx/html;
     }
 }

One thing you should also consider... the more 'precise' the location block, the higher in your config it should reside. Like that location = /50x.html. In a perfect world, that would be set up top, right after the general server block settings.

Hope it helps.

like image 67
OldFart Avatar answered Sep 23 '22 00:09

OldFart


Error caused by root directive

location ^~ /test/ {
    root /usr/share/nginx/public;
    index index.html index.htm;
}

Fix with alias directive

 location ^~ /test/ {
     alias /usr/share/nginx/public;
     index index.html index.htm;
 }

Other Improvements

Extra tip: the index directive can be set so that you don't have to re-write it.

server {
    listen       80;
    server_name  localhost;

    root   /usr/share/nginx/html;
    index  index.html index.htm;

    error_page   500 502 503 504  /50x.html;

    location / { }

    location ~^/test/ {
        alias /usr/share/nginx/public;
    }

    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

nginx matches Location blocks partly based on position in the config. Ideally, you would invert what you have now. The location block would be higher in nginx config. To that end, the location = /50x.html would also move up. Order is

  1. Exact match =
  2. Forward match ^~ /
  3. Case sensitive regex ~ /
  4. Case insensitive regex ~*
  5. Path match /

More about nginx location priority. Also, you can always review the official documentation. The nginx documentation for location block http://nginx.org/en/docs/http/ngx_http_core_module.html#location

like image 41
gtzilla Avatar answered Sep 23 '22 00:09

gtzilla


when your app is vuejs,you need write like this,can prevent 404,pay attention to double /test/

   location ^~/test/ {
       alias /usr/local/soft/vuejs/;
       try_files $uri $uri/ /test/index.html;
    }
like image 24
yongfa365 Avatar answered Sep 23 '22 00:09

yongfa365


I just solved this (index.html not found) issue.
For me, I misstyped my project name to match your ec2 project name with the nginx path.

Move to nginx/sites-enabled to check nginx path

  1. cd /etc/nginx/sites-enabled
  2. cat your_project_name
  3. Check your nginx path (for me : root/home/ubuntu/practice/current/public;)

Move to home directory to check your project name
4. cd
5. ls
6. If your ec2 project name(for me: practice) is not match with your nginx path name(for me: practice) then you might got "index.html not found error"

like image 33
기재민 Avatar answered Sep 23 '22 00:09

기재민