Search code examples
nginxnginx-confignginx-location

The issue of try_files configure in nginx.conf


my nginx version is 1.16.1. this is my try_filse configure:

location / {
     root /home/nginx/html/dist/;
     try_files $uri $uri/ /index.html;
}

location = /index.html {
     root  /home/nginx/html/dist/;
}

as what said on nginx.org, if $uri and $uri failed. will redirect to /index.html. pay attention to my root configure and i do not configure index config, so the index default value will be index.html.

  1. create index.html in file path /home/nginx/html/dist/home/nginx/html/dist/index.html
  2. 127.0.0.1/home/nginx/html/dist/ in browser. defalut index file, index.html will be found, so the finally file path found by nginx is that rootpaht + rui + default index file = /hmoe/nginx/html/dist/home/nginx/html/dist/idnex.html
  3. if index.html existed in the file path of step1, it works. but there was no index.html, failed. Refer to nginx.org, the request will be indirect to from /home/nginx/html/dist/home/nginx/html/dist/index.html to the root path /home/nginx/html/dist/index.html.
  4. but the result awlays 403 forbiden.

i trace the source code and debug them step by step, if redirection triggered .this branch must be tirggerd. enter link description here but tf->names is /index.html, never be null.

so the reindrect function will be called. enter link description here

but always goto to this code , 403 forbiden. because open file failed in the ngx_http_static_modue. enter link description here

if tf->names is /index.html, this record never be null, the branch and redirect never be triggerd, maybe a bug.


Solution

  • has sovled, misunderstanding of try_files. i have asked same question on nginx slack community. get correct answer. anywhy thank you anyone even that guy vote -1.