我有以下nginx服务器configuration:
server { listen 80; server_name example.com root /server/root; index index.php; error_page 404 = /index.php; location ~ \.php$ { try_files $uri =404; proxy_pass http://127.0.0.1:8080; proxy_redirect off; 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_set_header X-Request-URI $request_uri; } }
我想要的行为是,当nginx遇到一个不存在的文件的请求,而是通过一个404页面显示index.php页面。 问题是似乎apache(这是什么被代理回来)仍然试图解决原来的请求,当它得到请求。 如果我去http://example.com/blahblah ,我找回错误:
The requested URL /blahblah was not found on this server.
这是一个Apache错误。 我怎样才能让index.php显示为404页面,就像它是一个静态文件一样?
你使用的是哪个版本的nginx? 这个问题在1.1.12中得到解决: http : //nginx.org/en/CHANGES
编辑:如果你不能更新,你可以用你的当前error_page和try_filesreplace:
location / { try_files $uri $uri /index.php; } location ~ \.php$ { # Leave the =404 at the end so we don't 500 when /index.php doesn't exist try_files $uri /index.php =404; proxy_pass http://127.0.0.1:8080; proxy_redirect off; 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_set_header X-Request-URI $request_uri; }
nginx将不得不拦截Apache的回应,承认404版本,并返回自己的。
如果nginx没有办法做到这一点,那么也许你可以configurationApache不返回任何东西 – 从而触发nginx自己的404状态?
如果你坚持使用Apache,你必须设置Apache重写规则,将404错误发送给你的应用程序,而不是nginx的configuration。