平滑升級nginx0.8.46到nginx1.0,並增長清除緩存模塊
源碼下載:
wget http://labs.frickle.com/files/ngx_cache_purge-1.0.tar.gz
wget http://www.nginx.org/download/nginx-1.0.0.tar.gz
升級步驟:
一、tar zxvf ngx_cache_purge-1.0.tar.gz
二、tar zxvf nginx-1.0.0.tar.gz
./configure --user=www --group=www --add-module=../ngx_cache_purge-1.0 --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module ,按
原來的安裝路徑配置,本身須要的包打上。
三、而後make,但不要make instal
四、編譯完,在objs目錄下有一個
nginx執行文件,就是它了。
備份下原來老的nginx文件
mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old
五、在把新objs下的nginx cp到sbin下。
cp /root/nginx-1.0.0/objs/nginx /usr/local/nginx/sbin/
七、執行如下命令,讓nginx把nginx.pid改爲nginx.pid.oldbin 跟着啓動新的nginx
kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`
八、退出舊的nignx
kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin`
九、升級完成。
十、清除緩存模塊配置:vi /usr/local/webserver/nginx/conf/nginx.conf
user www www;
worker_processes 8;
error_log /usr/local/webserver/nginx/logs/nginx_error.log crit;
pid /usr/local/webserver/nginx/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;
events
{
use epoll;
worker_connections 65535;
}
http
{
include mime.types;
default_type application/octet-stream;
charset utf-8;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 300m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
client_body_buffer_size 512k;
proxy_connect_timeout 5;
proxy_read_timeout 60;
proxy_send_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript. text/css application/xml;
gzip_vary on;
#注:proxy_temp_path和proxy_cache_path指定的路徑必須在同一分區
proxy_temp_path /data/proxy_temp_dir;
#設置
Web緩存區名稱爲cache_one,內存緩存空間大小爲200MB,1天沒有被訪問的內容自動清除,硬盤緩存空間大小爲30GB。
proxy_cache_path /data/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;
upstream backend_server {
server 192.168.8.43:80 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.8.44:80 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.8.45:80 weight=1 max_fails=2 fail_timeout=30s;
}
server
{
listen 80;
server_name www.yourdomain.com 192.168.8.42;
index index.html index.htm;
root /data0/htdocs/www;
location /
{
#若是後端的服務器返回50二、50四、執行超時等錯誤,自動將請求轉發到upstream負載均衡池中的另外一臺服務器,實現故障轉移。
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_cache cache_one;
#對不一樣的HTTP狀態碼設置不一樣的緩存時間
proxy_cache_valid 200 304 12h;
#以域名、URI、參數組合成Web緩存的Key值,Nginx根據Key值哈希,存儲緩存內容到二級緩存目錄內
proxy_cache_key $host$uri$is_args$args;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://backend_server;
expires 1d;
}
#用於清除緩存,假設一個URL爲http://192.168.8.42/test.txt,經過訪問http://192.168.8.42/purge/test.txt就能夠清除該URL的緩存。
location ~ /purge(/.*)
{
#設置只容許指定的IP或IP段才能夠清除URL緩存。
allow 127.0.0.1;
allow 192.168.0.0/16;
deny all;
proxy_cache_purge cache_one $host$1$is_args$args;
}
#擴展名以.php、.jsp、.cgi結尾的動態應用程序不緩存。
location ~ .*\.(php|jsp|cgi)?$
{
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://backend_server;
}
access_log off;
}