Nginx專爲性能優化而開發,其最大的優勢就是它的穩定性和低系統資源消耗,以及對http併發鏈接的高處理能力,單臺物理服務器可支持20000~50000個併發請求,正是如此,大量提供社交網絡、新聞資訊、電子商務及虛擬主機等服務的企業紛紛選擇Nginx來提供web服務,目前中國大陸使用nginx網站用戶有:新浪、網易、騰訊,另外知名的微網誌Plurk也使用nginx。
Apache 和 Nginx 的區別:https://blog.51cto.com/14227204/2435423
下面開始安裝 Nginx:
1、準備工做:
Centos 7 系統及光盤
編譯安裝的軟件包: https://pan.baidu.com/s/1-GaLSYxt4fP5R2gCVwpILA
提取碼: kph5
也能夠從官網 https://nginx.org/ 下載使用
2、開始搭建Nginx網站:
安裝所須要的依賴包並卸載當前有的 httpd 服務(若是肯定沒有,可省略):php
[root@mysql yum.repos.d]# yum -y erase httpd [root@mysql /]# yum -y install pcre-devel zlib-devel # 安裝所需依賴包
編譯安裝及配置優化 Nginx:html
[root@mysql /]# useradd -M -s /sbin/nologin nginx # Nginx 默認以 nobody 身份運行,建議建立一個專門的用戶帳號 [root@mysql /]# tar zxf nginx-1.12.0.tar.gz -C /usr/src/ [root@mysql /]# cd /usr/src/nginx-1.12.0/ [root@mysql nginx-1.12.0]# ./configure --prefix=/usr/local/nginx --user=nginx \ > --group=nginx --with-http_stub_status_module && make && make install [root@mysql /]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ # 建立連接文件,方便命令使用
Nginx 運行控制:
一、檢查配置文件mysql
[root@mysql /]# nginx -t # 檢查配置文件,若是出現 OK 或 successful 字樣說明沒問題 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
二、啓動、中止 Nginxnginx
[root@mysql /]# nginx # 啓動 [root@mysql /]# killall -s HUP nginx # 重啓 選項 -s HUP 等同於 -1 [root@mysql /]# killall -s QUIT nginx # 關閉 選項 -s QUIT 等同於 -3
注意:最小化安裝的 centos 7 默認沒有安裝 killall 命令,可使用 「yum -y install psmisc」
爲了使 Nginx 服務的啓動、中止、重載等操做更方便,能夠編寫一個 Nginx 服務腳本,這亞子更符合 Centos 系統的管理習慣:web
[root@mysql /]# 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@mysql /]# chmod +x /etc/init.d/nginx // 授予權限 [root@mysql /]# chkconfig --add nginx // 添加爲系統服務 [root@mysql /]# systemctl start nginx // 啓動 Nginx
[root@mysql /]# vim /usr/local/nginx/conf/nginx.conf ................... #user nobody; // 運行用戶 worker_processes 1; // 工做進程數量 #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; // 使用 epoll 模型,以提升性能 worker_connections 4096; // 每一個進程處理 4096 個鏈接 }
以上的優化是基於全局配置實施的,各項優化的含義以下:sql
一、worker_processes :表示工做進程的數量,若服務器由多塊CPU或者使用多核處理器,能夠參考CPU核心總數來指定工做進程數。具體含義在worker_connections配置項中體現出來,
二、worker_connections:這個配置項指定的是每一個進程處理的鏈接,通常在10000如下(默認爲1024),與上面工做進程數量的配置項關聯,舉個栗子:若工做進程數爲8,每一個進程處理4096個鏈接,則容許Nginx正常提供服務的鏈接數已經超過了3萬個(4096*8=32768)。固然,具體還要看服務器硬件、網絡帶寬等物理條件的性能表現。vim
搭建基於域名的虛擬 web 主機:
HTTP配置:
Nginx的配置文件使用「http { }」界定標記用於設定HTTP服務器,包括訪問日誌、http端口、網頁目錄、默認字符集、鏈接保持,以及虛擬web主機、php解析等網站全局設置,其中大部分包含在子界定標記 「 server { }」內。「 server { }」表明一個具體的網站設置。centos
[root@mysql /]# 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 { // web 服務的監聽配置 listen 80; // 監聽地址及端口 server_name www.test1.com; // 網站名稱 FQDN charset koi8-r; // 網頁的默認字符集 access_log logs/host.access.log main; location / { // 根目錄配置 root html; // 網站根目錄的位置,相對於安裝目錄 index index.html index.php; // 默認首頁,索引頁 } error_page 500 502 503 504 /50x.html; // 內部錯誤的反饋頁面 location = /50x.html { // 錯誤頁面配置 root html; }
以上配置只是搭建了一個網站服務,若想運行多個,可複製配置文件最後面提供的模板,粘貼到 「server{ } 」配置上面,由於在配置文件中有太多的 「 { }」,爲了不錯誤,因此才需複製到原有的 「server{ } 」之上,以下:性能優化
[root@mysql /]# vim /usr/local/nginx/conf/nginx.conf server { listen 80; server_name www.test1.com; charset utf-8; access_log logs/host.access.log main; location / { root /var/www/test1; index index.html index.php; } location /status { stub_status on; access_log off; } } server { listen 80; server_name www.test2.com; charset utf-8; access_log logs/host.access.log main; location / { root /var/www/test2; index index.html index.php; }
虛擬主機到此就配置完成了,而後重啓服務使配置生效,DNS 自行配置,可參考博文:https://blog.51cto.com/14227204/2384462bash
[root@mysql /]# 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 [root@mysql /]# systemctl restart nginx [root@mysql named]# mkdir -p /var/www/test1 # 建立網站根目錄 [root@mysql named]# mkdir -p /var/www/test2 [root@mysql named]# echo www.test1.com > /var/www/test1/index.html # 建立測試文件 [root@mysql named]# echo www.test2.com > /var/www/test2/index.html
客戶機開始驗證:
查看當前的狀態信息:
測試另外一個域名: