Nginx静态文件缓存

来自ling
跳转至: 导航搜索

https://blog.csdn.net/zhangjunli/article/details/80436489

server {
    listen 80;
    # gzip config
    gzip on;
    gzip_min_length 1k;
    gzip_comp_level 9;
    gzip_types text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml;
    gzip_vary on;
    gzip_disable "MSIE [1-6]\.";
    charset utf-8;

#     proxy_connect_timeout 500;
#     #跟后端服务器连接的超时时间_发起握手等候响应超时时间
#     proxy_read_timeout 600;
#     #连接成功后_等候后端服务器响应的时间_其实已经进入后端的排队之中等候处理
#     proxy_send_timeout 500;
#     #后端服务器数据回传时间_就是在规定时间内后端服务器必须传完所有数据
#     proxy_buffer_size 128k;
#     #代理请求缓存区_这个缓存区间会保存用户的头信息以供Nginx进行规则处理_一般只要能保存下头信息即可
#     proxy_buffers 4 128k;
#     #同上 告诉Nginx保存单个用的几个Buffer最大用多大空间
#     proxy_busy_buffers_size 256k;
#     #如果系统很忙的时候可以申请更大的proxy_buffers 官方推荐*2
#     proxy_temp_file_write_size 128k;
#     #proxy缓存临时文件的大小
#     proxy_temp_path /usr/local/nginx/temp;
#     #用于指定本地目录来缓冲较大的代理请求
#     proxy_cache_path /usr/local/nginx/cache levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;
#     #设置web缓存区名为cache_one,内存缓存空间大小为12000M,自动清除超过15天没有被访问过的缓存数据,硬盘缓存空间大小200g

#     add_header Access-Control-Allow-Origin *;
#     add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
#     add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';


    location ~ runtimeapi {
        rewrite ^(.*)/runtimeapi/(.*)$ /runtimeapi/$2 break;
        proxy_pass http://css-gateway-api-service:30000;
        proxy_set_header   X-Forwarded-Proto $scheme;
        proxy_set_header   Host              $http_host;
        proxy_set_header   X-Real-IP         $remote_addr;
    }
    location ~ cssbasicdataapi {
        rewrite ^(.*)/cssbasicdataapi/(.*)$ /cssbasicdataapi/$2 break;
        proxy_pass http://css-gateway-api-service:30000;
        proxy_set_header   X-Forwarded-Proto $scheme;
        proxy_set_header   Host              $http_host;
        proxy_set_header   X-Real-IP         $remote_addr;
    }
    location ~ cssdispatchmgtapi {
        rewrite ^(.*)/cssdispatchmgtapi/(.*)$ /cssdispatchmgtapi/$2 break;
        proxy_pass http://css-gateway-api-service:30000;
        proxy_set_header   X-Forwarded-Proto $scheme;
        proxy_set_header   Host              $http_host;
        proxy_set_header   X-Real-IP         $remote_addr;
    }
    location ~ csssparepartsapi {
        rewrite ^(.*)/csssparepartsapi/(.*)$ /csssparepartsapi/$2 break;
        proxy_pass http://css-gateway-api-service:30000;
        proxy_set_header   X-Forwarded-Proto $scheme;
        proxy_set_header   Host              $http_host;
        proxy_set_header   X-Real-IP         $remote_addr;
    }
    location ~ cssworkorderapi {
        rewrite ^(.*)/cssworkorderapi/(.*)$ /cssworkorderapi/$2 break;
        proxy_pass http://css-gateway-api-service:30000;
        proxy_set_header   X-Forwarded-Proto $scheme;
        proxy_set_header   Host              $http_host;
        proxy_set_header   X-Real-IP         $remote_addr;
    }
    location ~ cssmobileplatformapi {
        rewrite ^(.*)/cssmobileplatformapi/(.*)$ /cssmobileplatformapi/$2 break;
        proxy_pass http://css-gateway-api-service:30000;
        proxy_set_header   X-Forwarded-Proto $scheme;
        proxy_set_header   Host              $http_host;
        proxy_set_header   X-Real-IP         $remote_addr;
    }
    location /runtime {
        proxy_pass http://css-runtime-ui-service;
        proxy_set_header   X-Forwarded-Proto $scheme;
        proxy_set_header   Host              $http_host;
        proxy_set_header   X-Real-IP         $remote_addr;

        if ($request_filename ~* .*\.(?:htm|html)$)
        {
            add_header Cache-Control "private, no-store, no-cache, must-revalidate, proxy-revalidate";
        }
        if ($request_filename ~* .*\.(?:js|css|jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm)$)
        {
            expires      7d;
        }
    }
    location /cssbasicdata {
        proxy_pass http://css-basicdata-ui-service;
        proxy_set_header   X-Forwarded-Proto $scheme;
        proxy_set_header   Host              $http_host;
        proxy_set_header   X-Real-IP         $remote_addr;
        if ($request_filename ~* .*\.(?:htm|html)$)
        {
            add_header Cache-Control "private, no-store, no-cache, must-revalidate, proxy-revalidate";
        }
        if ($request_filename ~* .*\.(?:js|css|jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm)$)
        {
            expires      7d;
        }
    }
    location /cssdispatchmgt {
        proxy_pass http://css-dispatchmgt-ui-service;
        proxy_set_header   X-Forwarded-Proto $scheme;
        proxy_set_header   Host              $http_host;
        proxy_set_header   X-Real-IP         $remote_addr;
        if ($request_filename ~* .*\.(?:htm|html)$)
        {
            add_header Cache-Control "private, no-store, no-cache, must-revalidate, proxy-revalidate";
        }
        if ($request_filename ~* .*\.(?:js|css|jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm)$)
        {
            expires      7d;
        }
    }
    location /cssspareparts {
        proxy_pass http://css-spareparts-ui-service;
        proxy_set_header   X-Forwarded-Proto $scheme;
        proxy_set_header   Host              $http_host;
        proxy_set_header   X-Real-IP         $remote_addr;
        if ($request_filename ~* .*\.(?:htm|html)$)
        {
            add_header Cache-Control "private, no-store, no-cache, must-revalidate, proxy-revalidate";
        }
        if ($request_filename ~* .*\.(?:js|css|jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm)$)
        {
            expires      7d;
        }
    }
    location /cssworkorder {
        proxy_pass http://css-workorder-ui-service;
        proxy_set_header   X-Forwarded-Proto $scheme;
        proxy_set_header   Host              $http_host;
        proxy_set_header   X-Real-IP         $remote_addr;
        if ($request_filename ~* .*\.(?:htm|html)$)
        {
            add_header Cache-Control "private, no-store, no-cache, must-revalidate, proxy-revalidate";
        }
        if ($request_filename ~* .*\.(?:js|css|jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm)$)
        {
            expires      7d;
        }
    }
    location / {
        proxy_pass http://css-portal-ui-service;
        proxy_set_header   X-Forwarded-Proto $scheme;
        proxy_set_header   Host              $http_host;
        proxy_set_header   X-Real-IP         $remote_addr;
        if ($request_filename ~* .*\.(?:htm|html)$)
        {
            add_header Cache-Control "private, no-store, no-cache, must-revalidate, proxy-revalidate";
        }
        if ($request_filename ~* .*\.(?:js|css|jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm)$)
        {
            expires      7d;
        }
    }
}