常见的nginx中间件架构
1、静态资源web服务(文件读取sendfile、tcp_nopush、tcp_nodelay、压缩,预读gzip板块)2、代理商服务3、负载均衡调度器SLB4、动态缓存(增加cache-control、expires头)
静态资源解决
静态资源访问、压缩
- nginx配置
server{ listen 80; server_name localhost; sendfile on; access_log /var/log/nginx/log/static_access.log main; #匹配图片 location ~ .*\.(jpeg|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 /home/project/nginx-code/images; } #匹配文本 location ~ .*\.(txt|xml)$ { 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 /home/project/nginx-code/doc; } #相当于预先压缩文件,这个文件夹下的能由其余脚本生成压缩文件访问 location ~ ^/download$ { gzip_static on; tcp_nopush on; root /home/project/nginx-code; }}12345678910111213141516171819202122232425262728293031
- linux命令
#测试配置文件nginx -tc /etc/nginx/nginx.conf#重新加载配置文件nginx -s reload -c /etc/nginx/nginx.conf1234
- 结果
开启压缩前: