我目前有一台预渲染工作的Nginx服务器。但由于某些原因,当找不到资源时,会将其重定向到index.html页面,而不是显示404。
这是我的配置,任何帮助将不胜感激。
server {
listen 80;
server_name localhost;
root /home/administrator/public_html;
index index.html;
location / {
try_files $uri @prerender;
}
location @prerender {
error_page 404 = @normal;
#proxy_set_header X-Prerender-Token TOKEN;
set $prerender 0;
if ($http_user_agent ~* "baiduspider|twitterbot|facebookexternalhit|rogerbot|linkedinbot|embedly|quora link preview|showyoubot|outbrain|pinterest|slackbot|vkShare|W3C_Validator") {
set $prerender 1;
}
if ($args ~ "_escaped_fragment_") {
set $prerender 1;
}
if ($http_user_agent ~ "Prerender") {
set $prerender 0;
}
if ($uri ~ "\.(js|css|xml|less|png|jpg|jpeg|gif|pdf|doc|txt|ico|rss|zip|mp3|rar|exe|wmv|doc|avi|ppt|mpg|mpeg|tif|wav|mov|psd|ai|xls|mp4|m4a|swf|dat|dmg|iso|flv|m4v|torrent|ttf|woff)") {
set $prerender 0;
}
#resolve using Google's DNS server to force DNS resolution and prevent caching of IPs
resolver 8.8.8.8;
if ($prerender = 1) {
#setting prerender as a variable forces DNS resolution since nginx caches IPs and doesnt play well with load balancing
set $prerender "service.prerender.io";
rewrite .* /$scheme://$host$request_uri? break;
proxy_pass http://$prerender;
}
}
location @normal {
}
}
我在@JuniorCompressor
的帮助下使用工作配置更新了问题答案 0 :(得分:2)
您可以尝试:
server {
...
location @prerender {
recursive_error_pages on;
error_page 404 = @normal;
...
if ($prerender = 0) {
return 404;
}
...
}
location @normal {
}
}
您让@prerender
抛出未找到的内容,然后@normal
可以正常处理请求。
答案 1 :(得分:0)
server {
listen 80;
server_name localhost;
root /home/administrator/public_html;
index index.html;
location / {
try_files $uri @prerender;
}
location @prerender {
error_page 404 = @normal;
#proxy_set_header X-Prerender-Token TOKEN;
set $prerender 0;
if ($http_user_agent ~* "baiduspider|twitterbot|facebookexternalhit|rogerbot|linkedinbot|embedly|quora link preview|showyoubot|outbrain|pinterest|slackbot|vkShare|W3C_Validator") {
set $prerender 1;
}
if ($args ~ "_escaped_fragment_") {
set $prerender 1;
}
if ($http_user_agent ~ "Prerender") {
set $prerender 0;
}
if ($uri ~ "\.(js|css|xml|less|png|jpg|jpeg|gif|pdf|doc|txt|ico|rss|zip|mp3|rar|exe|wmv|doc|avi|ppt|mpg|mpeg|tif|wav|mov|psd|ai|xls|mp4|m4a|swf|dat|dmg|iso|flv|m4v|torrent|ttf|woff)") {
set $prerender 0;
}
#resolve using Google's DNS server to force DNS resolution and prevent caching of IPs
resolver 8.8.8.8;
if ($prerender = 1) {
#setting prerender as a variable forces DNS resolution since nginx caches IPs and doesnt play well with load balancing
set $prerender "service.prerender.io";
rewrite .* /$scheme://$host$request_uri? break;
proxy_pass http://$prerender;
}
}
location @normal {
}
}
Tnx到@JuniorCompressor