Nginx 學習筆記

 常見web服務器

apache
loghttpd
tomcat
IBM websphere
Microsoft IIS


Nginx 的中文維基

Nginx ("engine x") 是一個高性能的 HTTP 和  反向代理  服務器,也是一個 IMAP/POP3/SMTP  代理服務器  。 Nginx 是由  Igor Sysoev 爲俄羅斯訪問量第二的 Rambler.ru  站點開發的,它已經在該站點運行超過四年多了。Igor 將源代碼以類BSD許可證的形式發佈。自Nginx 發佈四年來,Nginx 已經因爲它的穩定性、豐富的功能集、 示例配置文件和低系統資源的消耗而聞名了。目前國內各大門戶網站已經部署了Nginx,如新浪、網易、騰訊等;國內幾個重要的視頻分享網站也部署了Nginx,如六房間、酷6等。 新近發現Nginx 技術在國內日趨火熱,越來越多的網站開始部署Nginx。

 
使用BSD協議   2011市場佔有率爲nginx5.3%  apache70% IIS 20.5%
 
 
Nginx優勢
1.高併發,CPU消耗低
    Nginx支持5W高併發連接,實際2-4W.
    使用epoll(linux2.6內核)和kqueue(freeBSD)網絡模型 
    而Apache使用傳統select模型和prefork多進程模式
2.內存消耗少
    同等硬件條件下Nginx的處理能力是Apache的5-10倍
3.成本低廉
    使用BSD協議發行
4.其它優勢
   配置簡單、支持Rewrite重寫、內置健康檢查功能、節省帶寬、穩定性高、支持熱部署
 
Nginx的版本
    Nginx分舊穩定版、新穩定版和開發版三個版本
    從0.7版本開始支持windows版,但其效率較低下
Nginx編譯需求
    10M磁盤空間
    GCC編譯器
    autoconf和automake工具
 
模塊依賴性   
    gzip模塊需要zlib庫
    rewrite模塊需要pcre庫
    SSL功能需要Openssl庫
 
./configure常用參數 
    --prefix=<path>                安裝路徑,默認爲/usr/local/nginx
    --sbin-path=<path>            執行文件安裝路徑,默認爲<prefix>/sbin/nginx
    --conf-path=<path>            在沒有給定-c選項下的默認路徑,默認爲<prefix>/conf/nginx.conf
    --pid-path=<path>               PID路徑,沒有指定PID下的路徑爲<prefix>/logs/nginx.pid
    --lock-path=<path>             nginx.lock文件路徑
    --error-log-path=<path>       錯誤日誌路徑
    --http-log-path=<path>        access_log路徑
    --user=<user>                   Nginx使用用戶,默認爲nobody
    --group=<group>               Nginx使用的組,默認爲nobody
    --with-resig_module            啓用rtsig模塊
    --with-select_module           是否允許啓動SELECT模式,如沒有Kqueue.epoll.rtsig或poll,select模式殭屍默認模式
    --with-poll_module               沒有更好的模式則啓動poll模式
    --with-http_ssl_module         開啓SSL模塊,debian上是libssl
    --with-http_realip_module       啓動htto_realip_module
    --with-http_addition_module    啓動http_addittion_module
    --with-http_sub_module        啓動http_sub_module
    --with-http_flv_moudle           啓動flv模塊
    --with-http_charset_module    禁用server status頁
    --with-http_gzip_module        禁用gzip
    --with-http-rewrite_module      禁用rewrite
    --with-http_proxy_module       禁用proxy
    --with-http_perl_module        禁用perl模塊
    --http-client-body-temp-path=PATH    http客戶端請求緩存文件路徑
    --http-proxy-tmp-path=PATH             http反向代理緩存文件目錄
    --without-http                        禁用http SERVER   
    --with-mail                            啓用IMAP4/POP3/SMTP代理
    --with-mail_ssl_module            maillsll模塊
    --without-pcre                       禁用PCRE庫
    --with-zlib=DIR                    zlib庫路徑
    --with-openssl=DIR                openssl庫路徑
    --with-dubug                        啓用調速日誌
    --add-module=PATH                添加第三方模塊的目錄
 
 
 ./configure  --user=www --group=www   --with-pcre=/usr/include/pcre --with-http_stub_status_module --prefix=/usr/local/nginx --with-http_ssl_module

啓動nginx
    /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
    如不加-c則默認使用安裝目錄下/conf/nginx.conf
 
