定义缓存目录
使用您喜欢的文本编辑器打开/etc/nginx/nginx.conf,并在http {区域加入:
proxy_cache_path /var/www/cache levels=1:2 keys_zone=my-cache:8m max_size=1000m inactive=600m;
proxy_temp_path /var/www/cache/tmp;
real_ip_header X-Forwarded-For;
前2行创建一个缓存目录。 真正的X-Forwarded-For头指示Nginx将原始IP地址转发到后端(端口8080),否则所有流量似乎都来自127.0.0.1。
应用缓存
接下来,我们需要在/etc/nginx/sites-available/website下创建虚拟主机
server {
listen 80;
server_name _;
server_tokens off;
location / {
proxy_pass http://127.0.0.1:8080/;
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_cache my-cache;
proxy_cache_valid 3s;
proxy_no_cache $cookie_PHPSESSID;
proxy_cache_bypass $cookie_PHPSESSID;
proxy_cache_key "$scheme$host$request_uri";
add_header X-Cache $upstream_cache_status;
}
}
server {
listen 8080;
server_name _;
root /var/www/your_document_root/;
index index.php index.html index.htm;
server_tokens off;
location ~ \.php$ {
try_files $uri /index.php;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
然后通过执行以下操作启用它:
cd
ln -s /etc/nginx/sites-available/website /etc/nginx/sites-enabled/website
/etc/init.d/nginx restart
第一个服务器定义是在端口80上运行的反向缓存代理。
第二个服务器定义用于后端(典型的nginx配置,端口8080,而不是80)。
proxy相关指令介绍
proxy_pass http://127.0.0.1:8080/将流量转发到端口8080,Nginx后端位于该端口
proxy_cache my-cache定义要使用的高速缓存,这里是my-cache,我们之前在nginx.conf中添加的
proxy_cache_valid 3s将缓存时间设置为3秒。 在确定缓存到期之前的秒数(清除缓存
.........................................................