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

时间:2022-07-22
本文章向大家介绍Nginx 配置相关--Gzip压缩、CORS,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Gzip压缩

效果还是很明显的

# 直接配置到 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配置

# 将 <!--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;
}