Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nginx return a file when request for path

Tags:

nginx

As title descript,when i request for "http://example.com/test/poster/",i want to get the real file "/home/vagrant/example/resouces/views/posters.html".

Here is my nginx configuration:

server_name example.com;
root /home/vagrant/example/public;
index index.html;

location / {
    return 404;
}

location ~ ^/test/poster/?$ {
    default_type text/html;
    index posters.html;
    alias /home/vagrant/example/resouces/views/;
}

But when i make the request,i get a 404 response.I search for the nginx error log,i get the info below:

2018/10/20 11:54:41 [debug] 4690#4690: *180 http script copy: "/home/vagrant/example/resouces/views/"
2018/10/20 11:54:41 [debug] 4690#4690: *180 open index "/home/vagrant/example/resouces/views/posters.html"
2018/10/20 11:54:41 [debug] 4690#4690: *180 internal redirect: "/test/poster/posters.html?"
2018/10/20 11:54:41 [debug] 4690#4690: *180 rewrite phase: 1

Here is my question,when open index,it has find the file,why does it act a internal redirect?

I finally resolve it by using try_files,i just want to know why it dose not work when i use alias with index in one location block.Thanks!

like image 304
poettian Avatar asked Mar 02 '26 23:03

poettian


2 Answers

The index module performs a multistage process.

If the URI ends with a / and is found to be a directory within the location that is currently processing the request, Nginx will check for a file matching a name in the index directive's value.

If a matching file is found, the URI will be changed by appending that name, and the search for a location to process the new URI will commence.

In your case, the URI /test/poster/posters.html does not match the original location and therefore the posters.html file is not found.

like image 76
Richard Smith Avatar answered Mar 04 '26 19:03

Richard Smith


  1. If the uri end with '/',nginx will combine index file and alias path as a fullpath index file and to check if this file exist.
  2. If it exist,then a internal redirect will commence;if not exist,nginx will check if the alias path is a dir.
  3. If the alias path is a dir then nginx will try other index file until it return a 403;If the alias path is not a dir,nginx will directly return a 404.

  1. If the uri not end with '/',nginx will use the alias path directly.
  2. If the alias path is a file but not exist,nginx will return a 404;If the alias path is a dir,nginx will return a 301 with a trailing slash appended with origin uri.
like image 26
poettian Avatar answered Mar 04 '26 18:03

poettian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!