Назад | Перейти на главную страницу

Как определить настройки CORS на корневом уровне (базовом уровне) в файле конфигурации Nginx

У меня это работает Nginx конфигурация:

    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;
        }

    }

Как можно видеть, есть повторяющийся код, касающийся CORS. Мой вопрос, как определить его только один раз и заставить работать для всех подобластей?