配置檢查
    nginx -t -c  /usr/local/nfinx/conf/nginx.conf
 
平滑重啓,重新加載配置文件
    ps -ef|grep nginx
 
    kill -HUP    `cat \usr\local\nginx\logs\'nginx.pid`
 
其它信號
    TERM,INT         快速關閉
    QUIT                從容關閉
    USR1                重新打開日誌
    USR2                平滑升級
    WITCH               從容關閉工作進程
 
平滑升級
    在不中斷服務的情況下使用新版本、重新編譯的程序
    1.備份舊的執行文件
    2.kill -USR2 舊版本主進程號
        舊的.pid文件將重命名爲old.bin然後執行新的可執行程序
        依次啓動新主進程和工作進程
    3.此時新舊版本同時運行,使用WINCH信號發送給舊的主進程,然後他的工作進程也將從容關閉
        kill -WINCH    舊版本主進程號
    4.一段時間後舊的工作進程處理已有連接請求後退出
    5.此時可決定是否使用新的進程
        如不使用
        kull -HUP 舊進程號               不重載配置文件下重啓工作進程
        kill  -QUIT   新主進程             從容關閉其工作進程
        kill -TERM   新主進程            強制退出
        新進程退出後舊進程將移除.oldbin前綴,恢復到.pid文件
 
        如果使用
        kill -QUIT 舊的主進程
 
虛擬主機的配置
----------------------------------------------------------------------------------
 
worker_processes  1;
#指定工作衍生進程數(一般爲CPU總核心數的兩倍)
 
events 
{
     worker_connections  1024;
     #允許的連接數量
}
 
http {
   include     mime.types;
   default_type  application/octet-stream;
 
   sendfile     on;
   keepalive_timeout  65;
 
   server
     {
     listen     80;
     server_name  www.vspace.tk *.vspace.tk;
     access_log  logs/www.vspace.tk  main;
       location /
           {
            root   /etc/web/www;
            index  index.html index.htm;
          }
     }
   server
     {
     listen      80;
     server_name    bbs.vspace.tk;
     access_log    log/bbs.vspace.tk combined;
     location /
             {
             root   /etc/web/bbs;
             index   index.html index.htm;
         }
     }
}
--------------------------------------------------------------------------------
 
 

3)虛擬主機配置文件詳解
  
由於虛擬主機分別有一個文件來指定,下面舉例某個虛擬主機的配置如下:
[root@oracle132 vhosts]#
vi /usr/local/nginx/conf/vhosts/www.test.com.conf;
  
server {
        listen       80; #虛擬主機使用端口
        server_name  www.test.com; #虛擬主機訪問域名
        charset UTF-8; #設定nginx默認字符編碼
        #access_log logs/host.access.log main;
#所有jpg格式的圖片都有nginx來處理
        location ~ \.jpg$ {
                root    /cicro/cws3/vhosts/www.test.com/ROOT;
                expires 30d;
        }
#所有gif格式的圖片都有nginx來處理
        location ~ \.gif$ {
                root    /cicro/cws3/vhosts/www.test.com/ROOT;
                expires 30d;
        }
# upload和html下所有文件都有nginx來處理

        location ~ ^/(upload|html)/  {

                root    /cicro/cws3/vhosts/www.test.com/ROOT;

                expires 30d;

        }

#除去上面的文件,剩下的所有都代理給http://127.0.0.1:8009來訪問

        location / {

            root   /cicro/cws3/vhosts/www.test.com/ROOT;

            index  index.html;

            proxy_pass  http://127.0.0.1:8009;

        }
#設定查看Nginx狀態的地址

        location /NginxStatus {

                        access_log              on;

                        auth_basic              "NginxStatus";

auth_basic_user_file    ../htpasswd;

        }

        #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;

        }

}

 在上面有設置查看Nginx狀態的地址,需要apache的htpasswd 來生成一個登錄驗證文件,這樣生成一個htpasswd 文件:

[root@oracle132 vhosts]# /usr/local/bin/htpasswd  -c htpasswd gaojf

New password:   (此處輸入您的密碼)
Re-type new password:   (再次輸入您的密碼)
Adding password for user  gaojf

 

上面 /usr/local/bin/htpasswd 是htpasswd 文件的執行路徑,如果沒有這個文件,可以從apache的bin目錄拷貝一個過來即可!

-c是創建一個文件

-c後面的httpasswd是創建驗證文件的名字.

gaojf是創建的用戶

 

