一、服務安裝與啟停:
windows:
nginx -s [ stop | quit | reopen | reload ]
start?nginx ?//啟動服務
nginx -s stop //停止服務
nginx -s reload //重裝配置文件
nginx -s reopen //打開日志
tasklist /fi "imagename eq nginx.exe" ?//顯示服務進程
Linux:
cd nginx-1.2.0
./configure
make
sudo make install
ps aux|grep nginx //顯示服務進程
在Linux下安裝Nginx詳細說明
為了確保能在Nginx中使用正則表達式進行更靈活的配置,安裝之前需要確定系統是否安裝有 PCRE(Perl Compatible Regular Expressions)包。可以到ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/?下載最新的PCRE源碼包,使用下面命令下載編譯和安裝PCRE包:
# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-7.7.tar.gz?
# tar zxvf pcre-7.7.tar.gz # cd pcre-7.7 # ./configure?
# make?
# make install
接下來安裝Nginx,Nginx一般有兩個版本,分別是穩定版和開發版,可以根據您的目的來選擇這兩個版本的其中一個,下面是把 Nginx 安裝到 /opt/nginx 目錄下的詳細步驟:
# wget http://sysoev.ru/nginx/nginx-0.6.31.tar.gz
# tar zxvf nginx-0.6.31.tar.gz # cd nginx-0.6.31
# ./configure --with-http_stub_status_module –prefix=/opt/nginx
# make # make install
其中參數--with-http_stub_status_module是為了啟用nginx的NginxStatus功能,用來監控Nginx的當前狀態。
? ? 安裝成功后/opt/nginx目錄下有四個子目錄分別是:conf、html、logs、sbin 。其中Nginx的配置文件存放于conf/nginx.conf,Nginx只有一個程序文件位于sbin目錄下的nginx文件。確保系統的80端口沒被其他程序占用,運行sbin/nginx命令來啟動Nginx,打開瀏覽器訪問此機器的IP,如果瀏覽器出現Welcome to nginx! 則表示Nginx已經安裝并運行成功。
二、常用的Nginx參數和控制
Nginx安裝后只有一個程序文件,本身并不提供各種管理程序,它是使用參數和系統信號機制對 Nginx進程本身進行控制的。?
通過程序運行參數控制:
Nginx的參數包括有如下幾個:
-c?<path_to_config>:使用指定的配置文件而不是conf目錄下的nginx.conf 。
-t:測試配置文件是否正確,在運行時需要重新加載配置的時候,此命令非常重要,用來檢測所修改的配置文件是否有語法錯誤。
-v:顯示nginx版本號。
-V:顯示nginx的版本號以及編譯環境信息以及編譯時的參數。
例如我們要測試某個配置文件是否書寫正確,可以使用以下命令:
sbin/nginx -t -c? conf/nginx2.conf
通過信號對Nginx進行控制:
Nginx 支持下表中的信號:
信號名????????????????作用描述
TERM, INT? ? ? ? ?快速關閉程序,中止當前正在處理的請求
QUIT? ? ? ? ? ? ? ? ? ?處理完當前請求后,關閉程序
HUP????????????????????重新加載配置,并開啟新的工作進程,關閉就的進程,此操作不會中斷請求
USR1? ? ? ? ? ? ? ? ? 重新打開日志文件,用于切換日志,例如每天生成一個新的日志文件
USR2? ? ? ? ? ? ? ? ? 平滑升級可執行程序
WINCH????????????????從容關閉工作進程
有兩種方式來通過這些信號去控制Nginx:
第一是通過logs目錄下的nginx.pid查看當前運行的Nginx的進程ID,通過kill -XXX <pid>來控制Nginx,其中XXX就是上表中列出的信號名。
如果系統中只有一個Nginx進程,那您也可以通過killall命令來完成,例如運行killall -s HUP nginx來讓Nginx重新加載配置。
三、配置Nginx
先來看一個實際的配置文件:
user nobody;
# 工作進程的屬主?
worker_processes 4;
# 工作進程數,一般與 CPU 核數等同?
#error_log logs/error.log;?
#error_log logs/error.log notice;?
#error_log logs/error.log info;?
#pid logs/nginx.pid;
events {?
? ? use epoll;#Linux 下性能最好的event模式?
? ? worker_connections 2048;# 每個工作進程允許最大的同時連接數?
}?
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" \''?
? ? # \''"$http_user_agent" "$http_x_forwarded_for"\'';?
? ? #access_log off;?
? ? access_log logs/access.log;# 日志文件名?
? ? sendfile on;?
? ? #tcp_nopush on;?
? ? tcp_nodelay on;?
? ? keepalive_timeout 65;?
? ? include gzip.conf; # 集群中的所有后臺服務器的配置信息?
? ? upstream tomcats {?
? ? ? ? server 192.168.0.11:8080 weight=10;?
? ? ? ? server 192.168.0.11:8081 weight=10;?
? ? ? ? server 192.168.0.12:8080 weight=10;?
? ? ? ? server 192.168.0.12:8081 weight=10;?
? ? ? ? server 192.168.0.13:8080 weight=10;?
? ? ? ? server 192.168.0.13:8081 weight=10;?
? ? }?
? ? server {?
? ? ? ? listen 80;#HTTP 的端口?
? ? ? ? server_name localhost;?
? ? ? ? charset utf-8;?
? ? ? ? #access_log logs/host.access.log main;?
? ? ? ? location ~ ^/NginxStatus/ {?
? ? ? ? ? ? stub_status on; #Nginx 狀態監控配置?
? ? ? ? ? ? access_log off;?
? ? ? ? }?
? ? ? ? location ~ ^/(WEB-INF)/ {?deny all;?}?
? ? ? ? location ~ \\.(htm|html|asp|php|gif|jpg|jpeg|png|bmp|ico|rar|css|js| zip|java|jar|txt|flv|swf|mid|doc|ppt|xls|pdf|txt|mp3|wma)$ {?
? ? ? ? ? ? root /opt/webapp;?
? ? ? ? ? ? expires 24h;?
? ? ? ? }?
? ? ? ? location / {?
? ? ? ? ? ? proxy_pass http://tomcats;# 反向代理?
? ? ? ? ? ? include proxy.conf;?
? ? ? ? }?
? ? ? ? error_page 404 /html/404.html;?
? ? ? ? # redirect server error pages to the static page /50x.html?
? ? ? ? # error_page 502 503 /html/502.html;?
? ? ? ? error_page 500 504 /50x.html;?
? ? ? ? location = /50x.html {?root html;?}?
? ? }?
}
Nginx監控:
上述配置中,首先我們定義了一個location ~ ^/NginxStatus/,這樣通過http://localhost/NginxStatus/ 就可以監控到 Nginx 的運行信息,顯示的內容如下:
Active connections: 70 server accepts handled requests 14553819 14553819 19239266 Reading: 0 Writing: 3 Waiting: 67
NginxStatus顯示的內容意思如下:
????active connections? – 當前 Nginx 正處理的活動連接數。
????server accepts handled requests -- 總共處理了 14553819 個連接 , 成功創建 14553819 次握手 ( 證明中間沒有失敗的 ), 總共處理了 19239266 個請求 ( 平均每次握手處理了 1.3 個數據請求 )。
????reading -- nginx 讀取到客戶端的 Header 信息數。
????writing -- nginx 返回給客戶端的 Header 信息數。
????waiting -- 開啟 keep-alive 的情況下,這個值等于 active - (reading + writing),意思就是 Nginx已經處理完正在等候下一次請求指令的駐留連接。
靜態文件處理:
通過正則表達式,我們可讓Nginx識別出各種靜態文件,例如images路徑下的所有請求可以寫為:
location ~ ^/images/? { root /opt/webapp/images; }
而下面的配置則定義了幾種文件類型的請求處理方式。
location ~ \\.(htm|html|gif|jpg|jpeg|png|bmp|ico|css|js|txt)$ {?
? ? root /opt/webapp;?
? ? expires? 24h;?
}
對于例如圖片、靜態HTML文件、js腳本文件和css樣式文件等,我們希望Nginx直接處理并返回給瀏覽器,這樣可以大大加快網頁瀏覽時的速度。因此對于這類文件我們需要通過root指令來指定文件的存放路徑,同時因為這類文件并不常修改,通過expires指令來控制其在瀏覽器的緩存,以減少不必要的請求。expires指令可以控制HTTP應答中的“Expires”和“Cache-Control”的頭標(起到控制頁面緩存的作用)。可以使用以下的格式來書寫Expires:
expires 1 January, 1970, 00:00:01 GMT; expires 60s; expires 30m; expires 24h; expires 1d; expires max; expires off;
動態頁面請求處理:
? ? Nginx本身并不支持現在流行的JSP、ASP、PHP、PERL等動態頁面,但它可以通過反向代理將請求發送到后端的服務器,例如Tomcat、Apache、IIS等來完成動態頁面的請求處理。前面的配置示例中,我們首先定義了由Nginx直接處理的一些靜態文件請求后,其他所有的請求通過 proxy_pass 指令傳送給后端的服務器。最簡單的?proxy_pass?用法如下:
location / {
? ? proxy_pass? ? http://localhost:8080;
? ? proxy_set_header X-Real-IP $remote_addr;
}
這里沒有使用到集群,而是將請求直接送到運行在8080端口的Tomcat服務上來完成類似JSP和Servlet的請求處理。
當頁面的訪問量非常大的時候,往往需要多個應用服務器來共同承擔動態頁面的執行操作,這時我們就需要使用集群的架構。Nginx通過upstream指令來定義一個服務器的集群,最前面那個完整的例子中我們定義了一個名為tomcats的集群,這個集群中包括了三臺服務器共6個Tomcat 服務。而proxy_pass指令的寫法變成了:
location? / {
????proxy_pass? http://tomcats;
? ? proxy_set_header X-Real-IP? $remote_addr;
}
在Nginx的集群配置中,Nginx使用最簡單的平均分配規則給集群中的每個節點分配請求。一旦某個節點失效時,或者重新起效時,Nginx都會非常及時的處理狀態的變化,以保證不會影響到用戶的訪問。
四、常用命令說明
1、Location
Location 指令,是用來為匹配的 URI 進行配置,URI 即語法中的"/uri/",可以是字符串或正則表達式。但如果要使用正則表達式,則必須指定前綴。
1)基本語法
location ?[=|~|~*|^~|@] ?/uri/ ?{...}
= ? ? 表示精確匹配,如果找到,立即停止搜索并立即處理此請求。
~ ? ? 表示區分大小寫匹配
~* ? 表示不區分大小寫匹配
^~ ?表示只匹配字符串,不查詢正則表達式。
@ ? ?指定一個命名的location,一般只用于內部重定向請求。
2)匹配過程
首先對字符串進行匹配查詢,最確切的匹配將被使用。然后,正則表達式的匹配查詢開始,匹配第一個結果后會停止搜索,如果沒有找到正則表達式,將使用字符串的搜索結果,如果字符串和正則都匹配,那么正則優先級較高。
3)配置實例
location = / {
? ? ????#只匹配對 / 目錄的查詢
? ????? [ config ?A ] ?
}
location ? ?/ ?{
? ? ? ??# 匹配以 / 開始的查詢,即所有查詢都匹配。
? ? ? ??[ config ?B ]?
}
location ?^~ /images/ {
? ? ? ??# 匹配以 /images/ 開始的查詢,不再檢查正則表達式。
? ????? [ config ?C ]?
}
location ~* \.(gif|jpg|jpeg)$ ?{
????????# 匹配以gif, jpg, or jpeg結尾的文件,但優先級低于config C。
????????[ config ?D ]?
}
4)全局變量
$args ? ? ? ? ? ? ? ? ? ? ? ? #這個變量等于請求行中的參數。
$content_length ? ? ? #請求頭中的Content-length字段。
$content_type ? ? ? ? ? #請求頭中的Content-Type字段。
$document_root ? ? ? #當前請求在root指令中指定的值。
$host ? ? ? ? ? ? ? ? ? ? ? ? #請求主機頭字段,否則為服務器名稱。
$http_user_agent ? ? ?#客戶端agent信息。
$http_cookie ? ? ? ? ? ? #客戶端cookie信息。
$limit_rate ? ? ? ? ? ? ? ? #這個變量可以限制連接速率。
$request_body_file ? #客戶端請求主體信息的臨時文件名。
$request_mothod ? ? #客戶端請求的動作,通常為GET或POST。
$remote_addr ? ? ? ? ? #客戶端的IP地址。
$remote_port ? ? ? ? ? ?#客戶端的端口。
$remote_user ? ? ? ? ? ?#已經經過Auth Basic Module驗證的用戶名。
$request_filename ? ?#當前請求的文件路徑,由root或alias指令與URI請求生成。
$query_string ? ? ? ? ? ?#與$args相同。
$scheme ? ? ? ? ? ? ? ? ? ?#HTTP方法(如http,https)。
$server_protocol ? ? ? #請求使用的協議,通常是HTTP/1.0或HTTP/1.1。
$server_addr ? ? ? ? ? ? #服務器地址,在完成一次系統調用后可以確定這個值。
$server_name ? ? ? ? ? #服務器名稱。
$server_port ? ? ? ? ? ? #請求到達服務器的端口號。
$request_uri ? ? ? ? ? ? #包含請求參數的原始URI,不包含主機名,如“/foo/bar.php?arg=baz”。
$uri ? ? ? ? ? ? ? ? ? ? ? ? ? #不帶請求參數的當前URI,不包含主機名,如“/foo/var.html”。
$document_uri ? ? ? ?#與$uri相同。
2、upstream(負載均衡)
把www.domain.com均衡到本機不同的端口,也可以改為均衡到不同的地址上。
http ?{
? ? upstream myproject {
? ? ? ? server ?127.0.0.1:8000 ? weight=3;
? ? ? ? server ?127.0.0.1:8001;
? ? ? ??server ?127.0.0.1:8002;
? ? ? ? server ?127.0.0.1:8003;
? ? }
? ? server {
? ? ? ? listen 80;
? ? ? ? server_name ?www.domain.com;
? ? ? ? location / {
? ? ? ? ? ? proxy_pass ?http://myproject;
? ? ? ? }
? ? }
}
nginx的upstream目前支持以下幾種方式的分配:
1)輪詢(默認):每個請求按時間順序逐一分配到不同的后端服務器,如果后端服務器down掉,能自動剔除。
2)weight:指定輪詢幾率,weight和訪問比率成正比,用于后端服務器性能不均的情況。好的服務器weight高些,差的服務器weight低些。
upstream ?bakend {
? ? server ?127.0.0.1:8000 ? weight=3;
}
3)ip_hash:每個請求按訪問的IP和hash結果分配,這樣每個訪客固定訪問一個后端服務器,可以解決session的問題。無法將權重(weight)與ip_hash聯合起來分發連接。如果有某臺服務器不可用,必須標記其為down。
upstream ?bakend {
? ? ip_hash;
? ? server ? 192.168.0.14:80;
? ? server ? 192.168.0.15:80;?
}
4)fair(第三方):按后端服務器的響應時間來分配請求,響應時間短的優先分配。
upstream ?bakend ?{
? ? server ?server1;
? ? server ?server2;
? ? fair;
}
5)url_hash(第三方):
upstream ?bakend {
? ? ip_hash;
? ? server ?127.0.0.1:8000 ? down;
? ??server ?127.0.0.1:8001 ? weight=2;
? ? server ?127.0.0.1:8002;
? ? server ?127.0.0.1:8003 ? backup;
}
在需要使用負載均衡的server中增加proxy_pass http://bakend/;
每個設備的狀態設置為:
1>down:表示當前的server暫時不參與負載。
2>weight:權重默認為1,weight越大,負載的權重就越大。
3>max_fails:允許請求失敗的次數默認為1,當超過最大次數時,返回proxy_next_upstream模塊定義的錯誤。
4>fail_timeout:max_fails次失敗后,暫停的時間。
5>backup:備用,其他所有的非backup機器down或者忙的時候,請求backup機器。所以這臺機器壓力會最輕。
五、編譯優化
默認nginx使用的GCC編譯參數是-O,需要更加優化可以使用以下兩個參數:
–with-cc-opt='-O3′?
–with-cpu-opt=opteron?
使得編譯針對特定CPU以及增加GCC的優化;
六、configure參數詳解
源代碼解壓后即可輸入./configure –help 查看所有configure參數;
具體參數解釋如下:
–prefix=<path> – Nginx安裝路徑。如果沒有指定,默認為 /usr/local/nginx。?
–sbin-path=<path> – Nginx可執行文件安裝路徑。只能安裝時指定,如果沒有指定,默認為<prefix>/sbin/nginx。?
–conf-path=<path> – 在沒有給定-c選項下默認的nginx.conf的路徑。如果沒有指定,默認為<prefix>/conf/nginx.conf。?
–pid-path=<path> – 在nginx.conf中沒有指定pid指令的情況下,默認的nginx.pid的路徑。如果沒有指定,默認為 <prefix>/logs/nginx.pid。?
–lock-path=<path> – nginx.lock文件的路徑。?
–error-log-path=<path> – 在nginx.conf中沒有指定error_log指令的情況下,默認的錯誤日志的路徑。如果沒有指定,默認為 <prefix>/logs/error.log。?
–http-log-path=<path> – 在nginx.conf中沒有指定access_log指令的情況下,默認的訪問日志的路徑。如果沒有指定,默認為 <prefix>/logs/access.log。?
–user=<user> – 在nginx.conf中沒有指定user指令的情況下,默認的nginx使用的用戶。如果沒有指定,默認為 nobody。?
–group=<group> – 在nginx.conf中沒有指定user指令的情況下,默認的nginx使用的組。如果沒有指定,默認為 nobody。?
–builddir=DIR – 指定編譯的目錄?
–with-rtsig_module – 啟用 rtsig 模塊?
–with-select_module –without-select_module – 允許或不允許開啟SELECT模式,如果 configure 沒有找到更合適的模式,比如:kqueue(sun os),epoll (linux kenel 2.6+), rtsig(實時信號)或者/dev/poll(一種類似select的模式,底層實現與SELECT基本相 同,都是采用輪訓方法) SELECT模式將是默認安裝模式?
–with-poll_module –without-poll_module – Whether or not to enable the poll module. This module is enabled by default if a more suitable method such as kqueue, epoll, rtsig or /dev/poll is not discovered by configure.?
–with-http_ssl_module – 開啟HTTP SSL模塊,使NGINX可以支持HTTPS請求。這個模塊需要已經安裝了OPENSSL,在DEBIAN上是libssl?
–with-http_realip_module – 啟用 ngx_http_realip_module?
–with-http_addition_module – 啟用 ngx_http_addition_module?
–with-http_sub_module – 啟用 ngx_http_sub_module?
–with-http_dav_module – 啟用 ngx_http_dav_module?
–with-http_flv_module – 啟用 ngx_http_flv_module?
–with-http_stub_status_module – 啟用 “server status” 頁?
–without-http_charset_module – 禁用 ngx_http_charset_module?
–without-http_gzip_module – 禁用 ngx_http_gzip_module. 如果啟用,需要 zlib 。?
–without-http_ssi_module – 禁用 ngx_http_ssi_module?
–without-http_userid_module – 禁用 ngx_http_userid_module?
–without-http_access_module – 禁用 ngx_http_access_module?
–without-http_auth_basic_module – 禁用 ngx_http_auth_basic_module?
–without-http_autoindex_module – 禁用 ngx_http_autoindex_module?
–without-http_geo_module – 禁用 ngx_http_geo_module?
–without-http_map_module – 禁用 ngx_http_map_module?
–without-http_referer_module – 禁用 ngx_http_referer_module?
–without-http_rewrite_module – 禁用 ngx_http_rewrite_module. 如果啟用需要 PCRE 。?
–without-http_proxy_module – 禁用 ngx_http_proxy_module?
–without-http_fastcgi_module – 禁用 ngx_http_fastcgi_module?
–without-http_memcached_module – 禁用 ngx_http_memcached_module?
–without-http_limit_zone_module – 禁用 ngx_http_limit_zone_module?
–without-http_empty_gif_module – 禁用 ngx_http_empty_gif_module?
–without-http_browser_module – 禁用 ngx_http_browser_module?
–without-http_upstream_ip_hash_module – 禁用 ngx_http_upstream_ip_hash_module?
–with-http_perl_module – 啟用 ngx_http_perl_module?
–with-perl_modules_path=PATH – 指定 perl 模塊的路徑?
–with-perl=PATH – 指定 perl 執行文件的路徑?
–http-log-path=PATH – Set path to the http access log?
–http-client-body-temp-path=PATH – Set path to the http client request body temporary files?
–http-proxy-temp-path=PATH – Set path to the http proxy temporary files?
–http-fastcgi-temp-path=PATH – Set path to the http fastcgi temporary files?
–without-http – 禁用 HTTP server?
–with-mail – 啟用 IMAP4/POP3/SMTP 代理模塊?
–with-mail_ssl_module – 啟用 ngx_mail_ssl_module?
–with-cc=PATH – 指定 C 編譯器的路徑?
–with-cpp=PATH – 指定 C 預處理器的路徑?
–with-cc-opt=OPTIONS – Additional parameters which will be added to the variable CFLAGS. With the use of the system library PCRE in FreeBSD, it is necessary to indicate –with-cc-opt=”-I /usr/local/include”. If we are using select() and it is necessary to increase the number of file descriptors, then this also can be assigned here: –with-cc-opt=”-D FD_SETSIZE=2048″.?
–with-ld-opt=OPTIONS – Additional parameters passed to the linker. With the use of the system library PCRE in FreeBSD, it is necessary to indicate –with-ld-opt=”-L /usr/local/lib”.?
–with-cpu-opt=CPU – 為特定的 CPU 編譯,有效的值包括:pentium, pentiumpro, pentium3, pentium4, athlon, opteron, amd64, sparc32, sparc64, ppc64?
–without-pcre – 禁止 PCRE 庫的使用。同時也會禁止 HTTP rewrite 模塊。在 “location” 配置指令中的正則表達式也需要 PCRE 。?
–with-pcre=DIR – 指定 PCRE 庫的源代碼的路徑。?
–with-pcre-opt=OPTIONS – Set additional options for PCRE building.?
–with-md5=DIR – Set path to md5 library sources.?
–with-md5-opt=OPTIONS – Set additional options for md5 building.?
–with-md5-asm – Use md5 assembler sources.?
–with-sha1=DIR – Set path to sha1 library sources.?
–with-sha1-opt=OPTIONS – Set additional options for sha1 building.?
–with-sha1-asm – Use sha1 assembler sources.?
–with-zlib=DIR – Set path to zlib library sources.?
–with-zlib-opt=OPTIONS – Set additional options for zlib building.?
–with-zlib-asm=CPU – Use zlib assembler sources optimized for specified CPU, valid values are: pentium, pentiumpro?
–with-openssl=DIR – Set path to OpenSSL library sources?
–with-openssl-opt=OPTIONS – Set additional options for OpenSSL building?
–with-debug – 啟用調試日志?
–add-module=PATH – Add in a third-party module found in directory PATH?