簡體   English   中英

Nginx:返回錯誤403並顯示一條消息

[英]Nginx: return error 403 and display a message

如果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塊中,因為我想阻止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這樣的工具非常適合調試這種情況。 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM