使用URL查询参数时,Nginx try_files后备路径返回404

时间:2019-10-23 20:54:39

标签: nginx nginx-location nginx-config

目标

我有一个文件夹/assets,其中包含index.hmlcss/*js/*等文件。

这是我要完成的事情:

  • /index.html服务器index.html
  • 尝试尝试投放静态文件(css/main.css等)
  • 如果上述操作失败,则返回404

我还需要通过查询参数($args

这是我的enginx部分配置:

location / {
  root /assets;
  try_files $uri $uri/index.html$is_args$args =404;
}

使用此设置,我得到以下结果:

  • domain.com/css/main.css> 200: domain.com/css/main.css

  • domain.com/index.html> 200: domain.com/index.html

  • domain.com/> 200: (renders same as domain.com/index.html)
  • domain.com/index.html?q=x> 200: domain.com/index.html?q=x
  • domain.com/?q=x> 404: Not Found

问题

要使domain.com/?q=x返回与domain.com/index.html?q=x而不是不是404相同的响应,我需要做些什么?

1 个答案:

答案 0 :(得分:0)

@ richard-smith提出的答案已解决

location / {
  root /assets;
   index index.html;
   try_files $uri $uri/ =404;