Skip to content

Instantly share code, notes, and snippets.

@mozillazg
Forked from m4ttbrock/nginx-nodejs-cors
Created January 15, 2016 11:20
Show Gist options
  • Save mozillazg/eb44a413ff8e0e0b4f2f to your computer and use it in GitHub Desktop.
Save mozillazg/eb44a413ff8e0e0b4f2f to your computer and use it in GitHub Desktop.

Revisions

  1. mozillazg revised this gist Jan 15, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion nginx-nodejs-cors
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@ server {

    location / {

    if ($http_origin ~* (https?://.*\.example\.com(:[0-9]+)?)) {
    if ($http_origin ~* "https?://.*\.example\.com(:[0-9]+)?") {
    set $cors "true";
    }

  2. @m4ttbrock m4ttbrock revised this gist Nov 6, 2013. 1 changed file with 3 additions and 20 deletions.
    23 changes: 3 additions & 20 deletions nginx-nodejs-cors
    Original file line number Diff line number Diff line change
    @@ -1,22 +1,3 @@
    server {
    listen 80;
    server_name example.com www.example.com;
    root /var/example;

    #ssl on;
    #ssl_certificate /etc/nginx/ssl/wildcard.crt;
    #ssl_certificate_key /etc/nginx/ssl/wildcard.key;

    access_log /var/log/nginx/example.log;
    error_log /var/log/nginx/example.error.log warn;

    location / {
    add_header 'Access-Control-Allow-Origin' "http://example.com";
    add_header 'Access-Control-Allow-Credentials' 'true';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
    }
    }

    server {
    listen 80;

    @@ -25,7 +6,9 @@ server {

    location / {

    set $cors "true";
    if ($http_origin ~* (https?://.*\.example\.com(:[0-9]+)?)) {
    set $cors "true";
    }

    if ($request_method = 'OPTIONS') {
    set $cors "${cors}options";
  3. @m4ttbrock m4ttbrock created this gist Nov 6, 2013.
    71 changes: 71 additions & 0 deletions nginx-nodejs-cors
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,71 @@
    server {
    listen 80;
    server_name example.com www.example.com;
    root /var/example;

    #ssl on;
    #ssl_certificate /etc/nginx/ssl/wildcard.crt;
    #ssl_certificate_key /etc/nginx/ssl/wildcard.key;

    access_log /var/log/nginx/example.log;
    error_log /var/log/nginx/example.error.log warn;

    location / {
    add_header 'Access-Control-Allow-Origin' "http://example.com";
    add_header 'Access-Control-Allow-Credentials' 'true';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
    }
    }

    server {
    listen 80;

    server_name subdomain.example.com;
    access_log /var/log/nginx/example.access.log;

    location / {

    set $cors "true";

    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 = "trueget") {
    add_header 'Access-Control-Allow-Origin' "$http_origin";
    add_header 'Access-Control-Allow-Credentials' 'true';
    }

    if ($cors = "truepost") {
    add_header 'Access-Control-Allow-Origin' "$http_origin";
    add_header 'Access-Control-Allow-Credentials' 'true';
    }

    if ($cors = "trueoptions") {
    add_header 'Access-Control-Allow-Origin' "$http_origin";
    add_header 'Access-Control-Allow-Credentials' 'true';
    add_header 'Access-Control-Max-Age' 1728000;
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
    add_header 'Access-Control-Allow-Headers' 'Authorization,Content-Type,Accept,Origin,User-Agent,DNT,Cache-Control,X-Mx-ReqToken,Keep-Alive,X-Requested-With,If-Modified-Since';
    add_header 'Content-Length' 0;
    add_header 'Content-Type' 'text/plain charset=UTF-8';
    return 204;
    }

    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-NginX-Proxy true;
    proxy_pass_header Set-Cookie;
    proxy_cache_bypass $cookie_nocache $arg_nocache $arg_comment;
    proxy_pass http://127.0.0.1:8675;
    proxy_redirect off;
    }
    }