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

前端之家收集整理的这篇文章主要介绍了返回400个代码时,nginx添加标题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

  1. server {
  2. listen *:80 ;
  3. server_name userchamp.com;
  4. access_log /var/log/Nginx/embertest.com.access.log;
  5. location / {
  6. root /var/www/embertest/public;
  7. try_files $uri $uri/ /index.PHP?$args ;
  8. index index.html index.htm index.PHP;
  9. }
  10. location ~ \.PHP${
  11. if ($request_method = 'OPTIONS') {
  12. add_header 'Access-Control-Allow-Origin' '*';
  13. add_header 'Access-Control-Allow-Credentials' 'true';
  14. add_header 'Access-Control-Allow-Methods' 'GET,POST,PUT,DELETE,OPTIONS';
  15. add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
  16. add_header 'Access-Control-Max-Age' 1728000;
  17. add_header 'Content-Type' 'text/plain charset=UTF-8';
  18. add_header 'Content-Length' 0;
  19. return 204;
  20. }
  21. if ($request_method = 'POST') {
  22. add_header 'Access-Control-Allow-Origin' '*';
  23. add_header 'Access-Control-Allow-Credentials' 'true';
  24. add_header 'Access-Control-Allow-Methods' 'GET,Content-Type';
  25. }
  26. if ($request_method = 'PUT') {
  27. add_header 'Access-Control-Allow-Origin' '*';
  28. add_header 'Access-Control-Allow-Credentials' 'true';
  29. add_header 'Access-Control-Allow-Methods' 'GET,DELETE OPTIONS';
  30. add_header 'Access-Control-Allow-Headers' 'DNT,Content-Type';
  31. }
  32. if ($request_method = 'GET') {
  33. add_header 'Access-Control-Allow-Origin' '*';
  34. add_header 'Access-Control-Allow-Credentials' 'true';
  35. add_header 'Access-Control-Allow-Methods' 'GET,Content-Type';
  36. }
  37. if ($request_method = 'DELETE') {
  38. add_header 'Access-Control-Allow-Origin' '*';
  39. add_header 'Access-Control-Allow-Credentials' 'true';
  40. add_header 'Access-Control-Allow-Methods' 'GET,Content-Type';
  41. }
  42. root /var/www/embertest/public;
  43. try_files $uri $uri/ /index.PHP?$args ;
  44. index index.html index.htm index.PHP;
  45. fastcgi_index index.PHP;
  46. fastcgi_param PATH_INFO $fastcgi_path_info;
  47. fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
  48. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  49. fastcgi_param APP_ENV dev;
  50. fastcgi_param APP_DBG true;
  51. fastcgi_pass unix:/var/run/PHP5-fpm.sock;
  52. fastcgi_split_path_info ^(.+\.PHP)(/.+)$;
  53. include fastcgi_params;
  54. }
  55. }
最佳答案
根据ngx_header_moduleNginx官方文档,当响应代码为400时,add_header无法正常工作

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

另一种方式,你可以试试HttpHeadersMoreModule,这是更强大的.

猜你在找的Nginx相关文章