【问题标题】:nginx add headers when returning 400 codesnginx在返回400码时添加headers
【发布时间】:2013-12-23 06:13:40
【问题描述】:

我正在开发一个带有 laravel 后端的 ember.js 应用程序。如果出现问题,我正在尝试使用 php 返回 http 错误代码。我注意到,当发出 PUT 请求并返回 400 状态代码时,我的 CORS 标头会被我的 conf 文件忽略,这会破坏我的 ember 前端。我不知道为什么 PUT/400 代码组合会使 nginx 忽略我的配置文件。任何帮助将非常感激。

 server {
  listen                *:80 ;

  server_name           userchamp.com;
  access_log            /var/log/nginx/embertest.com.access.log;

  location / {

    root  /var/www/embertest/public;
    try_files  $uri  $uri/  /index.php?$args ;
    index  index.html index.htm index.php;

  }

  location ~ \.php$ {

        if ($request_method = 'OPTIONS') {

        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
        add_header 'Access-Control-Max-Age' 1728000;
        add_header 'Content-Type' 'text/plain charset=UTF-8';
        add_header 'Content-Length' 0;

        return 204;
     }

     if ($request_method = 'POST') {

        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';

     }

     if ($request_method = 'PUT') {

        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
     }
     if ($request_method = 'GET') {

        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';

     }

     if ($request_method = 'DELETE') {

        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';

     }
    root  /var/www/embertest/public;
    try_files  $uri  $uri/  /index.php?$args ;
    index  index.html index.htm index.php;
    fastcgi_index index.php;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    fastcgi_param  PATH_TRANSLATED $document_root$fastcgi_path_info;
    fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param    APP_ENV dev;
    fastcgi_param     APP_DBG true;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    include fastcgi_params;
  }
}

【问题讨论】:

    标签: nginx


    【解决方案1】:

    对于 nginx >= 1.7.5

    将“always”附加到标题定义:

    add_header 'Access-Control-Allow-Origin' '*' always;
    

    适用于 nginx

    根据ngx_header_module的nginx官方文档,add_header在响应码为400时无法工作

    syntax:     add_header name value;
    default:    —
    context:    http, server, location, if in location
    
    
    Adds the specified field to a response header provided that the response code equals 
    200, 201, 204, 206, 301, 302, 303, 304, or 307. A value can contain variables.
    

    另外一种方式,你可以试试HttpHeadersMoreModule,功能更强大。

    【讨论】:

    • 非常感谢 - 我今天遇到了这个问题。
    • 如果您使用 nginx >= 1.7.5,您可以添加名为“always”的第三个参数,即使有错误响应代码也可以使用。
    • 您应该将“always”添加到您想要的每个标题中,并将 response.stuck 放在此处...
    • “永远”是解决我问题的关键。谢谢!
    • @simon,您应该将其发布为答案,以便获得尽可能多的支持。
    猜你喜欢
    • 2022-01-03
    • 1970-01-01
    • 2019-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-08
    • 1970-01-01
    相关资源
    最近更新 更多