使用 url 查询参数时,Nginx try_files 回退路径 returns 404
Nginx try_files fallback path returns 404 when using url query args
目标
我有一个文件夹 /assets
,其中包含 index.hml
、css/*
、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
变为 return 与 domain.com/index.html?q=x
相同的响应而不是 404
@richard-smith 提出的答案解决了它
location / {
root /assets;
index index.html;
try_files $uri $uri/ =404;
目标
我有一个文件夹 /assets
,其中包含 index.hml
、css/*
、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
变为 return 与 domain.com/index.html?q=x
相同的响应而不是 404
@richard-smith 提出的答案解决了它
location / {
root /assets;
index index.html;
try_files $uri $uri/ =404;