編譯安裝Nginx網站服務及優化技術
編譯安裝Nginx網站服務及優化
一、關於Nginx 二、Nginx模組單元介紹 三、不同版本的Nginx區別 四、主配置檔案修改 4.1、全域性配置 4.2、程序數配置 4.3、http配置 五 Nginx的優化安裝 5.1、編譯安裝 5.2、Nginx的訪問狀態統計 5.3、Nginx的驗證功能 六、配置Nginx虛擬主機 6.1、基於域名 6.2、基於IP 6.3、基於埠一、關於Nginx
1.1、一款高效能、輕量級Web服務軟體
1.1.1、穩定性高
1.1.2、系統資源消耗低
1.1.3、對HTTP併發連線的處理能力高
1.1.4、單臺物理伺服器可支援30000~50000個併發請求
二、Nginx模組單元介紹
2.1、ngx_http_access_module模組:實現基於ip的訪問控制功能
2.2、ngx_http_auth_basic_module模組:實現基於使用者的訪問控制,使用basic機制進行使用者認證
2.3、ngx_http_stub_status_module模組:用於輸出nginx的基本狀態資訊
2.4、ngx_http_log_module模組:用指定的格式寫入請求日誌
2.5、ngx_http_gzip_module模組:用gzip演算法來壓縮資料可以節約頻寬,預設nginx不壓縮資料,但是壓縮會消耗CPU資源,且壓縮文字影象類效果較好,能達到30%左右,但壓縮音訊視訊沒有多大意義,因為本身音視訊就是被壓縮過的,很可能對音視訊壓縮反而會增大其體積
2.6、ngx_http_ssl_module模組:啟用https時使用的模組
2.7、ngx_http_rewrite_module模組:重定向模組,可以將客戶端的請求基於regex所描述的模式進行檢查,而後完成替換。當舊的業務和新的業務不一樣,網站更名了,就可以使用此模組。將訪問舊的請求重定向成新的
2.8、ngx_http_referer_module模組:可以跟蹤連結從哪裡跳轉過來的,該欄位可以用來防止盜鏈
2.9、ngx_http_headers_module模組:向由代理伺服器響應給客戶端的響應報文新增自定義首部,或修改指定首部的
三、不同版本的Nginx區別
2.1、Mainline version
2.2、Stable version:最新穩定版,生產環境上建議使用的版本
2.3、Legacy versions:遺留的老版本的穩定版
四、主配置檔案修改
4.1、全域性配置
1 #user nobody; ##指定使用者,預設是匿名使用者 2 worker_processes 1; ##工作程序,實現高併發可以增加值 3 #error_log logs/error.log; ##錯誤日誌檔案 4 #pid logs/nginx.pid; ##pid檔案
4.2、程序數配置
1 events { ##一個程序包含多個執行緒 2 use epoll; #能顯著提高程式在大量併發連線中只有少量活躍的情況下的系統CPU利用率 3 worker_connections 4096; 4 }
5 連線數4096基於執行緒數
4.3、http配置
1 http{}:協議層面;server{}:服務層面;location{}:網頁站點目錄層面 2 http { 3 access_log logs/access.log main; 4 sendfile on; ##傳送郵件 5 keepalive timeout 65; ##連線超時時間 6 server { 7 listen 80; 8 server_name localhost; ##域名 9 charset utf-8; ##字符集 10 location / { 11 root html; ##網頁站點目錄名稱 12 index index.html index.php; } ##主頁的相對路徑 13 error_page 500 502 503 504 /50x.html; ##提示錯誤頁面(500是伺服器出錯) 14 location = /50x.html { 15 root html; } 16 } 17 }
五、Nginx的優化安裝
5.1、編譯安裝
1 [root@localhost ~]# yum -y install pcre-devel zlib-devel 2 3 [root@localhost ~]# tar zxf nginx-1.12.2.tar.gz 4 [root@localhost ~]# cd nginx-1.12.2/ 5 [root@localhost nginx-1.12.2]# ./configure \ 6 > --prefix=/usr/local/nginx \ 7 > --user=nginx \ 8 > --group=nginx \ 9 > --with-http_stub_status_module ###統計狀態模組 10 11 [root@localhost nginx-1.12.2]# make && make install 12 13 [root@localhost ~]# useradd -M -s /sbin/nologin nginx ###建立一個不可登入的程式使用者 14 15 [root@localhost ~]# ln -s /usr/local/nginx/sbin/nginx /usr/bin ###優化執行路徑 16 [root@localhost ~]# nginx ###啟動服務 17 [root@localhost ~]# netstat -anpt | grep nginx ###檢視nginx服務是否開啟 18 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 16429/nginx: master 19 20 [root@localhost ~]# killall -s QUIT nginx ###選項-s QUIT等於-3 停止服務 21 [root@localhost ~]# netstat -anpt | grep nginx 22 [root@localhost ~]# nginx 23 [root@localhost ~]# killall -s HUP nginx ###選項-s HUP等於-1 重新載入 24 [root@localhost ~]# netstat -anpt | grep nginx 25 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 16488/nginx: master 26 27 [root@localhost ~]# vi /etc/init.d/nginx ###製作管理指令碼 28 #!/bin/bash 29 #chkconfig: 35 20 80 30 #description: nginx server 31 PROG="/usr/local/nginx/sbin/nginx" 32 PIDF="/usr/local/nginx/logs/nginx.pid" 33 34 case "$1" in 35 start) 36 $PROG 37 ;; 38 stop) 39 killall -s QUIT $(cat $PIDF) 40 ;; 41 restart) 42 $0 stop 43 $0 start 44 ;; 45 reload) 46 killall -s HUP $(cat $PIDF) 47 ;; 48 *) 49 echo "Usage: $0 {start|stop|reload|status}" 50 exit 1 51 esac 52 exit 0 53 54 [root@localhost ~]# chmod +x /etc/init.d/nginx 55 [root@localhost ~]# chkconfig --add nginx
5.2、Nginx的訪問狀態統計
5.2.1、修改主配置檔案
1 [root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf 2 user nginx nginx ###修改#user nobody為user nginx nginx 3 error_log logs/error.log info ###去除#號 4 5 events { 6 use epoll; ###新增此行,預設使用select/poll 7 worker_connections 1024; ###表示一個工作程序允許1024個連線 8 } 9 10 location ~ /status { ###配置統計功能 11 stub_status on; 12 access_log off; 13 } ###在server模組裡的error_page上面增加 14 15 [root@localhost ~]# nginx -t ###檢查一下配置檔案 16 nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok 17 nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful 18 19 [root@localhost ~]# nginx -V ###檢視版本號及開啟的模組 20 nginx version: nginx/1.12.2 21 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) 22 configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module 23 24 [root@localhost ~]# systemctl start nginx ###開啟nginx服務 25 [root@localhost ~]# systemctl stop firewalld 26 [root@localhost ~]# setenforce 0 ###關閉防火牆和核心防護
5.2.2、驗證
在客戶端瀏覽器輸入:http://192.168.73.40/status 驗證
5.2.3、nginx status 詳解
1 active connections:活躍的連線數量; 2 server accepts handled requests:總共處理n個連線,成功建立n次握手,共處理n個請求; 3 reading:讀取客戶端的連線數; 4 writing:響應資料到客戶端的數量; 5 waiting:開啟keep-alive的情況下,這個值等於active-(reading+writing),意思就是Nginx已經處理完正在等候下一次指令的駐留地址。
5.3、Nginx的驗證功能
5.3.1、主配置檔案
1 [root@localhost ~]# yum -y install httpd-tools 2 [root@localhost ~]# htpasswd -c /usr/local/nginx/passwd.db lisi ###建立訪問使用者 3 New password: 4 Re-type new password: 5 Adding password for user lisi 6 [root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf 7 location / { 8 root html; 9 index index.html index.htm; 10 allow 192.168.73.40/24; ###允許本機訪問 11 deny all; ###拒絕所有 12 auth_basic "secret"; ###驗證方式為密碼驗證 13 auth_basic_user_file /usr/local/nginx/passwd.db; ###密碼所在檔案 14 } 15 [root@localhost ~]# nginx -t ###驗證配置檔案是否有錯 16 nginx: [warn] low address bits of 192.168.73.40/24 are meaningless in /usr/local/nginx/conf/nginx.conf:48 17 nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok 18 nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful 19 20 [root@localhost ~]# systemctl restart nginx ###重啟服務
5.3.2、驗證
六、配置Nginx虛擬主機
6.1、基於域名
6.1.1、修改配置檔案
1 vi /usr/local/nginx/conf/nginx.conf 2 3 server { 4 listen 80; 5 server_name www.aa.com; 6 7 charset utf-8 8 9 location / { 10 root /var/www/aa; 11 index index.html index.htm; 12 } 13 } 14 15 server { 16 listen 80; 17 server_name www.ab.com; 18 19 charset utf-8; 20 21 location / { 22 root /var/www/ab; 23 index index.html index.htm; 24 } 25 } 26 27 [root@localhost ~]# nginx -t ###檢查語法 28 nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok 29 nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful 30 31 [root@localhost ~]# systemctl restart nginx ###重啟nginx服務 32 [root@localhost ~]# systemctl stop firewalld 33 [root@localhost ~]# setenforce 0 34 35 [root@localhost ~]# vi /etc/hosts ###增加對映 36 192.168.73.40 www.aa.com www.ab.com
6.1.2、準備測試頁
1 [root@localhost ~]# mkdir -p /var/www/aa 2 [root@localhost ~]# mkdir -p /var/www/ab 3 [root@localhost aa]# vi index.html 4 <html><body><h1>This is test1!</h1></body></html> 5 [root@localhost ab]# vi index.html 6 <html><body><h1>This is test2!</h1></body></html>
6.1.3、客戶機測試
6.2、基於IP
6.2.1、新增一張網絡卡、修改配置檔案
1 [root@www ~]# vi /usr/local/nginx/conf/nginx.conf 2 server { 3 listen 192.168.73.40:80; ###修改IP地址 4 server_name www.aa.com; 5 6 charset utf-8; 7 8 #access_log logs/aa.access.log main; 9 10 location / { 11 root /var/www/aa; 12 index index.html index.htm; 13 } 14 } 15 16 server { 17 listen 192.168.73.134:80; ###修改IP地址 18 server_name www.aa.com; 19 20 charset utf-8; 21 22 #access_log logs/aa.access.log main; 23 24 location / { 25 root /var/www/aa; 26 index index.html index.htm; 27 } 28 } 29 30 [root@www ~]# nginx -t ###檢查語法 31 nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok 32 nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful 33 34 [root@www ~]# systemctl restart nginx 35 36 [root@www ~]# vi /etc/hosts 37 192.168.73.40 www.aa.com 38 192.168.73.134 www.aa.com
6.2.3、客戶機測試
6.3、基於埠
6.3.1、修改配置檔案
1 [root@www ~]# vi /usr/local/nginx/conf/nginx.conf 2 server { 3 listen 192.168.73.40:80; ###更改埠號 4 server_name www.aa.com; 5 6 charset utf-8; 7 8 #access_log logs/aa.access.log main; 9 10 location / { 11 root /var/www/aa; 12 index index.html index.htm; 13 } 14 } 15 16 server { 17 listen 192.168.73.40:8080; ###更改埠號 18 server_name www.aa.com; 19 20 charset utf-8; 21 22 #access_log logs/aa.access.log main; 23 24 location / { 25 root /var/www/aa; 26 index index.html index.htm; 27 } 28 } 29 30 [root@www ~]# systemctl restart nginx
6.3.2、客戶機測試