这是我的nginxconfiguration:
server { # Running port listen 80; error_page 404 /404; # Settings to by-pass for static files location ^~ / { # Example: # root /full/path/to/application/static/file/dir; root /var/www; include /etc/nginx/mime.types; try_files $uri $uri/ @htmlext; } location ~ \.html$ { try_files $uri = 404; } location @htmlext { rewrite ^(.*)$ $1.html last; } }
目的是在发生404错误时显示页面/404 。 但是,当我访问不存在的页面时,我现在正在收到500 Internal Server Error 。 为什么?
我认为你的主要问题在这里: rewrite ^(.*)$ $1.html last; 并使用last标志。
这是什么文件说 :
如果这些指令[重写]放在位置内,
last标志应该被breakreplace,否则nginx会做10个周期并返回500错误
这是我们可以在日志中看到的。 在这里,我要求fake.html不存在:
[debug] 3480#0: *1 http script var: "/fake.html.html.html.html.html.html" [debug] 3480#0: *1 trying to use file: "/fake.html.html.html.html.html.html" "/var/www/fake.html.html.html.html.html.html" [debug] 3480#0: *1 http script var: "/fake.html.html.html.html.html.html" [debug] 3480#0: *1 trying to use dir: "/fake.html.html.html.html.html.html" "/var/www/fake.html.html.html.html.html.html" [debug] 3480#0: *1 trying to use file: "@htmlext" "/var/www@htmlext" [error] 3480#0: *1 rewrite or internal redirection cycle while redirect to named location "@htmlext".... [debug] 3480#0: *1 http finalize request: 500, "/fake.html.html.html.html.html.html?" a:1, c:7 [debug] 3480#0: *1 http special response: 500, "/fake.html.html.html.html.html.html?"
有趣! ;)
所以,用下面的代码replace你的重写规则: rewrite ^(.*)$ $1.html break;
或者,如果try_files指令中不存在任何文件,则可以返回404 。
try_files $uri $uri/ @htmlext =404;
但是,在这一点上,您的自定义404页面不应该工作。
这是我的build议:
server { # Running port listen 80; error_page 404 /404.html; location ~^/404.html { root /var/www; internal; } # Your other stuff Here ...... }
尝试replace这个:
try_files $uri = 404;
有:
try_files $uri =404;
一个正确的方法来定义404页面:
error_page 404 /404.html; location /404.html { internal; }