0

I have this working Nginx configuration:

    location /static/ {

# Uncomment the following line in production mode
        expires max;

# Remove signature of the static files that is used to overcome the browser cache
        location ~ ^/static/version {

            rewrite ^/static/(version\d*/)?(.*)$ /static/$2 last;
        }

        location ~* \.(ico|jpg|jpeg|png|gif|svg|js|css|swf|eot|ttf|otf|woff|woff2|json)$ {

            add_header Cache-Control "public";
            add_header Access-Control-Allow-Origin "https://store.my.domain";
            if ($request_method = 'OPTIONS') {
                add_header Access-Control-Allow-Origin "https://store.my.domain";
                add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
                add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
                add_header 'Access-Control-Max-Age' 1728000;
                add_header 'Content-Type' 'text/plain; charset=utf-8';
                add_header 'Content-Length' 0;
                return 204;
            }

            expires +1y;

            if (!-f $request_filename) {
                rewrite ^/static/(version\d*/)?(.*)$ /static.php?resource=$2 last;
            }
        }
        location ~* \.(zip|gz|gzip|bz2|csv|xml)$ {

            add_header Cache-Control "no-store";
            add_header Access-Control-Allow-Origin "https://store.my.domain";
            if ($request_method = 'OPTIONS') {
                add_header Access-Control-Allow-Origin "https://store.my.domain";
                add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
                add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
                add_header 'Access-Control-Max-Age' 1728000;
                add_header 'Content-Type' 'text/plain; charset=utf-8';
                add_header 'Content-Length' 0;
                return 204;
            }

            expires    off;

            if (!-f $request_filename) {
                rewrite ^/static/(version\d*/)?(.*)$ /static.php?resource=$2 last;
            }
        }
        if (!-f $request_filename) {
            rewrite ^/static/(version\d*/)?(.*)$ /static.php?resource=$2 last;
        }

        add_header Access-Control-Allow-Origin "https://store.my.domain";
        if ($request_method = 'OPTIONS') {
            add_header Access-Control-Allow-Origin "https://store.my.domain";
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
            add_header 'Access-Control-Max-Age' 1728000;
            add_header 'Content-Type' 'text/plain; charset=utf-8';
            add_header 'Content-Length' 0;
            return 204;
        }

    }

As can be seen there is a repetitive code concerning CORS. My question, how to define it only once and make it work for all sub locations?

2
  • 1
    serverfault.com/a/584596/211028
    – Alexey Ten
    Nov 21, 2019 at 8:10
  • Thanks @AlexeyTen, what I have learned meanwhile is that add_header directive can be inherited as you say but it does so as long as the child block does not define additional add_headers, if does so, the parent ones get ignored.
    – W.M.
    Nov 21, 2019 at 8:44

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.