Nginx (engine x)專爲性能優化而開發,其特色是佔有內存少,它的穩定性和低系統資源消耗,以及對併發鏈接的高處理能力,(單臺物理服務器可支持5000個併發請求)。事實上nginx的併發能力確實在同類型的網頁服務器中表現較好,中國大陸使用nginx網站用戶有:百度、京東、新浪、網易、騰訊、淘寶等。同時也提供了IMAP/POP3/SMTP服務。
Nginx的優勢:php
*** 能夠高併發鏈接** 官方測試Nginx可以支撐5萬併發鏈接,實際生產環境中能夠支撐2~4萬併發鏈接數。 *** 內存消耗少** Nginx+PHP(FastCGI)服務器,在3萬併發鏈接下,開啓10個Nginx進程消耗150MB內存,15MB*10=150MB,開啓的64個PHP-CGI進程消耗1280內存,20MB*64=1280MB,加上系統自身消耗的內存,總共消耗不到2GB的內存。 *** 成本低廉** 購買F5BIG-IP、NetScaler等硬件負載均衡交換機,須要十多萬到幾十萬人民幣,而Nginx爲開源軟件,採用的是2-clause BSD-like協議,能夠免費試用,而且可用於商業用途。 *** 配置文件很是簡單** 網絡和程序同樣通俗易懂,即便,非專用系統管理員也能看懂。 *** 支持Rewrite重寫** 可以根據域名、URL的不一樣,將http請求分到不一樣的後端服務器羣組。 *** 內置的健康檢查功能** 若是NginxProxy後端的某臺Web服務器宕機了,不會影響前端的訪問。 *** 節省帶寬** 支持GZIP壓縮,能夠添加瀏覽器本地緩存的Header頭。 *** 穩定性高** 用於反向代理,宕機的機率微乎其微。 * **支持熱部署** Nginx支持熱部署,它的自動特別容易,而且,幾乎能夠7天*24小時不間斷的運行,即便,運行數個月也不須要從新啓動,還可以在不間斷服務的狀況下,對軟件版本進行升級。
下圖是Nginx、Apache、lighttpd的性能對比:html
已上說了那麼多都是爲了凸顯Nginx性能的強大,那麼如何基於centos 7搭建Nginx網站服務器(包含虛擬web主機的配置),下面咱們繼續來說解Nginx的配置以及在虛擬機上的應用:前端
centos 7服務器一臺;nginx
3.須要用到的軟件包,連接web
https://pan.baidu.com/s/1cfdQeNWAidd3XVtGisQU6g 提取碼: usjt vim
4.也能夠從官網網站 http://www.nginx.org/ 下載
.3、開始搭建Nginx網站(掛載系統盤,安裝所需的依賴包。):後端
一、安裝所需依賴包,均由系統盤提供:
二、編譯安裝及配置優化Nginxcentos
[root@localhost media]# useradd -M -s /sbin/nologin nginx #建立系統用戶 [root@localhost media]# tar zxf nginx-1.12.0.tar.gz -C /usr/src #解包 [root@localhost media]# cd /usr/src/nginx-1.12.0/ [root@localhost nginx-1.12.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module && make && make install #編譯安裝Nginx [root@localhost ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ #建立主程序的連接文件 爲了使Nginx服務的啓動,中止,重載等操做更加方便,能夠編輯Nginx服務腳本。腳本編譯以下: [root@localhost ~]# vim /etc/init.d/nginx #編輯服務腳本 #!/bin/bash # chkconfig: - 99 20 PROG="/usr/local/nginx/sbin/nginx" PIDF="/usr/local/nginx/logs/nginx.pid" case "$1" in start) $PROG ;; stop) kill -s QUIT $(cat $PIDF) ;; restart) $0 stop $0 start ;; reload) kill -s HUP $(cat $PIDF) ;; *) echo "USAGE:$0 {start | stop | restart | reload}" exit 1 esac exit 0 [root@localhost ~]# chmod +x /etc/init.d/nginx #添加執行權限 [root@localhost ~]# chkconfig --add nginx #添加爲系統服務 [root@localhost ~]# systemctl start nginx #啓動Nginx服務,以確認腳本的正常運行 [root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf #調整配置文件,以優化web服務 .............. worker_processes 2; #工做進程數 #error_log logs/error.log; #錯誤日誌文件位置 #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; #PID文件的位置 events { use epoll; #在even{ }中添加該行以提升性能 worker_connections 4096; 每一個進程處理4096個鏈接 }
以上的優化是基於全局配置實施的,各項優化的含義以下:瀏覽器
worker_processes :表示工做進程的數量,若服務器由多塊CPU或者使用多核處理器,能夠參考CPU核心總數來指定工做進程數。具體含義在worker_connections配置項中體現出來,緩存
三、搭建基於域名的虛擬web主機:
一、HTTP配置:
Nginx的配置文件使用「http { }」界定標記用於設定HTTP服務器,包括訪問日誌、http端口、網頁目錄、默認字符集、鏈接保持,以及虛擬web主機、php解析等網站全局設置,其中大部分包含在子界定標記 「 server { }」內。「 server { }」表明一個具體的網站設置。
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf 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 logs/access.log main; #訪問日誌位置 sendfile on; 開啓高效傳輸文件模式 #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #鏈接保持超時 #gzip on; server { listen 80; #web服務器監聽端口,可使用「ip地址:端口」的形式 server_name www.test1.com; #網站域名 charset utf-8; #網站默認字符集,須去掉前面的「#」號 access_log logs/test1.access.log main; # 訪問日誌文件名 location /status { #添加 location /status 以便開啓狀態統計,訪問位置爲/status stub_status on; #打開狀態統計功能 access_log off; #關閉此位置的日誌記錄 } location / { root /var/www/test1; #網站根目錄 index index.html index.php; #默認首頁,改成index.php以便支持php網頁 } ; .......................... error_page 500 502 503 504 /50x.html; #內部錯誤的反饋頁面 location = /50x.html { #錯誤頁面配置 root html; } } }
以上配置只是搭建了一個網站服務,若想運行多個,可複製配置文件最後面提供的模板,粘貼到 「server{ } 」配置上面,由於在配置文件中有太多的 「 { }」,爲了不錯誤,因此才需複製到原有的 「server{ } 」之上,以下:
server { listen 80; server_name www.test2.com; charset utf-8; access_log logs/test2.access.log main; location /status { stub_status on; access_log off; } location / { root /var/www/test2; index index.html index.php; } } server { listen 80; server_name www.test1.com; ...........................
至此,虛擬主機搭建已經完成,需重啓服務,以服務生效,來驗證web服務器的正常運行(DNS需自行設置)
4、訪問狀態統計虛擬主機應用
[root@localhost ~]# nginx -t #重啓服務前使用該命令檢查配置文件, #若配置文件有錯,會提示錯在第幾行, #若沒錯,則顯示OK,有錯誤的話,重啓服務不會報錯,但配置文件不生效。 nginx: [emerg] unexpected ";" in /usr/local/nginx/conf/nginx.conf:44 #表示第44行有錯誤 nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed [root@localhost ~]# nginx -t #如下顯示ok,表示沒問題。 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 #下面準備網站目錄及測試文件,爲兩個虛擬web主機分別創建根目錄,並準備測試首頁以方便在測試時區分 [root@localhost named]# mkdir -p /var/www/test1 [root@localhost named]# mkdir -p /var/www/test2 [root@localhost named]# echo "www.test1.com" > /var/www/test1/index.html [root@localhost named]# echo "www.test2.com" > /var/www/test2/index.html
客戶機驗證:
①訪問www.test1.com 的首頁:
②訪問www.test1.com 的狀態統計頁:
上述含義以下:
Active connections表示當前的活動鏈接數爲2;
server accepts handled requests表示已處理的鏈接信息,三個數字分別表示已處理鏈接數3個,成功的握手次數爲3個,已處理的請求爲6個。
①訪問www.test2.com 的首頁:
②訪問www.test2.com 的狀態統計頁:
已上就是訪問狀態統計與虛擬主機的應用,感謝閱讀。