如何在 Nginx 配置文件中的根级别(基础级别)上定义 CORS 设置

如何在 Nginx 配置文件中的根级别(基础级别)上定义 CORS 设置

我有这个工作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。我的问题是,如何仅定义一次并使其适用于所有子位置?

相关内容