nginx静态资源缓存服务器
2020-03-03 本文已影响0人
linux_python
nginx静态资源缓存服务器
静态资源指的就是网站中固定的文本,图片这些一旦确定就不会变化的文件; 而在反向代理中缓存住这些文件的话, 可以加快用户访问网站的速度;
- nginx的ngx_http_proxy_module中的proxy_cache指令来实现缓存的加载和命中;
- nginx的ngx_http_proxy_module中的proxy_cache_path指令来定义缓存的存储位置及超时清理时限;
通过使用ngx_http_gzip_module和ngx_http_gunzip_module两个模块来加速缓存的传输, 尤其是采用https协议时, 加速效果是很明显的;
user nginx;
worker_processes 1;
error_log logs/error.log;
pid logs/nginx.pid;
events {
use epoll;
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
upstream webserver {
server 192.168.123.12;
server 192.168.123.13;
}
proxy_cache_path /var/cache/nginx/picture levels=1:1:2 keys_zone=picture:10m max_size=10g inactive=60m;
server {
listen 80;
server_name www.daemons.com;
charset koi8-r;
access_log logs/host.access.log main;
error_log logs/host.error.log;
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://webserver;
}
location ~ \.(gif|jpg|jpeg|png|bmp|ico)$ {
# ngx_http_header_module -->> add_header 添加响应头字段信息;
add_header X-Cache $upstream_cache_status;
proxy_cache picture;
proxy_cache_key $host$uri$is_args$args;#以全路径md5值做做为Key
proxy_cache_valid 200 304 12h; #对不同的HTTP状态码设置不同的缓存时间
expires 7d; #总体缓存时间
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://webserver;
}
}
}