nginx簡介及特性
1.)nginx是一個高性能的web服務器和反向代理服務器,也是一個郵件代理服務器。具有高併發,開銷小的特性。性能穩定,採用異步I/O處理機制。配置簡潔。
2.)nginx特性:
基本功能:
靜態資源的web服務器,能緩存打開的文件描述符
反向代理服務器,緩存、負載均衡
支持FastCGI
模塊化,非DSO機制,過濾器gzip,SSI和圖像大小調整等
支持SSL
擴展功能:
基於名稱和IP做虛擬主機
支持keepalive
支持平滑配置更新或程序版本升級
定製訪問日誌,支持使用日誌緩存以提高性能
支持url rewrite
支持路徑別名
支持基於IP及用戶的認證
支持速率限制,併發限制等
1.)nginx編譯安裝
安裝nginx準備工作
創建nginx userand group
# useradd -r -s /sbin/nologin nginx
安裝nginx sslproxy 需要的開發依賴包
# yum installopenssl-devel pcre-devel
解壓nginx
tar xf nginx-1.6.1.tar.gz
編譯安裝
# ./configure--prefix=/usr/local/nginx #指定nginx安裝目錄 --sbin-path=/usr/sbin/nginx #指定nginx管理命令目錄,也可以不知道而後連接到/usr/sbin下 --conf-path=/etc/nginx/nginx.conf #指定nginx主配置文件存放位置 --error-log-path=/var/log/nginx/error.log #指定nginx錯誤日誌存放位置 --http-log-path=/var/log/nginx/access.log #指定nginx訪問日誌存放位置 --pid-path=/var/run/nginx/nginx.pid #指定nginx的PID存放位置 --lock-path=/var/lock/nginx.lock #指定nginx鎖文件存放位置 --user=nginx #指定nginx的啓動用戶 --group=nginx #指定nginx的用戶組 --with-http_ssl_module #指定nginx支持ssl模塊 --with-http_flv_module #指定nginx流媒體模塊 --with-http_stub_status_module #開啓nginx的狀態查看模塊 --with-http_gzip_static_module #開啓nginx壓縮功能模塊 --http-client-body-temp-path=/var/tmp/nginx/client #指定客戶端請求主體臨時存放目錄 --http-proxy-temp-path=/var/tmp/nginx/proxy #指定客戶端請求數據,而後端服務器先加載到nginx代理服務器的目錄 --http-fastcgi-temp-path=/var/tmp/nginx/fastcgi #指定客戶端請求的CGI相關的緩存存放路徑 --http-uwsgi-temp-path==/var/tmp/nginx/uwsgi #指定客戶端請求python開發相關頁面的緩存路徑 --http-scgi-temp-path=/var/tmp/nginx/scgi #scgi,對cgi的擴展 --with-pcre #指定代理模塊 # make &&make install 註釋:nginx是異步服務器,如果客戶端上傳一個數據,並且nginx作爲代理服務器,需要先將客戶端上次的數據保存到nginx的指定存放路徑,等客戶端上傳完成後才交給後端服務器。如果客戶端下載一個文件,也需要在後端服務器加載到前端nginx代理服務器後才響應給客戶。也就是爲什麼要指定上面幾個temp-path的原因。
2.)創建編譯安裝時指定/var/tmp/nginx這個目錄
mkdir -pv/var/tmp/nginx/
3.)啓動nginx,檢查端口
# nginx
4.)輸入IP地址,訪問測試頁
5.)nginx編譯安裝的時候是沒有提供啓動控制腳本的
# /usr/sbin/nginx #啓動nginx # kill `cat/var/run/nginx/nginx.pid` #停止nginx # kill -HUP `cat/var/run/nginx/nginx.pid` #平滑重啓nginx # nginx -t -c/etc/nginx/nginx.conf #檢查nginx配置文件
6.)提供Sys風格啓動控制腳本,加入開機啓動。腳本的提供是由rpm軟件包安裝生成的,需要修改一些參數就可以使用
# chkconfig --addnginx # chkconfig nginxon # chkconfig --listnginx nginx 0:off 1:off 2:on 3:on 4:on 5:on 6:off
7.)正常情況下,nginx配置文件是沒有語法高亮的,如果需要可以下載nginx.vim來修改
下載地址:http://www.vim.org/scripts/script.php?script_id=1886
# mkdir -pv.vim/syntax mkdir: createddirectory `.vim' mkdir: createddirectory `.vim/syntax' # cp nginx.vim~/.vim/syntax/ # vim~/.vim/filetype.vim auBufRead,BufNewFile /etc/nginx/* if &ft == '' | setfiletype nginx | endif
這次nginx.conf裏面就是高亮顯示的了。
8.)nginx配置文件
全局配置段 events{ … } http{ … } ################################################################ 全局配置段默認設置。 user nginx; #nginx的啓動用戶 worker_processes 1; #worker的進程數,一般是CPU的物理核心數 #error_log logs/error.log; # 錯誤日誌 #error_log logs/error.log notice; #日誌級別 #error_log logs/error.log info; pid /var/run/nginx/nginx.pid; # nginx進程的PID worker_rlimit_nofile65535; # worker進程可打開的文件數 worker_rlimit_sigpending # 設定每個用戶能夠發往worker進程信號隊列的數量 #ssl_enginedevice; # 在存在ssl硬件加速器上的服務器,指定鎖使用的ssl硬件加速設備 #timer_resolutiontime; # 每次內核事件調用返回時,都會使用gettimeofday()來更新nginx緩存時鐘,time_resolution用於定義每隔多久纔會更新一次緩存時鐘。 worker_priority-10; # 指定worker進程的nice值,值越小調度就會越優先 daemon on; # 是否開啓nginx調試功能,默認爲on,off後可以將所有信息輸出到控制檯。 master_process on; # 是否以master/worker模式運行nginx,默認爲on,調試時可以設置off以方便追蹤。
9.)nginx提供了可以綁定進程運行在指定CPU上,就是所謂的CPU親緣性。這是屬於nginx優化的一部分,儘可能的避免進行來回活動在其他的核心上
查看主機CPU的核心數
# cat /proc/cpuinfo| grep 'processor' processor : 0 processor : 1 processor : 2 processor : 3
在配置文件中綁定CPU。全局配置段
worker_cpu_affinity0001 0010 0100;
查看nginx綁定在哪個CPU上
# ps -eopid,args,psr | grep nginx 1449 nginx: master process /usr/ 0 1450 nginx: worker process 0 1451 nginx: worker process 1 1452 nginx: worker process 2
可以看下我做的測試
下面是沒綁定的時候nginx進程運行在哪個CPU上
下面是我綁定CPU後的測試
10.)events配置段
events{ #accept_mutex on; # 是否打開nginx負載均衡鎖,此鎖能夠讓多個worker進程輪流序列化的與新的客戶端連接。功能默認是打開的。 #lock_file /path/to/lock.file; # lock文件 #accept_mutex_delay#ns; # 一個worker進程爲取得accept鎖的等待時長 multi_sccept on; # 是否允許一次性響應多個用戶請求,默認爲off。 woker_connections ; #每個worker最大併發響應最大請求數,如果爲代理服務器,應該是worker_rlimit_nofile數值的2倍。 } http{ … server { # 定義一個虛擬主機,nginx支持使用基於域名IP的虛擬主機。 listen 80; #nginx監聽的端口 server_name localhost; #定義主機名,可以跟多個主機名。當nginx收到一個請求後,會取出首部的server值,而後跟其他server_name進行比較 #比較方式: #精確匹配:www.test.com #左側通配符匹配:*.test.com #右側通配符匹配:www.* #正則表達式匹配:~^.*\.test\.com$ server_name_hash_bucket_size 64; # 爲了實現快速查找,nginx使用hash表來保存主機名 #charset koi8-r; #access_log logs/host.access.log main; #location匹配設置,根據用戶請求指定URI來匹配指定的location以進行匹配 #= :精確匹配,如果匹配到,則退出匹配。 #~ :正則表達式匹配模式,匹配時區分字符大小寫 #~*:正則表達式匹配模式,匹配時忽略大小寫 #^~: URI的前半部分匹配,禁止正則表達式 location / { root html; # 文件路徑定義 # root html : 設置web資源路徑,用戶指定請求的網頁存放路徑 index index.html index.htm; # 定義默認頁面 } #error_page 404 /404.html; # 錯誤頁面重定向 #try_file $uri paht2 : 自左向右讀取path所指定路徑,在第一次找到即停止並返回,如果path不存在,則返回最後一個uri # redirect server error pages to thestatic page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; }… } 定義基於IP的虛擬主機 server { listen 80; server_name 192.168.122.135; location / { root html/web1; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 80; server_name 192.168.122.136; location / { root html/web2; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
11.)創建網站跟目錄
# mkdir /usr/local/nginx/html/{web1,web2} # vim/usr/local/nginx/html/web1/index.html # vim/usr/local/nginx/html/web2/index.html # ifconfig eth1:0192.168.122.136/24 # ifconfig | grep'inet addr' inetaddr:192.168.122.135 Bcast:192.168.122.255 Mask:255.255.255.0 inetaddr:192.168.122.136 Bcast:192.168.122.255 Mask:255.255.255.0
測試訪問
12.)location 匹配設置
server { listen 80; server_name 192.168.122.135; location / { root html/web1; index index.html index.htm; } location ^~/images/ { root html/web3; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } # mkdir/usr/local/nginx/html/web3 # vim/usr/local/nginx/html/web3/index.html
# mkdir/usr/local/nginx/html/web3/images # cp/usr/local/nginx/html/web3/index.html /usr/local/nginx/html/web3/images/
第一個輸入192.168.133.135/images沒有匹配到是因爲在web3下沒有images這個目錄,而roothtml/web3下指明瞭是應該訪問的html/web3/images下的內容
13.)目錄文件別名
location^~/images/ { alias html/web3; index index.html index.htm; }
# cp/usr/local/nginx/html/web3/images/index.html /usr/local/nginx/html/web3/
14.)錯誤頁面重定向
server { listen 80; server_name 192.168.122.135; error_page 404 /404.html; #error_page 404 =200 /404.html; … } # vim/usr/local/nginx/html/web1/404.html
15.)頁面不存在,定向到首頁去
location /newweb { root html/web3; try_files $uri /index.html; } # mkdir /usr/local/nginx/html/web3/newweb
16.)訪問控制,我的無線網絡是172網段的,虛擬機是192網段的,就禁止172網段進行訪問
server { listen 80; server_name 192.168.122.135; allow 172.16.0.0/16; deny all; … }
17.)性能監控模塊status
server { location /status { stub_status on; access_log off; allow all; #deny all; } }
18.)用戶認證模塊
server { … location /status { stub_status on; access_log off; auth_basic "nginxstatus"; auth_basic_user_file/etc/nginx/.nginxpass; } … } # htpasswd -c -m/etc/nginx/.nginxpass admin
19.)防盜鏈
server { location ~* \.(jpg|png|gif)$ { root html/web2; valid_referers none blockedwww.a.com *.a.com; if ($invalid_referer) { rewrite ^/http://www.a.com/403.html; } }
上面的主機名已經不再是基於IP的虛擬主機,而是修改成了www.a.com
通過web1的主機訪問測使用圖片
通過www.a.com使用圖片
日誌已經顯示給重定向了
20.)URL地址重寫 rewrite
last: 一旦被當前規則匹配並重寫後立即停止檢查後續的其它rewrite的規則,而後通過重寫後的規則重新發起請求 break: 一旦被當前規則匹配並重寫後立即停止後續的其它rewrite的規則,而後繼續由nginx進行後續操作 redirect: 返回302臨時重定向 permanent: 返回301永久重定向
# mkdir/usr/local/nginx/html/web2/{jpg,images} # mv 123.jpg/usr/local/nginx/html/web2/images/ server{ … location /jpg/ { root html/web2; rewrite ^/jpg/(.*\.(jpg|gif|png))$/images/$1 last; } … }
正常的訪問也是可以進行的
總結:基本配置只是做出了一部分示例,還有很多功能和參數沒有介紹,官方網站上也是有中文的nginx的文檔