第一, nginx 的介紹
1 nginx 從0.7.48 版本後凱斯,支援烈士squid的緩存功能。該緩存是把URL及相關組合當作key,然後用md5編碼哈希後儲存在硬碟上,是以nginx 支援任意的URl連接配接,同時也支援404/301/302 這樣非200 狀态碼。雖然目前官方nginx web 緩存伺服器隻能為指定的URL或者狀态碼設定過期時間,但是不支援類似squid的purge 指令,需要手動清除緩存頁面,但是,通過第三方的nginx子產品,可以清楚指定URL的緩存,當然nginx的web緩存服務主要是由proxy_cache相關指令集和fastcgi_cache相關指令集構成,而proxy_cache 主要是用于反向代理,又來緩存後端伺服器的内容源而Fastcgi_cache 主要緩存的是動态程式
下載下傳相關的軟體包
prce-8.00.tar.gz ngx_cache_purge-1.2.tar.gz nginx-0.8.53.tar.gz
第三 安裝相關的軟體包
#tar xvf prce-8.00.tar.gz
#cd prce-8.00
#./configure
#make && make install
#useradd -s /sbin/nologin www
#cd ../
#tar –xvf ngx_cache_purge-1.2.tar.gz
#tar nginx-0.8.53.tar.gz
#cd nginx-0.8.53
#./configure --user=www –group=www –add-module=../ngx_cache_purge-1.2 –prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
第四,修改并配置nginx
#cd /usr/local/nginx/conf
#vim nginx.conf
#nginx 運作的使用者 user nobody;
#開啟的程序數
worker_processes 1;
#定義錯誤日志的路徑及其日志級别
error_log /usr/local/nginx/logs/error.log crit;
#nginx 的程序
pid logs/nginx.pid;
#specifiles the value for maximum file descriptors that can be opened by this process
#檔案句柄數,和系統單程序打開的檔案數相同,不必理會程序個數
worker_rlimit_nofile 65535;
events {
use epoll;
worker_connections 1024; #定義的是單個程序的連接配接數,該值受系統程序打開檔案數限制,需要修改打開的檔案句柄數,但是max_client = worker_proxesses X work_connextions,
}
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" '
'"request_method $scheme://$host$request_uri $server_protocol" $status $body_bytes_sent'
'"$http_user_agent" "$http_x_forwarded_for"';
# 指定伺服器名稱哈希的大小,hash bucket size 等于一路處理器緩存大小,與server_names_hash_max_size 共同控制儲存伺服器名的HASH表
server_names_hash_bucket_size 128;
# 以下兩項是設定用戶端請求Header頭緩存去的大小,4 為個數。128k 為大小。申請4個128k。當http 的uri太長或者request header 過大時會報414 Request URI too large 或者400 bad request
client_header_buffer_size 32k;
large_client_header_buffers 4 128k;
#HTTP請求的BODY 最大限制,若超出此值,報413 Request Entity Too Lager
client_max_body_size 8m;
#緩沖去代理使用者請求的最大位元組數,可以了解為先儲存本地,然後在傳給使用者
client_body_buffer_size 32k;
#不允許用戶端主動關閉連接配接,如果該項為設定在nginx的日志中可能出現499 錯誤
proxy_ignore_client_abort on;
#nginx 和後端伺服器連接配接逾時時間 發起握手等候響應時間
proxy_connect_timeout 5;
#連接配接成功後等候後端伺服器響應時間,其實已經進入後端的排隊等候處理
proxy_read_timeout 60;
#後端伺服器資料回傳時間,就是在規定的時間内後端伺服器必須傳完所有的資料
proxy_send_timeout 5;
#代理請求緩存去,該緩存去間儲存使用者的頭資訊,以供nginx進行規則處理一般隻要保能儲存下頭資訊即可
proxy_buffer_size 32k;
#告訴nginx儲存單個用的幾個buffer 最大用多少空間
proxy_buffers 4 64k;
#高負載下緩沖大小(proxy_buffers*2)
proxy_busy_buffers_size 128k;,
#設定緩存檔案夾大小,如果大于該值,将從upstream 伺服器傳遞請求,而不緩沖到磁盤上
proxy_temp_file_write_size 1024m;
#這個将為打開檔案指定緩存,預設是沒有啟用的,max指定緩存數量,建議和打開檔案數一緻,inactive是指經過多長時間檔案沒被請求後删除緩存。
open_file_cache max=102400 inactive=20s;
#這個是指多長時間檢查一次緩存的有效資訊。
open_file_cache_valid 30s;
#open_file_cache指令中的inactive參數時間内檔案的最少使用次數,如果超過這個數字,檔案描述符一直是在緩存中打開的,如上例,如果有一個檔案在inactive時間内一次沒被使用,它将被移除。
open_file_cache_min_uses 1;
#shutdown error display nginx version
# 關閉錯誤時的nginx 的版本顯示
server_tokens off;
#open os function sendfile
# 打開高效的檔案傳輸模式
sendfile on;
#tcp_nopush on;
tcp_nopush on; # 打開linux TCP_CORK,隻有sendfile 打開時,該項才有效,用來發送系統HTTP response headers 設定選項的目的是告訴TCP協定不要僅僅為清空發送的緩存而發送封包段。通常三個設定TCPNOPUSH 插口選項。當請求長度超過封包段最大長度時,協定就好可能發出滿長度的封包段,這樣可以減少封包段的數量,減少的程度取決于每次發送的數量
keepalive_timeout 60;
tcp_nodelay on; #打開TCP_NODELAY 在包含了keepalived 才有效,上面的四項有效的提高了檔案傳輸性能,用sendfile() 函數來轉移大量的資料,協定會需要預先解決資料包標頭部分,正常情況下標頭很小,而且套接字上設定了TCP_NODELAY 。有標頭的包将被立即傳輸,在一些情況下,因為包成功的被對方收到後需要請求對方确認,這樣,大量的資料傳輸就會被延遲而且産生大量不必要的網絡流量交換,但是在socket上是指了TCP_CORK ,就像個管道塞住塞子把帶有標頭的包填滿資料,所有的資料根據大小進行填充,自動通過資料包發送出去,但是在資料傳送完成是,需要把塞子打開
#開啟gzip 的設定
#gzip on;
gzip on;
#設定允許壓縮的頁面的最小位元組數,頁面位元組數從Header 頭中的Content-Length中擷取建議設定成大于1k的位元組數,小于1k可能越壓越大
gzip_min_length 1k;
#設定以16k為機關4倍申請記憶體做壓縮結果緩存,預設值是申請跟原始資料相同大小的記憶體空間存儲gzip壓縮結果
gzip_buffers 4 16k;
#預設1.1 ,大部分浏覽器支援gzip 壓縮
gzip_http_version 1.1;
#設定壓縮級别 壓縮比率1-9,壓縮比率越大,越消耗系統資源
gzip_comp_level 2;
#設定壓縮列席
gzip_types text/plain application/x-javascript text/css application/xml;
#設定前端的緩存服務,如squid緩存經過nginx壓縮的資料,該選項在做反向代理是設定壓縮,後面參數為驗證的header頭資訊,在做相應的壓縮處理,
gzip_vary on;
#proxy_cache
# 該處設定的是緩存的目錄及其設定的大小
proxy_cache_path /usr/local/nginx/proxy_temp levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;
#設定負載池
upstream backend_server {
server 192.168.2.194:80 ;
# 注weight設定的權重 max_fails 設定的是健康監測失敗次數,timeout 設定逾時時間 當然此處主要是設定nginx的反向代理及其緩存,對于nginx 的負載均衡,在以後的文章中再詳細介紹
# server 192.168.2.107:80 weight=1 max_fails=2 fail_timeout=30s;
}
server {
#注:監聽的端口号
listen 80;
#server_name localhost;
#設定監聽的主機名
if ($host !~ 'freehat.blog.51cto.com') {
return 403;
}
#charset koi8-r;
#access_log logs/host.access.log main;
#location / {
# root html;
# index index.html index.htm;
#}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
#設定擴充名以gif .jpg .css 等結尾的靜态檔案緩存
location ~.*\.(gif|jpg|jpeg|png|bmp|sqf|js|css)$
{
#設定防盜鍊
valid_referers none blocked freehat.blog.51cto.com;
if ($invalid_referer) {
return 404
#如果後端的伺服器傳回502 504 執行逾時等錯誤,自動将請求轉發到upstream 負責均衡池中的另外一台伺服器,實作故障轉移
proxy_next_upstream http_502 http_504 error timeout invalid_header;
#進行緩存,使用web緩存去cache_one
proxy_cache cache_one;
#以域名,URI,參數組合成web緩存的Key值,Nginx根據Key值哈希,儲存緩存内容到二級緩存目錄内
#對不同的HTTP 狀态碼設定不同的緩存時間
proxy_cache_valid 200 304 12h;
proxy_cache_valid any 1m;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
#設定proxy_set_header Accept-Encoding 或者是背景伺服器關閉gzip,該台機器才不會緩存被壓縮的檔案,造成亂碼
proxy_set_header Accept-Encoding "none";
# proxy_set_header Accept-Encoding ""; 同上面一項相同
#設定proxy_cache 支援背景設定的expires , 即使支援HTTP頭資訊定義的緩存規則
proxy_ignore_headers "Cache-Control" "Expires";
if ( !-e $request_filename )
{
}注: 該項是如果client 請求的檔案在cache 中儲存,nginx 直接從cache中把對應的檔案返還給client ,不會在去後端的Server 去取對應的檔案
expires 1h; 設定過去的時間
#set don't php jsp cgi in cache 設定php jsp cgi 不儲存在cache中
location ~.*\.(php|jsp|cgi)?$
{
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://backend_server;
}
#set js and css cache expire time 設定js|css 在浏覽器中過期的時間
location ~ .*\.(js|css)
{
expires 1h;
}
#clean all cache 設定清除所有的cache
location ~/purge(/.*)
{
allow 127.0.0.1;
allow 192.168.2.0/24;
deny all;
proxy_cache_purge cache_one $host$1$is_args$args;
}
第五,啟動機及其關閉nginx的指令
1 啟動之前檢查nginx的主配置檔案是否有錯誤
# /usr/local/nginx/sbin/nginx -t
2 啟動nginx的方式
# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
3 關閉nginx 的方式
# /usr/local/nginx/sbin/nginx -s stop
4 平滑啟動nginx
#kill -HUP `cat /usr/local/nginx/logs/nginx.pid`
本文轉自 freehat08 51CTO部落格,原文連結:http://blog.51cto.com/freehat/511002,如需轉載請自行聯系原作者