4.nginx之静态资源web服务

1.sendfile

Syntax:  sendfile on|off;
Default: sendfile off;
Context: http, server, location, if in location

引读:--with-file-aio异步文件读取

2.tcp_nopush

Syntax: tcp_nopush on | off;
Default:tcp_nopuush off;
Context:http,server,location	

作用:sendfile开启的情况下,提高网络包的传输效率

3.tcp_nodelay

Syntax:  tcp_nodelay on | off;
Default: tcp_nodelay on;
Context: http, server, location

作用:keepalive连接下,提高网络包的传输实时性

4.gzip压缩

Syntax: gzip on | off;
Default: gzip off;
Context: http, server, location, if in location

作用:压缩传输

压缩级别

Syntax: gzip_comp_level level;
Default: gzip_comp_level 1;
Context: http,server,location		
Syntax:  gzip_http_version 1.0|1.1;
Default: gzip_http_version 1.1;
Context: http, server, location

扩展nginx压缩模块

http_gzip_static_modult -预读gzip功能

http_gunzip_module -应用支持gunzip的压缩方式

server {
    listen       80;
    server_name  localhost;
    
    sendfile on;
    #charset koi8-r;
    access_log  logs/static_access.log  main;
    
    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  /usr/local/nginx/html/code/images;
    }

    location ~ .*\.(txt|xml)$ {
        gzip on;
        gzip_http_version 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  /usr/local/nginx/html/code/doc;
    }
    
    location ~ .*\.(htm|html)$ {
        expires 24h;
        root /usr/local/nginx/html/code;
    }
    location ~ ^/download {
        gzip_static on;
        tcp_nopush on;
        root /usr/local/nginx/html/code;
    }
}

5.expires

添加Cache-Control、Expires头

Syntax: expires [modified] time;
		expires epoch | max |off;
Default: expires off;
Context:http,server,location,if in location
location ~ .*\.(htm|html)$ {
 	expires 24h;
	root /usr/local/nginx/html/code;      
}

6.跨域访问

location ~ .*\.(htm|html)$ {
        add_header Access-Control-Allow-Origin *; #允许所有网站跨域访问,可以允许单独网址
        add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,OPTIONS;
        #expires 24h;
        root  /usr/local/nginx/html/code;
}

7.基于http_refer防盗链配置模块

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;
 		# 允许http://192.168.127.21 google的获取
        valid_referers none blocked http://192.168.127.21 ~/google\./;
        if ($invalid_referer) {
            return 403;
        }
        root  /usr/local/nginx/html/code/images;
    }

你可能感兴趣的:(Nginx应用)