加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > Nginx > 正文

返回400个代码时,nginx添加标题

发布时间:2020-12-13 21:07:18 所属栏目:Nginx 来源:网络整理
导读:我正在开发一个带有laravel后端的ember.js应用程序.我试图返回http错误代码与php如果有什么问题.我注意到,当发出PUT请求并返回400状态代码时,我的CORS标题被我的conf文件忽略,这个文件破坏了我的ember前端.我不知道为什么PUT / 400代码组合使nginx忽略我的co

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

 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,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,DELETE OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,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,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,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;
  }
}
最佳答案
根据ngx_header_module的nginx官方文档,当响应代码为400时,add_header无法正常工作

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,这是更强大的.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读