#查看nginxstatus:
http://www.test.com/nginxstatus/,輸入驗證帳號密碼,即可看到類似如下內容:
Active connections: 328
server accepts handled requests
9309 8982 28890
Reading: 1 Writing: 3 Waiting: 324
  第一行表示現在活躍的連接數
  第三行的第三個數字表示Nginx運行到當前時間接受到的總請求數,假如快達到了上限,就需要加大上限值了。

 
日誌
 
   log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
            '$status $body_bytes_sent "$http_referer" '
            '"$http_user_agent" "$http_x_forwarded_for"';
    如果web前端存在中間層則收集不到remote_addr而是拿到前端的地址
    但是反向代理在轉發請求的http頭信息中,可以增加X-Forwarded-For信息,來記錄原有客戶端IP
 
logs/www.log
 
192.168.10.138 - - [19/Mar/2011:18:04:59 +0800] "GET / HTTP/1.1" 200 34 "-" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322)" "-"
127.0.0.1 - - [19/Mar/2011:19:02:43 +0800] "GET / HTTP/1.1" 200 34 "-" "Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.151 Safari/534.16" "-"
 
access_log
 
    access_log /data/logs/web1.log main
    也可使用變量
    access_log /data/logs/$server_name.log main
    但有一些限制,1.Nginx進程的用戶和組必須有該路徑的讀寫權限
                        2.緩存將不會被使用
                        3.對於每一條日誌記錄,日誌文件都將先打開文件再寫入日子然後關閉
    爲了提高包含變量的日誌文件存放路徑的性能需要使用open_log_file_cache指令設置經經常被使用的日誌文件描述符緩存
 
    open_log_file_cache指令主要用來設置含有變量的日子路徑的文件描述符緩存
    語法:    open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time] | off
    默認我禁止
    max:設置緩存中的最大文件描述符數量。如超過則使用LRU算法清除“較不常用的文件描述符”
    iniactive:設置一個時間,如果時間內沒有使用此文件描述符則刪除,默認10s
    min_uses:在inactive指定時間範圍內,如果日誌文件超過被使用次數則記入緩存,默認爲1
    valid:設置多久檢查一次,看看變量指定的日誌文件路徑與文件名是否存在,默認60s
    open_log_file_cache max=1000 inactive=20s min_uses=2 valid=1m
 
 
Nginx日誌文件的切割
    nginx不支持像Apache一樣使用cronolog來輪轉日誌,但是可以採用以下方式來實現日誌文件的切割
    mv /data/logs/access.log  /data/logs/20110101.log
    kill -USR1 Nginx主進程號
 
 
可以通過crontab和shell腳本來實現日誌切割的自動化
 
nginx_cut_log.sh
---------------------------------------------------------------------
#!/bin/bash
#這個腳本須在每天00:00運行
 
#Nginx 日誌文件的存放路徑
logs_path="/usr/loacl/nginx/logs/"
 
mkdir -p ${log_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/
mv ${logs_path}access.log ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/
$(date -d "yesterday" +"%m")/access_$(date -d "yesterday" +"%Y%m%d").log
kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
-------------------------------------------------------------------------
 
crontab -e
00 00 * * * /bin/bash  /usr/local/nginx/sbin/cut_nginx_log.sh
 
nginx瀏覽器本地緩存設置
    語法:expir [time|epoch|max|off]        默認爲off
    作用域:http、server、location
    控制http應答中的expires和cache-control的header頭信息起到控制頁面緩存的作用
    
    time爲當前時間-/+來獲得
      負數表示no-cache
     正數或零表示max-age=time
    
    epoch指定expires值爲1 January,1970,00:00:01 GMT
    max指定expires值爲31 December 2037 23:59:59 GMT
     -1:指定“Expires”的值爲當前服務器時間-1s,即永遠過期。
    
     off:不修改“Expires”和"Cache-Control"的值
    
例如
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        expires 30d;
    }
 
 
 
安裝過程
 
PHP加速
; eaccelerator
[eaccelerator]
zend_extension="/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so"
eaccelerator.shm_size="128"
eaccelerator.cache_dir="/tmp/eaccelerator"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="3600"
eaccelerator.shm_prune_period="3600"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"
eaccelerator.keys = "disk_only"
eaccelerator.sessions = "disk_only"
eaccelerator.content = "disk_only"
eaccelerator.allowed_admin_path = "/ext/web/www/eaccelerator/control.php"
 
 
內核優化
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章