Nginx:返回错误403并显示一条消息

Mul*_*gno 6 webserver internet-explorer user-agent nginx

如果用户代理为MSIE 6,我希望nginx返回错误403并显示自定义错误消息。我使用了这段代码,一切在开始的第一分钟就起作用了。然后它只是返回错误而没有消息!不知道为什么...这是代码(我试图用'代替“,使纯文本不包含'或”,但仍然没有运气):

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.";
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:忘记说它在php块中,因为我只想为PHP请求阻止MSIE 6。

And*_*lov 5

事实上,你的配置应该可以工作。您可以使用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.
Run Code Online (Sandbox Code Playgroud)

访问日志也值得检查(默认情况下 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;
}
Run Code Online (Sandbox Code Playgroud)

并使用您的消息创建 old_browser.html 。请注意,此文件应大于 512 字节,以确保 MSIE 将显示其内容而不是某些标准 IE 403 消息。像http://browsershots.org这样的工具非常适合调试此类情况。:)