nginx 作為Web快取服務器

來源:互聯網
上載者:User

實驗環境】系統:Centos6.4IP: 192.168.56.120設定伺服器的檔案描述符 如果後面配置nginx的時候,需要調大worker_connections 這個參數,則需要對系統的檔案描述符進行設定,否則可能會出現下面的報錯:worker_connections exceed open file resource limit: 1024
(1) 修改/etc/security/limits.conf檔案,在檔案中添加如下行:* soft noproc 65535* hard noproc 65535* soft nofile 65535* hard nofile 65535就是限制了任意使用者的最大線程數和檔案數為65535。

其中*為所有使用者的開啟檔案數限制,可用"*"號表示修改所有使用者的限制;soft或hard指定要修改軟式節流還是硬限制;65535則指定了想要修改的新的限制值,即最大開啟檔案數(請注意軟式節流值要小於或等於硬限制)。修改完後儲存檔案。


(2) 修改/etc/pam.d/login檔案,在檔案中添加如下行:session required /lib/security/pam_limits.so

這是告訴Linux在使用者完成系統登入後,應該調用pam_limits.so模組來設定系統對該使用者可使用的各種資源數量的最大限制(包括使用者可開啟的最大檔案數限制),而pam_limits.so模組就會從/etc/security/limits.conf檔案中讀取配置來設定這些限制值。修改完後儲存此檔案。


(3) 修改/etc/rc.local指令碼,在指令碼中添加如下行:echo “65535"> /proc/sys/fs/file-max完成上面3步之後重啟伺服器,重啟完,通過ulimit -n 命令確認系統最大檔案描述符是否為剛剛設定的值:[root@localhost ~]# ulimit -n

65535


實驗配置】一、安裝Nginx [root@localhost ~]# yum install pcre-devel openssl-devel perl-ExtUtils-Embed gcc gcc-c++ make wget[root@localhost src]# wget http://labs.frickle.com/files/ngx_cache_purge-1.0.tar.gz[root@localhost src]# wget http://nginx.org/download/nginx-0.8.32.tar.gz [root@localhost src]# tar xf ngx_cache_purge-1.0.tar.gz [root@localhost src]# tar xf nginx-0.8.32.tar.gz [root@localhost src]# useradd -s /sbin/nologin -M www[root@localhost nginx-0.8.32]# ./configure --user=www --group=www --add-module=../ngx_cache_purge-1.0 --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module[root@localhost nginx-0.8.32]# make && make install 注意:這裡nginx可能要使用較低的版本,我用新版本1.5.x 編譯安裝的時候會報錯:make[1]: *** [objs/addon/ngx_cache_purge-1.0/ngx_cache_purge_module.o] Error 1make[1]: Leaving directory `/usr/src/nginx-1.5.3'

make: *** [build] Error 2


二、配置Nginx[root@localhost ~]# mkdir -p /data/proxy_temp_dir[root@localhost ~]# mkdir -p /data/web/www[root@localhost ~]# mkdir -p /data/proxy_cache_dir

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

