Nginx做爲CDN緩存負載均衡代理的配置實現

系統架構:

nginx+tomcat+mysql

本文只做Nginx做爲CDN緩存負載均衡代理的配置實現的介紹

相關軟件:

nginx-1.8.1.tar.gz

ngx_cache_purge-2.3.tar.gz (用於手動清理緩存)


一、nginx安裝

[root@localhost ~]tar -xf nginx-1.8.1.tar.gz

[root@localhost ~]tar -xf ngx_cache_purge-2.3.tar.gz -C /usr/local/ngx_cache_purge-2.3

[root@localhost ~]cd nginx-1.8.1

[root@localhost nginx-1.8.1]./configure --prefix=/usr/local/nginx --user=nginx --without-http_fastcgi_module --without-http_scgi_module --add-module=/usr/local/ngx_cache_purge-2.3;make;make install


二、nginx.conf配置

user nginx;
worker_processes  2;

error_log  logs/error.log;
pid /usr/local/nginx/nginx.pid;

events {
    worker_connections  1024;
}

http {
	charset  utf-8;
	server_names_hash_bucket_size 128;
	client_header_buffer_size 4k;
	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;

	server_tokens off;	  

	log_format  main  '$http_x_forwarded_for - $remote_user [$time_local] "$request" '
			  '$status $body_bytes_sent "$http_referer" '
			  '"$http_user_agent" "$upstream_cache_status" $remote_addr';
	#緩存配置1:2表示第一級目錄1個字符,第二級目錄2個字符;cache1:20m表示每個緩存區域20M空間;
	#3d表示3天后緩存過期
	proxy_cache_path  /ngx_cache/proxy_cache/cache1  levels=1:2 keys_zone=cache1:20m inactive=3d max_size=500m;			   
	
	upstream web_app
	{
	 server 0.0.0.0:8081 max_fails=2 fail_timeout=30s;
	}

    server {
        listen      80;
        server_name  
         
        #purge緩存清理配置
	location ~ /purge(/.*) {
		allow all;
		proxy_cache_purge cache1 $host$1$is_args$args;
		}
		
	#代理配置
        location / {
                proxy_pass http://web_app;
                proxy_next_upstream http_502 http_504 error timeout invalid_header;
                proxy_set_header Host  $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

		add_header Nginx-Cache $upstream_cache_status;#添加響應頭的緩存狀態,在瀏覽器F12可以看到Nginx-Cache:HIT,Nginx-Cache:MISS等狀態
		proxy_cache cache1;#設置資源緩存的zone
		proxy_cache_key $host$uri$is_args$args;#設置緩存的key,以域名、URI、參數組合成Web緩存的Key值,Nginx根據Key值哈希,存儲緩存內容到二級緩存目錄內
		proxy_cache_valid 200 304 30m;#200和304狀態碼進行30分鐘的緩存
		expires      1d;#緩存過期時間
        	}

        #錯誤頁提示
        error_page   500 502 503 504  /50/50.html;
        location ~ /50(/.*) {
        	root   html;
        	}
	}

三、緩存清理測試

如訪問http://203.195.144.57/public/images/main/homepage/wechat.jpg

wKioL1ePW5DwKNT7AABAmPxLMYg182.jpg-wh_50

則清理此圖片的格式爲http://203.195.144.57/purge/public/images/main/homepage/wechat.jpg

wKiom1ePXEDjdCyUAACY_kWjDwg951.jpg-wh_50


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章