ng配置add_header 允许跨域和不缓存设置

允许跨域

location / {
            root   html;
            index  index.html index.htm;
            try_files $uri $uri/ /index.html;
            # 设置允许跨域
            add_header Access-Control-Allow-Origin *;
        }

设置不缓存

location / {
            root   html;
            index  index.html index.htm;
            try_files $uri $uri/ /index.html;
            add_header Access-Control-Allow-Origin *;
            if ($request_filename ~* .*\.(?:htm|html|text)$)  {
               # 设置html资源不缓存
               add_header Cache-Control "private, no-store, no-cache, must-revalidate, proxy-revalidate";
            }
        }

设置不缓存后,发现允许跨域失效

查阅资源发现,if 判断下设置add_header,会重置add_header,造成上面设置失效,需要重新设置下允许跨域。最终设置如下:

location / {
            root   html;
            index  index.html index.htm;
            try_files $uri $uri/ /index.html;
            # 设置允许跨域
            add_header Access-Control-Allow-Origin *;
            if ($request_filename ~* .*\.(?:htm|html|text)$)  {
               # 设置html资源不缓存
               add_header Cache-Control "private, no-store, no-cache, must-revalidate, proxy-revalidate";
               # 重新设置允许跨域
               add_header Access-Control-Allow-Origin *;
            }
        }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容