Nginx支持相似Squid的緩存功能。Nginx的web緩存服務主要由proxy_cache相關命令集和fastcgi_cache相關的命令集構成的。前者是用於反向代理時對後端內容服務器進行緩存。後者主要是用於對fastcgi的動態程序進行緩存。固然能夠也可以使用第三方模塊ngx_slowfs_cache實現緩存服務器的配置。javascript
1、在Nginx下面安裝緩存服務器php
這裏呢,使用Nginx自帶的緩存模塊。經過nginx_cache命令實現數據的緩存。css
1.下載安裝ngx_cache_purge插件:html
wget http://labs.frickle.com/files/ngx_cache_purge-2.1.tar.gz 直接解壓便可: mkdir /app tar zxvf ngx_cache_purge-2.1.tar.gz -C /app
2.編譯安裝Nginx
java
tar zxvf nginx-1.6.2.tar.gz cd nginx-1.6.2 ./configure --user=www --group=www --prefix=/usr/local/nginx \ > --add-module=/app/ngx_cache_purge-2.1\ > --with-http_stub_status_module \ > --with-http_ssl_module make && make install 提示:安裝完成後,用"/usr/local/nginx/sbin/nginx -V"查看Nginx的版本和模塊信息 [root@mysql nginx-1.6.2]# /usr/local/nginx/sbin/nginx -V nginx version: nginx/1.6.2 built by gcc 4.4.7 20120313 (Red Hat 4.4.7-11) (GCC) TLS SNI support enabled configure arguments: --user=www --group=www --prefix=/usr/local/nginx --add-module=/app/ngx_cache_purge-2.1 --with-http_stub_status_module --with-http_ssl_module
2、配置Nginx緩存服務器node
Nginx緩存服務器的配置主要經過proxy_cache相關命令集實現的,下面是nginx.conf文件內容:mysql
user www www; worker_processes 1; error_log /usr/local/nginx/logs/nginx_error.log crit; pid /usr/local/nginx/nginx.pid; 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 64k; client_max_body_size 300m; sendfile on; tcp_nopush on; keepalive_timeout 65; tcp_nodelay on; client_body_buffer_size 128k; proxy_connect_timeout 5; proxy_read_timeout 60; proxy_send_timeout 5; proxy_buffer_size 16k; proxy_buffers 4 64k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.1; gzip_types text/plainapplication/x-javascript text/css application/xml; proxy_cache_path /backup/proxy_cache_dir levels=1:2 keys_zone=cache_one:4096m inactive=1d max_size=3g; #proxy_cache_path:設置緩存的目錄,後面跟緩存路徑,最好緩存目錄放在一個獨立硬盤 #levels=1:2 設置目錄深度,這裏是兩層目錄深度,第一層是一個字符,第二層是兩個字符 #keys_zone設置web緩存區名稱,這裏是cache_one,後面的4096m表示內存緩存空間大小爲4GB #inactive表示自動清除緩存文件的時間,1d表示一臺內沒有被訪問的內容自動清除 #max_size表示硬盤緩存空間可使用的最大值,默認狀況下經訪問的文件常放在內存中緩存,當內存緩存空間不足的時候,Nginx會將不常常訪問的數據從內存寫到磁盤 server { listen 80; server_name www.a.com www.b.com ; charset UTF8; #access_log logs/host.access.log main; location / { proxy_cache cache_one;#反向代理緩存設置命令,語法爲:"proxy_cache zone|off" proxy_cache_valid 200 300 12h;#對不一樣的HTTP狀態碼設置不一樣的緩存時間 proxy_cache_key $host$uri$is_args$args;#設置以什麼樣的參數獲得緩存 proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; proxy_pass http://127.0.0.1:8080; expires 1d; } #下面這段用於配置手動清除緩存策略,清除的方法爲:若是一個URL爲 #http://www.a.com/2014/0413/3.html,那麼經過訪問 #http://www.b.com/purge/2014/0413/3.html便可清除該URL的緩存 location ~ /purge/(/.*) { allow 127.0.0.1;#表示容許指定的IP或者IP段才能夠清除URL緩存 allow 192.168.88.0/24; deny all; proxy_cache_purge cache_one $host$1$is_args$args; } location ~ .*\.(jsp|php|jspx)?$ #設置不作緩存的內容 { proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; proxy_pass http://127.0.0.1:8080; } access_log off; } }
提示:檢查語法出錯nginx
[root@mysql conf]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: [alert] mmap(MAP_ANON|MAP_SHARED, 4294967296) failed (12: Cannot allocate memory) nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed
解決方法:web
緣由是共享內存空間設置過大,系統已沒有足夠的內存分配,致使沒法啓動nginx,能夠將上面的keys_zone=ngx_fcgi_cache:150m,設置小一些,例如keys_zone=ngx_fcgi_cache:50m,便可sql
3、測試proxy_cache實現的緩存功能
#啓動Nginx,若是有"change manager"進程,說明此進程是用來管理緩存服務和文件的 [root@mysql conf]# ps -ef|grep nginx root 4786 1 0 15:45 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx nginx 4787 4786 0 15:45 ? 00:00:00 nginx: worker process nginx 4788 4786 0 15:45 ? 00:00:00 nginx: cache manager process nginx 4789 4786 0 15:45 ? 00:00:00 nginx: cache loader process #接着能夠經過www.a.com訪問任意靜態頁面,測試訪問速度是否加快。同時能夠在緩存目錄裏面查看是否已經生成緩存文件了。若是有緩存文件,說明Nginx的緩存服務搭建成功了!
提示:若要清除指定的URL緩存,直接在清除緩存網頁的URL地址前面加purge便可。