Nat*_*ong 2 nginx http-status-code-404
我有一个站点,请求特定的 JSON 文件并发现它丢失会触发对后备 JSON 文件的请求。但是,我没有为第一个文件获得 404,而是获得了 的内容index.html
,这不是有效的 JSON 并产生了错误。
我将问题追溯到这个指令:
location / {
# First attempt to serve request as file, then
# as directory, then fall back to index.html
try_files $uri $uri/ /index.html;
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能让它为404
for服务mysite.com/some/nonexistent/file.json
,但仍然得到index.html
for mysite.com/
?
经过一番阅读,我更改了try_files
指令:
# Old way: fallback to index.html
try_files $uri $uri/ /index.html;
# New way: serve a 404 response
try_files $uri $uri/ =404;
Run Code Online (Sandbox Code Playgroud)
另一种选择是:
# Fall back to this error page
try_files $uri $uri/ error_pages/404.html
Run Code Online (Sandbox Code Playgroud)
我仍然有这个指令来处理服务index.html
为mysite.com/
:
server {
# ...
index index.html index.html;
#...
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4373 次 |
最近记录: |