Skip to content

Instantly share code, notes, and snippets.

@kaelzhang
Forked from alexjs/cors-nginx.conf
Last active July 6, 2017 06:32

Revisions

  1. kaelzhang revised this gist Jul 6, 2017. 1 changed file with 0 additions and 3 deletions.
    3 changes: 0 additions & 3 deletions cors-nginx.conf
    Original file line number Diff line number Diff line change
    @@ -77,7 +77,4 @@ location / {
    add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';

    }



    }
  2. @alexjs alexjs revised this gist Nov 28, 2012. 1 changed file with 49 additions and 18 deletions.
    67 changes: 49 additions & 18 deletions cors-nginx.conf
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,52 @@

    #
    # Slightly tighter CORS config for nginx
    #
    # A modification of https://gist.github.com/1064640/ to include a white-list of URLs
    #
    # Wide-open CORS config for nginx
    # Despite the W3C guidance suggesting that a list of origins can be passed as part of
    # Access-Control-Allow-Origin headers, several browsers (well, at least Firefox)
    # don't seem to play nicely with this.
    #
    # To avoid the use of 'Access-Control-Allow-Origin: *', use a simple-ish whitelisting
    # method to control access instead.
    #
    # NB: This relies on the use of the 'Origin' HTTP Header.

    location / {

    if ($request_method = 'OPTIONS') {
    if ($http_origin ~* (whitelist\.address\.one|whitelist\.address\.two)) {
    set $cors "true";
    }

    # Nginx doesn't support nested If statements. This is where things get slightly nasty.
    # Determine the HTTP request method used

    if ($request_method = 'OPTIONS') {
    set $cors "${cors}options";
    }
    if ($request_method = 'GET') {
    set $cors "${cors}get";
    }
    if ($request_method = 'POST') {
    set $cors "${cors}post";
    }

    if ($cors = "true") {
    # Catch all incase there's a request method we're not dealing with properly
    add_header 'Access-Control-Allow-Origin' "$http_origin";
    }

    if ($cors = "trueget") {
    add_header 'Access-Control-Allow-Origin' "$http_origin";
    add_header 'Access-Control-Allow-Credentials' 'true';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, 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 ($cors = "trueoptions") {
    add_header 'Access-Control-Allow-Origin' "$http_origin";

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

    #
    # Om nom nom cookies
    #
    @@ -27,26 +67,17 @@ location / {
    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' '*';
    if ($cors = "truepost") {
    add_header 'Access-Control-Allow-Origin' "$http_origin";
    add_header 'Access-Control-Allow-Credentials' 'true';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, 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, 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';

    }

    }
    }
  3. @michiel michiel revised this gist Sep 13, 2012. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions cors-nginx.conf
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@ location / {

    if ($request_method = 'OPTIONS') {

    add_header 'Access-Control-Allow-Origin' 'http://10.140.10.40';
    add_header 'Access-Control-Allow-Origin' '*';

    #
    # Om nom nom cookies
    @@ -28,12 +28,12 @@ location / {
    add_header 'Content-Type' 'text/plain charset=UTF-8';
    add_header 'Content-Length' 0;

    return 200;
    return 204;
    }

    if ($request_method = 'POST') {

    add_header 'Access-Control-Allow-Origin' 'http://10.140.10.40';
    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Credentials' 'true';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, 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';
    @@ -42,7 +42,7 @@ location / {

    if ($request_method = 'GET') {

    add_header 'Access-Control-Allow-Origin' 'http://10.140.10.40';
    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Credentials' 'true';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, 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';
  4. Michiel Kalkman revised this gist Jul 6, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion cors-nginx.conf
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@ location / {

    if ($request_method = 'OPTIONS') {

    add_header 'Access-Control-Allow-Origin' 'http://10.0.0.20/';
    add_header 'Access-Control-Allow-Origin' 'http://10.140.10.40';

    #
    # Om nom nom cookies
  5. Michiel Kalkman revised this gist Jul 6, 2011. 1 changed file with 38 additions and 24 deletions.
    62 changes: 38 additions & 24 deletions cors-nginx.conf
    Original file line number Diff line number Diff line change
    @@ -1,38 +1,52 @@
    #
    # Wide-open CORS config for nginx
    #
    location / {

    location / {
    if ($request_method = 'OPTIONS') {

    if ($request_method = 'OPTIONS') {
    add_header 'Access-Control-Allow-Origin' 'http://10.0.0.20/';

    #
    # Om nom nom cookies
    #

    add_header 'Access-Control-Allow-Origin' 'http://10.140.10.40';
    add_header 'Access-Control-Allow-Credentials' 'true';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, 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;
    add_header 'Access-Control-Allow-Credentials' 'true';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';

    #
    # Custom headers and headers various browsers *should* be OK with but aren't
    #

    return 200;
    }
    add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';

    #
    # Tell client that this pre-flight info is valid for 20 days
    #

    if ($request_method = 'POST') {
    add_header 'Access-Control-Max-Age' 1728000;
    add_header 'Content-Type' 'text/plain charset=UTF-8';
    add_header 'Content-Length' 0;

    add_header 'Access-Control-Allow-Origin' 'http://10.140.10.40';
    add_header 'Access-Control-Allow-Credentials' 'true';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, 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';
    return 200;
    }

    }
    if ($request_method = 'POST') {

    if ($request_method = 'GET') {
    add_header 'Access-Control-Allow-Origin' 'http://10.140.10.40';
    add_header 'Access-Control-Allow-Credentials' 'true';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, 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-Allow-Origin' 'http://10.140.10.40';
    add_header 'Access-Control-Allow-Credentials' 'true';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, 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' 'http://10.140.10.40';
    add_header 'Access-Control-Allow-Credentials' 'true';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, 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';

    }

    }
  6. Michiel Kalkman revised this gist Jul 6, 2011. 1 changed file with 25 additions and 26 deletions.
    51 changes: 25 additions & 26 deletions cors-nginx.conf
    Original file line number Diff line number Diff line change
    @@ -2,38 +2,37 @@
    # Wide-open CORS config for nginx
    #

    location / {
    location / {

    if ($request_method = 'OPTIONS') {
    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, OPTIONS';
    add_header 'Access-Control-Allow-Headers' 'DNT,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control';
    add_header 'Access-Control-Max-Age' 1728000;
    add_header 'Content-Type' 'text/plain charset=UTF-8';
    add_header 'Content-Length' 0;
    add_header 'Access-Control-Allow-Origin' 'http://10.140.10.40';
    add_header 'Access-Control-Allow-Credentials' 'true';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, 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 200;
    }
    return 200;
    }

    if ($request_method = 'POST') {
    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, OPTIONS';
    add_header 'Access-Control-Allow-Headers' 'DNT,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control';
    add_header 'Access-Control-Allow-Origin' 'http://10.140.10.40';
    add_header 'Access-Control-Allow-Credentials' 'true';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, 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';

    # proxy_pass http://somewhere;
    }
    }

    if ($request_method = 'GET') {
    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, OPTIONS';
    add_header 'Access-Control-Allow-Headers' 'DNT,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control';
    add_header 'Access-Control-Allow-Origin' 'http://10.140.10.40';
    add_header 'Access-Control-Allow-Credentials' 'true';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, 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';

    # proxy_pass http://somewhere;
    }
    }
    }

    }
  7. Michiel Kalkman revised this gist Jul 5, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion cors-nginx.conf
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,7 @@ location / {
    add_header 'Access-Control-Allow-Headers' 'DNT,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control';
    add_header 'Access-Control-Max-Age' 1728000;
    add_header 'Content-Type' 'text/plain charset=UTF-8';
    add_header 'Content-Length 0;
    add_header 'Content-Length' 0;

    return 200;
    }
  8. Michiel Kalkman revised this gist Jul 5, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion cors-nginx.conf
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    #
    # Wide-open CORS config for nxginx
    # Wide-open CORS config for nginx
    #

    location / {
  9. Michiel Kalkman created this gist Jul 5, 2011.
    39 changes: 39 additions & 0 deletions cors-nginx.conf
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    #
    # Wide-open CORS config for nxginx
    #

    location / {

    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, OPTIONS';
    add_header 'Access-Control-Allow-Headers' 'DNT,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control';
    add_header 'Access-Control-Max-Age' 1728000;
    add_header 'Content-Type' 'text/plain charset=UTF-8';
    add_header 'Content-Length 0;

    return 200;
    }

    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, OPTIONS';
    add_header 'Access-Control-Allow-Headers' 'DNT,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control';

    # proxy_pass http://somewhere;
    }

    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, OPTIONS';
    add_header 'Access-Control-Allow-Headers' 'DNT,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control';

    # proxy_pass http://somewhere;
    }
    }