26

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;
    }
}
Paulo Boaventura
  • 1,365
  • 1
  • 9
  • 29
jamesxu-e.g.
  • 650
  • 2
  • 10
  • 22
  • Do you mean `http://47.91.152.99/test/` (with a trailing `/`)? Is the file located at `/usr/share/nginx/public/test/index.html`? – Richard Smith Dec 12 '16 at 12:24
  • Yes, with a trailing /. And index.html is in the directotry. – jamesxu-e.g. Dec 13 '16 at 12:51
  • `index.html` is in which directory? Your question implies `/usr/share/nginx/public`, but your configuration file uses `/usr/share/nginx/public/test` – Richard Smith Dec 13 '16 at 12:56
  • The reason is you set your Debug to False, so you won't see the response from http://47.91.152.99. If you would like to see the response from http://47.91.152.99 set your Debug to True – Teshie Ethiopia Jul 21 '23 at 07:11

6 Answers6

39

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.

OldFart
  • 1,640
  • 11
  • 20
  • 3
    It doesn't work. I still get the 404 not found response. – jamesxu-e.g. Dec 13 '16 at 13:05
  • When access the url http://47.91.152.99/test, I want nginx server access the root directory /usr/share/nginx/public. – jamesxu-e.g. Dec 13 '16 at 13:14
  • It will be a match for requests starting with /images/ (location / also matches such requests, but has shorter prefix). The resulting configuration of the server block should look like this: server { location / { root /data/www; } location /images/ { root /data; } } – jamesxu-e.g. Dec 13 '16 at 13:18
  • 2
    After I create test directory under /usr/share/nginx/public, then the response is okay. Thank you for you reply. – jamesxu-e.g. Dec 13 '16 at 13:27
14

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

gtzilla
  • 1,265
  • 1
  • 16
  • 21
  • Moderator comment: "This edit was intended to address the author of the post and makes no sense as an edit. It should have been written as a comment or an answer." Comment wasn't long enough. What would you have preferred? – gtzilla Nov 30 '18 at 16:52
1

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;
    }
yongfa365
  • 352
  • 3
  • 7
  • there is not alot of value added here. Specially with using such a bloating try_files directive. $uri and $uri/ are already beeing seen and dealt with by nginx nature so nothing useful in using this. In the same respect, having index.html with the same path as the location block makes no sens and again, is something that the 'index' directive takes care of without the need to explicitly repeat it. There is no harm in doing it, per say, but certainly doesnt help anyone understand the concepts as a whole. – OldFart May 18 '22 at 02:24
1

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"

ouflak
  • 2,458
  • 10
  • 44
  • 49
기재민
  • 21
  • 5
  • I must stress that the 'site-enabled' and disabled directory format comes from Apache httpd and is usually found on Debian and similar distrubutions. A native nginx distributed package does NOT include such directories. Also, it is of note that wherever you put your configuration files, nginx.conf must have the needed 'include' directive in order to process it. – OldFart May 18 '22 at 02:18
  • This looks more like a general, very basic tutorial in using a keyboard, legacy tools and proper 'grammar' related to the environment – OldFart May 18 '22 at 02:29
0

and my server-blocks.conf

server {

        listen 80;

        index index.html index.htm index.nginx-debian.html;

        server_name 13.xxx.xxx.xx;

        location / {
        root /var/www/portaladmin/;


                proxy_pass http://13.xxx.xxx.xx:80/;

        }
         error_log /var/log/nginx/portaladmin-erorr.log;

}

and my load-balancer.conf

server {
  listen 80;
  server_name xx.xxx.xxx.xx
  access_log /home/ec2-user/logs/lb-access.log;
  error_log /home/ec2-user/logs/lb-error.log;

  location / {
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://13.xxx.xxx.xx:80;
           }
}
Reyyzzy
  • 1
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 12 '21 at 05:19
0

Creating nginx & sftp pods using Kubernetes.. I've found that the mountPath in my sftp-deployment.yaml file is related to the username in my sftp-server.

And the error has happened when I have changed the username without changing mountPath value to match my username. So, the files were uploading to '/home/old-username' instead of '/home/new-username'.