歡迎您光臨本站 註冊首頁

NGINX 介紹

←手機掃碼閱讀     火星人 @ 2014-03-04 , reply:0

NGINX 介紹

NGINX總結

目錄

一、安裝        2
A)一般安裝        2
B)高級安裝        2
二、基本命令        3
三、配置文件        3
A)配置文件解釋        3
B)帶監控模塊的配置        4
C)虛擬主機的配置        5
五、事件模型介紹--I/O復用方法        7
A)標準事件模型        7
B)高效事件模型        7
六、關於LEMP的結構        8
A)Linux+Nginx+Mysql+Php        8
B)其他結構        8
七、軟體的平滑升級        10
A)簡單修改配置文件        10
B)平滑升級二進位代碼        11
八、相關的處理信號        12
A)主進程可以處理的信號        12
B)工作進程可以處理的信號        12

Nginx (engine X)是一個高性能的HTTP和反向代理伺服器,也是一個IMAP/POP3/SMTP代理伺服器。Nginx是俄羅斯人開發的。Nginx的英文站點:http://nginx.net。Nginx的wiki站點:http://wiki.codemongers.com/Main
《解決方案》

一、安裝
A)一般安裝
./configure –prefix=/usr/local/nginx_2
make
make install
B)高級安裝
如果添加對網站狀態監控的功能,所需要做的工作如下
1)        安裝pcre包
tar zxvf pcre-7.2.tar.gz
cd pcre-7.2
./configure
make
make install
2)        安裝nginx
./configure –prefix=/usr/local/nginx_2 --with-http_stub_status_module
make
make install
狀態頁面

更多安裝選項可參考網站http://wiki.codemongers.com/NginxInstallOptions
《解決方案》

二、基本命令
nginx –v 顯示nginx的版本
# ./nginx -v
nginx version: nginx/0.5.34
nginx –V 顯示nginx的版本,編譯器和配置參數
# ./nginx -V
nginx version: nginx/0.5.34
built by gcc 3.4.6 20060404 (Red Hat 3.4.6-3)
configure arguments: --prefix=/usr/local/nginx_2 --with-http_stub_status_module
nginx –t 不運行,僅僅測試配置文件
nginx –c </path/to/config>為nginx指定配置文件
# ./nginx -t -c /usr/local/nginx_2/conf/nginx_status.conf
2008/01/26 18:34:40 4242#0: the configuration file /usr/local/nginx_2/conf/nginx_status.conf syntax is ok
2008/01/26 18:34:40 4242#0: the configuration file /usr/local/nginx_2/conf/nginx_status.conf was tested successfully

啟動nginx
/usr/local/nginx_2/sbin/nginx –c /usr/local/nginx_2/conf/nginx_status.conf
註:如果不指定配置文件,系統會按照Nginx安裝目錄下conf/nginx.conf的配置啟動。
退出nginx
   kill –QUIT nginx_pid
《解決方案》

三、配置文件
Nginx涉及到的配置文件
koi-utf  koi-win  mime.types  mime.types.default  nginx.conf  nginx.conf.default  win-utf
其中主配置文件是nginx.conf,主要的配置工作,都在這裡進行
mime.types文件規定文件的類型
A)配置文件解釋
nginx.conf文件
user          www        users;  規定用戶、用戶組
worker_processes 3; 規定工作進程數 默認是1
error_log logs/error.log;記錄日誌文件
pid        logs/nginx.pid; 主進程的進程號
events {
    use epoll;     規定使用的I/O復用模式
    worker_connections 1024; 規定進程的連接數 默認是1024
}
        最大客戶端數由worker_processes、worker_connections決定
        Max_client=worker_processes*worker_connections
                在反向代理情況下
                Max_client=worker_processes*worker_connections/4

http {
  include       conf/mime.types;
  default_type  application/octet-stream;
  log_format  main  '$remote_addr - $remote_user [$time_local] $request '
                      '"$status" $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
                       #規定了日誌的文件格式
   access_log  logs/access.log  main;
   keepalive_timeout  65;
    server {
        listen        80;
        server_name localhost;
        location / {
            root   html;
            index index.html index.htm;
        }
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
                              root   html;
                                                        }
    }
}
B)帶監控模塊的配置
################狀態監控######################
user          www        users;
worker_processes 3;
error_log logs/error.log;
pid        logs/nginx.pid;
events {
    use epoll;
    worker_connections 1024;
}
http {
  include       conf/mime.types;
  default_type  application/octet-stream;
  log_format  main  '$remote_addr - $remote_user [$time_local] $request '
                      '"$status" $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  logs/access.log  main;
    keepalive_timeout  65;
    server {
        listen        80;
        server_name localhost;
        location / {
            root   html;
            index index.html index.htm;
        }
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
                                                        }
    }
