Try ERPNext Buy Support Partners Foundation

[Tutorial Nginx] Enabling CORS for multiple Domains

hi
i was trying to add more than one domain in nginx , it works with one domain but multiple not
one domain :
add_header 'Access-Control-Allow-Origin' 'ww.xyz.com';
multiple format didn’ works for me … like
add_header 'Access-Control-Allow-Origin' 'ww.xyz.com, sub.xyz.com ';
add_header 'Access-Control-Allow-Origin' 'ww.xyz.com sub.xyz.com ';

the solution was :
Inside frappe bench folder write :
nano config/nginx.conf

write this code before the server block

  map $http_origin $origin_allowed {
     default 0;
     http://localhost:3000 1;
     https://sub.yourdomain.com 1;
 `#  ... add more allowed origins here`
    }

map $origin_allowed $origin {
  default "";
  1 $http_origin;
}

and inside the server block you want to open the cors in add this line

            if ($request_method = 'OPTIONS') {
                    add_header 'Access-Control-Allow-Origin' $origin;

as you can see $origin is set by the first code block . that way you can add as many sub domain and domains to call your server .

2 Likes

try with https://

add_header Access-Control-Allow-Origin "https://ww.xyz.com https://sub.xyz.com"

Can this feature be added to bench command?

bench setup nginx

https://github.com/frappe/bench/blob/develop/bench/config/templates/nginx.conf