Nginx 配置相关--Gzip压缩、CORS

2020-07-27 17:24:20 浏览数 (1)

Gzip压缩

效果还是很明显的

代码语言:javascript复制
# 直接配置到 nginx server下面即可
# 效果还是挺明显的,之前大概2m的js文件,现在只要600kb左右
# use gzip
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
#gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript application/javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
gzip_vary off;
gzip_disable "MSIE [1-6].";

##CORS

Spring MVC 4.2 开始支持CROS配置

代码语言:javascript复制
# 将 <!--IP--> 替换为IP即可,域名....
# 也可以直接将所有请求都返回 Access-Control-Allow-* , 但是并不推荐
if ($http_origin ~* (<!--IP-->)) {
        set $cors "true";
}

if ($cors = "true") {
        add_header 'Access-Control-Allow-Origin' "http://xxxxxx.xxx";
        #add_header 'Access-Control-Allow-Credentials' "true";
        add_header 'Access-Control-Max-Age' 86400;
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, DELETE';
        add_header 'Access-Control-Allow-Headers' 'reqid, nid, host, x-real-ip, x-forwarded-ip, event-type, event-id, accept, content-type';
        add_header 'Content-Length' 0;
        add_header 'Content-Type' 'text/plain, charset=utf-8';
        return 204;
}

0 人点赞