我希望nginx返回错误403,如果用户代理是MSIE 6,并显示自定义错误消息。我用了这个代码,一开始一切都很顺利。然后,它只是返回错误,没有消息!不知道为什么……下面是代码(我试图用'代替',有纯文本没有'或',仍然没有运气):
location ~ .php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+.php)(/.+)$;
include /etc/nginx/fastcgi.conf;
fastcgi_pass unix:/var/run/php5-fpm.sock;
if ($http_user_agent ~ "MSIE 6" ) {
return 403 "Browser not supported. Please update or change to another one.";
}
}
编辑:忘了说它在php块中,因为我想阻止MSIE 6只针对php请求。
实际上,您的配置应该可以工作。您可以使用curl:
检查它。# curl -i -H "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1" http://localhost/i.php
HTTP/1.1 403 Forbidden
Server: nginx/1.3.6
Date: Wed, 26 Dec 2012 10:05:34 GMT
Content-Type: application/octet-stream
Content-Length: 62
Connection: keep-alive
Browser not supported. Please update or change to another one.
访问日志也值得检查(默认log_format包含$http_user_agent变量)。
顺便说一下,/etc/nginx/fastcgi.conf里有什么?
另一件事是,如果您希望使用真正的MSIE 6浏览器的人看到您的消息,您最好这样做:
location ~ .php$ {
...
if ($http_user_agent ~ "MSIE 6" ) {
return 403;
}
error_page 403 /old_browser.html;
}
和创建old_browser.html与您的消息。请注意,这个文件应该大于512字节,以确保MSIE将显示它的内容,而不是一些标准的IE 403消息。像http://browsershots.org这样的工具非常适合调试这种情况。:)