记录Nginx作为静态资源web服务场景配置

时间:2021-03-30 06:13:03

server {

listen   80;

server_name    localhost;

sendfile    on;

access_log    /var/log/nginx/host.access.log  main;

#以jpg、gif、png结尾的下载路径

location ~.*\.(jpg|gif|png)$ {
                #gzip on;
                #gzip_http_version 1.1;
                #gzip_comp_level 2;
                #gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
                 root /opt/app/code/images;
            }
          #以txt、xml结尾的下载路径
          location ~.*\.(txt|xml)$ {
               #gzip on;
               #gzip_httpaversion 1.1;
               #gzip_comp_level 1;
               #gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
               root /opt/app/code/doc;
           }
         #以download目录开头的下载路径
         location ~^/download {
               gzip_static on;
               tcp_nopush on;
              root /opt/app/code;
         }

#以htm、html结尾的文件缓存模式
         location ~.*\.(htm|html)$ {
              expires  24h;
              root /opt/app/code;
         }

#设置允许跨域
         location ~.*\.(htm|html)$ {
              add_header Access-Control-Allow-Origin http://www.aabb.com;
              add_header Access-Contorl-Allow-Methods GET,POST,PUT,DELETE,OPTIONS;            
              root /opt/app/code;
         }

#设置防盗链:防止图片等资源被其他网站直接盗用
         location ~.*\.(gif|png|bmp)$ {
              valid_referers none blocked 允许访问的ip地址;

if($invalid_referer){

return 403;

}
              root /opt/app/code;
         }

}