mikeg_de

Cross-Origin-Resource-Sharing CORS for Nginx

Nov 16th, 2014
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. # Thanks to: http://jesus.perezpaz.es/2014/02/configure-subdomain-as-cdn-in-nginx-wordpress-w3-total-cache-configurations/
  2. # Find full tutorial at: http://mikeg.de/project/cloud-nginx-server-wordpress/
  3. # Keep images and CSS and other static files around in browser cache for as long as possible,
  4. location ~* .(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf|html|txt|htm)$ {
  5. expires max;
  6. log_not_found off;
  7. access_log off;
  8. add_header Cache-Control public;
  9.  
  10. # don't send cookies
  11. fastcgi_hide_header Set-Cookie;
  12.  
  13. # CORS config
  14. set $cors "true";
  15.  
  16. # Determine the HTTP request method used
  17. if ($request_method = 'OPTIONS') {
  18. set $cors "${cors}options";
  19. }
  20. if ($request_method = 'GET') {
  21. set $cors "${cors}get";
  22. }
  23. if ($request_method = 'POST') {
  24. set $cors "${cors}post";
  25. }
  26. if ($cors = "true") {
  27. # Catch all incase there's a request method we're not dealing with properly
  28. add_header 'Access-Control-Allow-Origin' '*';
  29. }
  30.  
  31. if ($cors = "trueget") {
  32. add_header 'Access-Control-Allow-Origin' '*';
  33. add_header 'Access-Control-Allow-Credentials' 'true';
  34. add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
  35. add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
  36. }
  37.  
  38. if ($cors = "trueoptions") {
  39. add_header 'Access-Control-Allow-Origin' '*';
  40.  
  41. # Om nom nom cookies
  42. add_header 'Access-Control-Allow-Credentials' 'true';
  43. add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
  44.  
  45. # Custom headers and headers various browsers *should* be OK with but aren't
  46. add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
  47.  
  48. # Tell client that this pre-flight info is valid for 20 days
  49. add_header 'Access-Control-Max-Age' 1728000;
  50. add_header 'Content-Type' 'text/plain charset=UTF-8';
  51. add_header 'Content-Length' 0;
  52. return 204;
  53. }
  54.  
  55. if ($cors = "truepost") {
  56. add_header 'Access-Control-Allow-Origin' '*';
  57. add_header 'Access-Control-Allow-Credentials' 'true';
  58. add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
  59. add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
  60.  
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment