Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add CORS (cross origin policy) to all domains in NGINX?

I have created a folder that will be used for serving static files (CSS, images, fonts and JS etc) I will eventually CNAME the folder into a subdomain for usage on a CDN to work with my Magento 2 setup.

I want to allow ALL domains ALL access via CORS - Cross Origin Policy and I want to cache the data too. This is what I have. (I am not asking for security suggestions or tips on JSONP issues - I want global access to the file directory please)

location /cdn-directory/ {

    location ~* \.(ico|jpg|jpeg|png|gif|svg|js|css|swf|eot|ttf|otf|woff|woff2|zip|gz|gzip|bz2|csv|xml)$ {
        add_header Cache-Control "public";
        add_header X-Frame-Options "ALLOW-FROM *";
        expires +1y;
    }

}

According to documentation it says X-Frame-Options supports ALLOW-FROM uri but cannot see examples of using * (all domains) or adding certain multiple domains in this ALLOW-FROM. I need to allow all domains access to my static files folder.

like image 348
TheBlackBenzKid Avatar asked Feb 03 '16 10:02

TheBlackBenzKid


People also ask

How do I enable CORS policy in NGINX?

To enable CORS on NGINX, you need to use the add_header directive and add it to the appropriate NGINX configuration file. to allow access from any domain.

How do I enable CORS for specific domains?

To initiate a cross-origin request, a browser sends the request with an Origin: <domain> HTTP header, where <domain> is the domain that served the page. In response, the server sends Access-Control-Allow-Origin: <domain> , where <domain> is either a list of specific domains or a wildcard to allow all domains.

What is Access-Control allow Origin NGINX?

As you can tell by Access-Control-Allow-Origin * – this is wide open configuration, meaning any client will be able to access the resource. You can list specific hostnames that are allowed to access the server: add_header "Access-Control-Allow-Origin" "http://test.com, https://example.com"

How do I add CORS to web config?

Add CORS support to ASP.NET Web API Now webpages hosted on 'https://localhost:44310' can make AJAX requests to your controller/action. You can also define CORS globally by passing the attribute to EnableCors : var cors = new EnableCorsAttribute("https://localhost:44310", "*", "*"); config.


1 Answers

I didn't try it i nginx, but allowing the origin of current request works in tomcat:

add_header X-Frame-Options "ALLOW-FROM $http_origin";
like image 189
Serge Seredenko Avatar answered Sep 22 '22 02:09

Serge Seredenko