user  www www;worker_processes  8;                                                                                                 error_log  logs/error.log crit;pid        logs/nginx.pid;worker_rlimit_nofile 65535;                                                                                                 events {    worker_connections  65535;}                                                                                                 http {    include       mime.types;    default_type  application/octet-stream;                                                                                                     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;    tcp_nodelay on;    keepalive_timeout  65;                                                                                                     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;    proxy_cache_path /data/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;#設定Web緩衝區名稱為cache_one,記憶體緩衝空間大小為200MB,1天清理一次緩衝,硬碟緩衝空間大小為30G                                                                                                     upstream web_proxy_cache {        server 192.168.56.113:80 weight=1 max_fails=2 fail_timeout=30s;        server 192.168.56.114:80 weight=1 max_fails=2 fail_timeout=30s;    }                                                                                                     server {        listen       80;    server {        listen       80;        server_name  192.168.56.120;        root   /data/web/www;        index  index.html index.htm;                                                                                                         location / {        proxy_next_upstream http_502 http_504 error timeout invalid_header;# 如果後端的伺服器返回502、504、執行逾時等錯誤,自動將請求轉寄到upstream負載平衡池中的另一台伺服器,實現容錯移轉                                                                                                         proxy_cache cache_one;        proxy_cache_valid 200 304 12h;   #對不同的HTTP狀態代碼設定不同的緩衝時間                                                                                                 # 以網域名稱、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://web_proxy_cache;        expires 1d;        }                                                                                                 # 用於清除緩衝,假設一個URL為http://192.168.56.120/test.txt,通過訪問http://192.168.56.120/purge/test.txt就可以清除該URL的緩衝。        location ~ /purge(/.*) {  # 設定只允許指定的IP或IP段才可以清除URL緩衝                allow 127.0.0.1;                allow 192.168.56.0/24;                deny all;                proxy_cache_purge cache_one $host$1$is_args$args;        }                                                                                                         location ~ .*\.(php|jsp|cgii)?$ {  # 副檔名以.php、.jsp、.cgi結尾的Live App程式不緩衝                proxy_set_header Host $host;                proxy_set_header X-Forwarded-For $remote_addr;                proxy_pass http://web_proxy_cache;                }        access_log off;    }}


檢查配置是否有誤[root@localhost ~]# /usr/local/nginx/sbin/nginx -tthe configuration file /usr/local/nginx/conf/nginx.conf syntax is okconfiguration file /usr/local/nginx/conf/nginx.conf test is successful啟動nginx[root@localhost ~]# /usr/local/nginx/sbin/nginx
三、測試在web1和web2 兩個web伺服器上建立不同頁面[root@web1 ~]# vim /data/web/www/index.html<H1>welcome to web1</H1><img src="123.jpg">在/data/web/www/ 目錄下上傳一個圖片,命名為123.jpg[root@web1 ~]# service httpd start[root@web2 ~]# service httpd start訪問效果如下:

650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/131227/234U042S-0.png" title="clipboard.png" alt="102915308.png" />


測試能否正常代理以及作為web緩衝[root@localhost ~]# curl -dump http://192.168.56.120<h1>welcome to web1</h1>[root@localhost ~]# curl -dump http://192.168.56.120<h1>welcome to web2</h1>清除123.jpg的緩衝,訪問http://192.168.56.120/purge/123.jpg 即可看到下面的頁面650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/131227/234U0A14-1.png" title="clipboard.png" alt="102012367.png" />


四、通過日誌查看HIT 情況

1、開啟日誌

log_format main '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for" ''"$upstream_cache_status"';access_log logs/access.log main;


2、添加$upstream_cache_status這個變數用來顯示緩衝的狀態,我們可以在配置中添加一個http頭來顯示這一狀態 location / { proxy_next_upstream http_502 http_504 error timeout invalid_header; proxy_cache cache_one; proxy_cache_valid 200 304 12h; proxy_cache_key $host$uri$is_args$args; add_header Nginx-Cache "$upstream_cache_status"; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; proxy_pass http://web_proxy_cache; expires 1d; }
3、通過日誌查看是否hits中緩衝可以看到,第一次訪問的時候是沒有通過緩衝的,狀態為-MISS192.168.56.1 - - [09/Aug/2013:12:18:02 +0800] "GET /123.jpg HTTP/1.1" 200 4496 "http://192.168.56.120/" "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36" "-"" MISS"第二次訪問的時候則是通過緩衝,狀態為HITS192.168.56.1 - - [09/Aug/2013:12:18:02 +0800] "GET /123.jpg HTTP/1.1" 304 0 "http://192.168.56.120/" "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36" "-"" HIT"


4、計算HIT命中機率

[root@localhost logs]# awk '{if($NF=="\"HIT\"") hit++} END{printf "%.2f%\n",hit/NR*100}' access.log 51.85%


本文出自 “pmghong” 部落格,請務必保留此出處http://pmghong.blog.51cto.com/3221425/1301412

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.