Nginx由核心和子產品組成,其中,核心的設計非常微小和簡潔,完成的工作也非常簡單,僅僅通過查找配置檔案将用戶端請求映射到一個location block(location是Nginx配置中的一個指令,用于URL比對),而在這個location中所配置的每個指令将會啟動不同的子產品去完成相應的工作。
Nginx相對于Apache優點:
高并發響應性能非常好,官方Nginx處理靜态檔案并發5w/s
反向代理性能非常好。(可用于負載均衡)
記憶體和cpu占用率低。(為Apache的1/5-1/10)
功能較Apache少(常用功能均有)
對php可使用cgi方式和fastcgi方式。
實驗機器:192.168.77.133
首先需要安裝pcre庫,然後再安裝Nginx:
安裝pcre支援rewrite庫,也可以安裝源碼,注*安裝源碼時,指定pcre路徑為解壓
源碼的路徑,而不是編譯後的路徑,否則會報錯
(make[1]: *** [/usr/local/pcre/Makefile] Error 127 錯誤)
yum install pcre-devel pcre -y
#下載下傳Nginx源碼包
#進入解壓目錄,然後sed修改Nginx版本資訊為WS(防止版本資訊被惡意者查到)
cd nginx-1.6.3 ; sed -i -e 's/1.6.3//g' -e 's/nginx\//WS/g' -e
's/"NGINX"/"WS"/g' src/core/nginx.h
#預編譯Nginx
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
#.configure預編譯成功後,執行make指令進行編譯
make
#make執行成功後,執行make install 正式安裝
make install
#自此Nginx安裝完畢
/usr/local/nginx/sbin/nginx -t 檢查nginx配置檔案是否正确,傳回OK即正确。
[root@192_168_77_133 ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
#啟動nginx
root@192_168_77_133 ~]# /usr/local/nginx/sbin/nginx
[root@192_168_77_133 ~]# ps -ef | grep nginx
root 21715 1 0 14:01 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 21716 21715 0 14:01 ? 00:00:00 nginx: worker process
root 21718 20717 0 14:01 pts/2 00:00:00 grep nginx
/usr/local/nginx/sbin/nginx -s stop 停止服務
[root@192_168_77_133 ~]# /usr/local/nginx/sbin/nginx -v 檢視版本
nginx version: WS
[root@192_168_77_133 ~]# /usr/local/nginx/sbin/nginx -V 檢視編譯參數
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-11) (GCC)
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
然後就可一在用戶端通路可以檢視到測試頁面如下:
<a href="http://s3.51cto.com/wyfs02/M01/71/06/wKiom1XDdeGhOktnAABl_3LYx48297.jpg" target="_blank"></a>
#nginx虛拟主機配置
在真實的伺服器環境,為了充分利用伺服器資源,一台nginx web伺服器同時會配置N個虛拟域名主機,即多個域名對于同樣一個80端口。然後伺服器IP數量很多,也可以配置基于多個IP對應同一個端口。
vi修改nginx.conf server段配置内容如下:
server {
listen 88; #端口
location / {
root html/a; #虛拟目錄
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
server {
listen 88; #端口
root html/b; #虛拟目錄
建立兩個不同的目錄mkdir –p /usr/local/nginx/html/{a,b},然後分别在兩個目錄建立兩個不 同的index.html網站頁面即可。通過用戶端配置hosts指向兩個域名,然後在IE浏覽器通路測試效果。
虛拟主機也可以單獨建立個虛拟檔案,然後在nginx.conf中引用即可,引用指令include+虛拟檔案
本文轉自 Anonymous123 51CTO部落格,原文連結:http://blog.51cto.com/woshitieren/1684276