nginx在返回400个代码时添加头

nginx在返回400个代码时添加头,nginx,Nginx,我正在开发一个带有laravel后端的ember.js应用程序。如果出现问题,我尝试用php返回http错误代码。我注意到,当发出PUT请求并返回400状态代码时,我的CORS头被我的conf文件忽略,这破坏了我的余烬前端。我不知道为什么PUT/400代码组合会让nginx忽略我的配置。任何帮助都将不胜感激 server { listen *:80 ; server_name userchamp.com; access_log

我正在开发一个带有laravel后端的ember.js应用程序。如果出现问题,我尝试用php返回http错误代码。我注意到,当发出PUT请求并返回400状态代码时,我的CORS头被我的conf文件忽略,这破坏了我的余烬前端。我不知道为什么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.7.5

将“始终”附加到标题定义:

add_header 'Access-Control-Allow-Origin' '*' always;
对于nginx<1.7.5

根据的nginx官方文件,当响应代码为400时,
add\u头
无法工作

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.

另一方面,您可以尝试更强大的

非常感谢-我今天遇到了这个问题。如果你使用nginx>=1.7.5,你可以添加第三个名为“always”的参数,它甚至可以与错误响应代码一起工作。你应该在每个需要响应的标题中添加“always”。卡在这里…always是解决我问题的关键。谢谢@西蒙,你应该把这个作为答案贴出来,这样才能得到更多的选票。