#######################狀態監控部分############################
server{
  listen 80;
  server_name status.ceshi.com;
  location / {
               stub_status on;
                   access_log off;
             }
       }
}
C)虛擬主機的配置
###############虛擬主機配置文檔#########################
user          www        users;
worker_processes  3;
error_log  logs/error.log;
pid        logs/nginx.pid;
events {
   use epoll;
    worker_connections  1024;
}
http {
  include       conf/mime.types;
  default_type  application/octet-stream;
  log_format  main  '$remote_addr - $remote_user [$time_local] $request '
                      '"$status" $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  logs/access.log  main;
    keepalive_timeout  65;
    server {
        listen       172.20.20.244;
        listen       172.20.20.244:80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
######################虛擬主機部分####################
  server {
        listen      172.20.20.244;
        listen      172.20.20.244:80;
        listen      8000;
        server_name  www.love.com;
        location / {
            root   www/love;
            index  index.html index.htm;
        }
    }
server {
       listen      172.20.20.244;
        listen      172.20.20.244:80;
        listen      9000;
        server_name  www.duzhenhua.com;
        location / {
            root   www/duzhenhua;
            index  index.html index.htm;
        }
}
server {
       listen      172.20.20.244;
        listen      172.20.20.244:80;
        listen      9900;
        server_name  www.du.com;
        location / {
            root   www/du;
            index  index.html index.htm;
        }
   }
}
《解決方案》

五、事件模型介紹--I/O復用方法
     與apache相類,nginx針對不同的操作系統,有不同的事件模型
      A)標準事件模型
       Select、poll屬於標準事件模型,如果當前系統不存在更有效的方法,nginx會選擇select或poll
      B)高效事件模型   
     Kqueue:使用於 FreeBSD 4.1+, OpenBSD 2.9+, NetBSD 2.0 和 MacOS X. 使用雙處理器的MacOS X系統使用kqueue可能會造成內核崩潰。
Epoll: 使用於Linux內核2.6版本及以後的系統。
/dev/poll:使用於 Solaris 7 11/99+, HP/UX 11.22+ (eventport), IRIX 6.5.15+ 和 Tru64 UNIX 5.1A+。
        Eventport:使用於 Solaris 10. 為了防止出現內核崩潰的問題, 有必要安裝安全補丁。
《解決方案》

六、關於LEMP的結構
     A)Linux+Nginx+Mysql+Php
    對於LEMP結構,網路上有人搭建了,整個搭建過程可以參考:
http://blog.s135.com/read.php/314.htm。
    如果考慮將此結構應用到實際項目中,還需要具體測試。
B)其他結構
     我們大部分項目都是採用Linux+Apache+Mysql+Php,可以考慮在前端添加一個Nginx伺服器,由它來處理大量靜態頁面,其它頁面由原來系統處理。

結構圖


流程圖
有人實現過此結構,參考網站:http://blog.kovyrin.net/2006/05/18/nginx-as-reverse-proxy/
《解決方案》

七、軟體的平滑升級
     A)簡單修改配置文件
     當系統管理員需要修改配置文件時,不需要停機可以參考以下過程。
     kill  -HUP  Nginx_root_PID
   例如:
首先按照需要修改Nginx的配置文件,然後使用-t參數測試配置文件的正確性,一切驗證無誤,最後執行新的配置文件。
# ps -ef|grep nginx
root      4258     1  0 16:12 ?        00:00:00 nginx: master process ./nginx
www       4259  4258  0 16:12 ?        00:00:00 nginx: worker process
www       4260  4258  0 16:12 ?        00:00:00 nginx: worker process
www       4261  4258  0 16:12 ?        00:00:00 nginx: worker process
root      4264  4220  0 16:12 pts/0    00:00:00 grep nginx
# kill -HUP 4258
# ps -ef|grep nginx
root      4258     1  0 16:12 ?        00:00:00 nginx: master process ./nginx
www       4265  4258  0 16:14 ?        00:00:00 nginx: worker process
www       4266  4258  0 16:14 ?        00:00:00 nginx: worker process
www       4267  4258  0 16:14 ?        00:00:00 nginx: worker process
root      4269  4220  0 16:14 pts/0    00:00:00 grep nginx
     B)平滑升級二進位代碼
過程分析
       首先,使用新的可執行程序替換舊的,然後,發送USR2(kill –USR2 ROOT_PID)信號給主進程,主進程將重新命名它的.pid文件為nginx.pid..oldbin,然後執行新的可執行程序,依次啟動新的主進程和工作進程。
          在這時,兩個nginx實例會同時運行,一起處理輸入的請求。需要逐步停止舊的實例,此時需要發送WINCH信號給就的主進程,然後它的工作進程開始關閉。
                  一段時間后,舊的工作進程處理了所有已連接的請求後退出,就僅由新的工作進程來處理輸入的請求。
          這時,因為舊的伺服器還尚未關閉它監聽的套接字,所以通過下面幾步仍然可以恢復舊的伺服器。
a)        發送HUP信號給舊的主進程,它將在不重載配置文件的情況下啟動它的工作進程。
b)        發送QUIT信號給新的主進程,要求其從容關閉其工作進程
c)        發送TERM信號給新的主進程,迫使其退出
d)        如果新的工作進程不退,可以發送kill命令
e)        新的主進程退出后,會恢復nginx.pid.oldbin為nginx.pid。一切照舊。
           如果升級成功,發送QUIT給舊的主進程。
《解決方案》

八、相關的處理信號
A)主進程可以處理的信號
   TERM ,INT            快速關閉
    QUIT                        從容關閉
        HUP                                重載配置,用新的配置開始新的進程,從容關閉舊的工作進程
USR1                        重新打開日誌文件
USR2                         平滑升級可執行程序
WINCH                        從容關閉工作進程
B)工作進程可以處理的信號
TERM ,INT                         快速關閉
QUIT                                從容關閉
USR1                                重新打開日誌文件


參考網站:
http://wiki.codemongers.com/Main
《解決方案》

自己整理的資料,感興趣的朋友可以參考一下^_^
《解決方案》

不錯, 不錯, 樓主辛苦了!
頭像也比較有個性, 順便問下那是不是 nginx 的作者?

[ 本帖最後由 newzy 於 2008-1-31 15:32 編輯 ]

[火星人 ] NGINX 介紹已經有920次圍觀

http://coctec.com/docs/service/show-post-32590.html