nginx条件414 error_page

时间:2016-01-12 15:27:08

标签: nginx

如果来自用户的请求的URI超过服务器允许的最大值,我的目标是创建不同的414错误页面。我在我的应用程序中设置了3个这样的414错误页面,格式如下; 414.html414.json414.xml

我希望默认显示html页面,但如果正在通过json或xml发出请求,我想为指定格式提供相关的414页面。

以下是我到目前为止对我的nginx配置文件所做的补充。

server {
  ...

  map $http_accept $my_file_type {
    default              "html";
    "~*application/json" "json";
    "~*application/xml"  "xml";
  }

  error_page 414 /414.$my_file_type;
  location = /414.$my_file_type {
    log_format my_format '$http_accept -- $my_file_type -- $request_filename';
    access_log /var/log/nginx/access.log my_format;

    root /srv/www/myapp/current/public/;
  }
}

到目前为止,通过将默认值从html更改为json再到xml,我可以在启动请求时获取每个静态页面在414错误中,我已检查了access.log$http_accept值始终显示为-,而$request_filename始终显示为{{1}而不是我在请求中传递的实际文件名。

我还可以补充说,所提出的请求确实有一个' Accept:application / json'从我的curl语句传递的标题,以测试适当的响应,但它仍然在我的access.log中显示为/srv/www/myapp/current/public/414.html,而-仍然是414页,而不是来自我的请求的实际文件。

非常感谢任何建议或建议。

0 个答案:

没有答案
相关问题