I was having some issues getting SVGs to load on my website if you were viewing website.com instead of www.website.com. The website is on an nginx server, so I added this, a
Here is a solution that uses map
.
This setup allows you to make requests to any subdomain and any port on my-domain.com.
map $http_origin $allow_origin {
~^https?://(.*\.)?my-domain.com(:\d+)?$ $http_origin;
# NGINX won't set empty string headers, so if no match, header is unset.
default "";
}
server {
listen 80 default_server;
server_name _;
add_header 'Access-Control-Allow-Origin' $allow_origin;
# ...
}
http://nginx.org/en/docs/http/ngx_http_map_module.html
There are some unexpected things that occur when using if
inside location blocks in NGINX. It's not recommended. https://www.nginx.com/resources/wiki/start/topics/depth/ifisevil/ and https://agentzh.blogspot.com/2011/03/how-nginx-location-if-works.html