nginx 404状态码转200状态码后遇到的坑解决方法
场景:
应项目需要两个网站(新站和老站并存),现将404的页面用以下方式(nginx配置)代理到老网站,运行完美,两站并存!
error_page 404 =200 @proxyOldSite;
location @proxyOldSite {
rewrite ^ $request_uri break;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://192.168.31.111:80;
}
location ~ \.php$ {
fastcgi_intercept_errors on;
include snippets/fastcgi-php.conf;
fastcgi_pass php_upstream;
#fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
但是,运营有需求,想更新快照,让某些链接返回 404,
那问题来了, 我们是将404 转向了 200 状态码?
怎么办呢?我尝试了 return 404; 百度、谷歌都没有找到解决方法,
只能自己想自己试了,以下是解决方法,附上,防止以后再采坑!
location = /xxx.html {
rewrite ^ /404.php last;
}
location = /404.php {
fastcgi_intercept_errors off;
include snippets/fastcgi-php.conf;
fastcgi_pass php_upstream;
#fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
404.php
<?php
header("HTTP/1.1 404 Not Found");
header("Status: 404 Not Found");
exit;