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

时间:2012-12-25 21:50:09

标签: internet-explorer nginx webserver user-agent

如果user-agent是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.";
    }
}
编辑:忘了说它在php块中,因为我想仅针对PHP请求阻止MSIE 6。

1 个答案:

答案 0 :(得分:2)

实际上,您的配置应该有效。您可以使用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这样的工具非常适合调试这种情况。 :)