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

标签 internet-explorer nginx webserver 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。

最佳答案

实际上,您的配置应该可以工作。您可以使用 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非常适合调试此类情况。 :)

关于internet-explorer - Nginx:返回错误 403 并显示一条消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14034235/

相关文章:

linux - 在 nginx 中处理默认服务器和 ip 转发的最佳实践

macos - Docker 1.9 卷未在 Mac OS 上更新。图片nginx

C Web 服务器 - 如何获取 URI?

web-hosting - PHP 解释器和 PHP 处理程序有什么区别?

http - 发送 HTTP 请求告诉服务器只返回 header 而不返回正文?

html - 为什么第一个LI在FF和IE中向下移动

internet-explorer - 我们应该为我们的 Intranet 应用程序部署 Webkit 浏览器吗?

reactjs - 适用于 Internet Explorer 10 及更低版本的 React、WebPack 和 Babel 生成 SCRIPT1002 : Syntax error

html - 为什么子导航隐藏在IE下的 slider 广告后面?

azure - 无法访问 AKS 入口的公共(public) IP