Auther:Rich七哥
Nginx優化一、Nginx隱藏版本號;二、網頁緩存、連接超時、網頁壓縮傳輸;配置連接超時:3.網頁壓縮傳輸:三、訪問控制、定義錯誤頁面、自動索引、目錄別名;四、日志分割;五、虛擬主機;六、平滑升級;七、加載第三方模塊;
案例環境:
系統類型 | IP地址 | 主機名 | 所需軟件 |
---|---|---|---|
Centos 6.5 | 192.168.1.78 | harry78 | nginx-1.6.2.tar.gz |
一、Nginx隱藏版本號;
方式一:修改配置文件
Ø 安裝Nginx
[root@www ~]# yum -y install pcre-devel zlib-devel popt-devel openssl-devel openssl
[root@www ~]# useradd -M -s /sbin/nologin nginx
[root@www ~]# tar zxvf nginx-1.6.2.tar.gz -C /usr/src/
[root@www ~]# cd /usr/src/nginx-1.6.2/
[root@www nginx-1.6.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-file-aio --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_ssl_module --with-pcre && make && make install
[root@www nginx-1.6.2]# cd
[root@www ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@www ~]# nginx
[root@www ~]# netstat -utlpn |grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 5618/nginx
Ø 配置版本號隱藏;
[root@www ~]# curl -I http://harry78 ##選項為-i
HTTP/1.1 200 OK
Server: nginx/1.6.2
Date: Wed, 11 Jul 2018 16:43:05 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Wed, 11 Jul 2018 16:40:55 GMT
Connection: keep-alive
ETag: "5b463317-264"
Accept-Ranges: bytes
[root@www ~]# vi /usr/local/nginx/conf/nginx.conf ##在http{}內添加即可
20 server_tokens off;
:wq
[root@www ~]# nginx -t ##檢查nginx配置文件語法
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@www ~]# killall -9 nginx
[root@www ~]# nginx
[root@www ~]# curl -I http://harry78
HTTP/1.1 200 OK
Server: nginx ##版本已經隱藏
Date: Fri, 08 Dec 2017 22:56:00 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Fri, 08 Dec 2017 22:47:50 GMT
Connection: keep-alive
ETag: "5a2b1696-264"
Accept-Ranges: bytes
方式二:修改源碼包;
[root@localhost ~]# yum -y install pcre-devel zlib-devel popt-devel openssl-*
[root@localhost ~]# useradd -M -s /sbin/nologin nginx
[root@localhost ~]# tar zxf nginx-1.6.2.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/nginx-1.6.2/
[root@localhost nginx-1.6.2]# vim src/core/nginx.h ##修改源代碼實現隱藏版本
13 #define NGINX_VERSION "6.6.6"
14 #define NGINX_VER "harry.cn/" NGINX_VERSION
15
16 #define NGINX_VAR "harry.cn"
:wq
[root@localhost nginx-1.6.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-file-aio --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_ssl_module --with-pcre && make && make install
[root@localhost nginx-1.6.2]# cd
[root@localhost ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@localhost ~]# nginx
[root@localhost ~]# curl -I http://harry78
HTTP/1.1 200 OK
Server: harry.cn/6.6.6
Date: Fri, 08 Dec 2017 23:06:20 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Fri, 08 Dec 2017 23:05:45 GMT
Connection: keep-alive
ETag: "5a2b1ac9-264"
Accept-Ranges: bytes
二、網頁緩存、連接超時、網頁壓縮傳輸;
1.**網頁緩存:**
作用:頁面緩存一般針對靜態網頁進行設置,對動態網頁不用設置緩存時間。方便客戶端
在日后進行相同內容的請求時直接返回,以避免重復請求,加快了訪問速度。
配置nginx緩存:
[root@www ~]# cat <<END >/usr/local/nginx/html/index.html
<html>
<head>
<title>harry78</title>
</head>
<body>
harry78
</body>
</html>
END
[root@www ~]# ls /usr/local/nginx/html/
index.html linux.jpg
[root@www ~]# vi /usr/local/nginx/conf/nginx.conf
55 location ~ .(gif|jpg|jpeg|png|bmp|ico)$ {
56 expires 1d;
57 }
58 location ~ .*.(js|css)$ {
59 expires 1h;
60 }
:wq
[root@www ~]# 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@www ~]# killall -9 nginx
配置連接超時:
作用:在企業網站中,為了避免同一個客戶長時間占用連接,造成服務器資源浪費,可以設置相應的連接超時參數,實現控制連接訪問時間。
配置項:
keepalived_timeout | 設置連接保持超時時間,一般可只設置該參數,默認為 65 秒,可根據網站的情況設置,或者關閉,可在 http 段、 server 段、或者 location 段設置。 |
---|---|
client_header_timeout | 指定等待客戶端發送請求頭的超時時間。 |
client_body_timeout | 設置請求體讀取超時時間。 |
注意: 若出現超時,會返回 408 報錯 |
[root@www ~]# vi /usr/local/nginx/conf/nginx.conf
32 keepalive_timeout 65;
33 client_header_timeout 60;
34 client_body_timeout 60;
:wq
[root@www ~]# killall -9 nginx
[root@www ~]# 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@www ~]# nginx
3.網頁壓縮傳輸:
作用:將服務端傳輸的網頁文件壓縮傳輸,使其更加快速、減少帶寬的占用;
配置:
[root@www ~]# vi /usr/local/nginx/conf/nginx.conf
37 gzip on; ##開啟 gzip 壓縮輸出
38 gzip_min_length 1k; ##用於設置允許壓縮的頁面最小字節數
39 gzip_buffers 4 16k; ##表示申請4 個單位為 16k 的內存作為壓縮結果流緩存,默認值是申請與原始數據大小相同的內存空間來儲存 gzip 壓縮結果
40 gzip_http_version 1.1; # #設置識別 http 協議版本,默認是 1.1
41 gzip_comp_level 2; ##gzip 壓縮比, 1-9 等級
42 gzip_types text/plain text/javascript application/x-javascript text/css text/xml application/xml application/xml+rss; ##壓縮類型,是就對哪些網頁文檔啟用壓縮功能
[root@www ~]# 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@www ~]# killall nginx
[root@www ~]# nginx
三、訪問控制、定義錯誤頁面、自動索引、目錄別名;
1.**訪問控制:**
作用:限制訪問網站資源;
配置項:
auth_basic "Nginx Status"; | 認證提示文字 |
---|---|
auth_basic_user_file /usr/local/nginx/conf/user.conf; | 認證用戶文件,可以使用apache提供的htpasswd命令來生成文件 |
allow 192.168.100.1; | 允許客戶端ip地址 |
deny 192.168.100.0/24; | 拒絕的網段 |
配置:
[root@www ~]# yum -y install httpd-tools
[root@www ~]# htpasswd -c /usr/local/nginx/conf/user.conf zs
[root@www ~]# cat /usr/local/nginx/conf/user.conf
zs:VJVdQdVHEIvZo
[root@www ~]# vi /usr/local/nginx/conf/nginx.conf
70 location /status {
71 stub_status on;
72 access_log off;
73 auth_basic "Nginx Status";
74 auth_basic_user_file /usr/local/nginx/conf/user.conf;
75 allow 192.168.100.1;
76 deny 192.168.100.0/24;
77 }
[root@ www ~]# killall -9 nginx
[root@ www ~]# nginx
客戶端訪問驗證:
2.**定義錯誤頁面:**
作用:根據客戶端的訪問網站的返回狀態碼,為其指定到特定的錯誤頁面;
配置:
[root@ www ~]# vi /usr/local/nginx/conf/nginx.conf
78 error_page 403 404 /404.html;
79 location = /404.html {
80 root html;
81 }
[root@ www ~]# echo "deny" >>/usr/local/nginx/html/404.html
[root@ www ~]# killall -9 nginx
[root@www ~]# nginx
客戶端訪問驗證:
3.**自動索引:**
作用:將網站轉化為類似ftp的站點,作為共享文件的工具;
配置:
[root@www ~]# mkdir -p /usr/local/nginx/html/download/haha/
[root@www ~]# touch /usr/local/nginx/html/download/haha/{1..10}.txt
[root@www ~]# vi /usr/local/nginx/conf/nginx.conf
82 location /download {
83 autoindex on;
84 }
[root@www ~]# killall -9 nginx
[root@www ~]# nginx
客戶端訪問測試:
4.**目錄別名:**
作用:將域名后綴的路徑設置一個別名,通過多種方式訪問;
配置:
[root@www ~]# vi /usr/local/nginx/conf/nginx.conf
85 location /dw {
86 alias /usr/local/nginx/html/haha/;
87 }
[root@www ~]# mkdir /usr/local/nginx/html/haha
[root@www ~]# echo "haha" >/usr/local/nginx/html/haha/index.html
[root@www ~]# killall -9 nginx
[root@www ~]# nginx
客戶端訪問測試:
四、日志分割;
方式:腳本方式
技術要點:
a.剪切日志后,使用kill -USR1發送信號重新生成日志文件,同時還不影響網站請求處理進程。
b.錯誤時通過echo和tee -a命令將錯誤顯示的同時寫入到日志文件/var/log/messages。
[root@www ~]# vi /root/cut_nginx_log.sh
五、虛擬主機;
作用:在同一台服務器上部署多個網站,減免資源的占用;
實現方式:
1.不同IP,不同域名,相同端口;
2.相同IP,相同域名,不同端口;
3.相同IP,相同端口,不同域名;
案例環境:
系統類型 | IP地址 | 主機名 | 所需軟件 |
---|---|---|---|
Centos 6.5 | 192.168.100.151 | harry666 | nginx-1.6.2.tar.gz |
方式一:不同IP,不同域名,相同端口;
[root@harry ~]# ip a |grep 192.168.100
inet 192.168.100.151/24 brd 192.168.100.255 scope global eth0
inet 192.168.100.200/24 brd 192.168.100.255 scope global secondary eth0:0
[root@linharry# vi /usr/local/nginx/conf/nginx.conf
worker_processes 1;
events {
use epoll;
worker_connections 4096;
}
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;
keepalive_timeout 65;
server {
listen 192.168.100.151:80;
server_name harry78;
charset utf-8;
access_log logs/harry.access.log main;
location / {
root /var/www/harry/;
index index.html index.php;
}
}
server {
listen 192.168.100.200:80;
server_name www.harry.cn;
charset utf-8;
access_log logs/harry.access.log main;
location / {
root /var/www/harry/;
index index.html index.php;
}
}
}
[root@linuxrharrykdir -p /var/www/harry
[root@linuxren harryr -p /var/www/harry
[root@linuxren ~]#harryarry78" >/var/www/harry/index.html
[root@linuxren ~]# echarryharry.cn" >/var/www/harry/index.html
[root@linuxren ~]# killaharryinx
[root@linuxren ~]# nginx
harry
客戶端訪問測試:
方式二:相同**IP,不同域名,相同端口;**
[root@harry ~]# vi /usr/local/nginx/conf/nginx.conf
worker_processes 1;
events {
use epoll;
worker_connections 4096;
}
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;
keepalive_timeout 65;
server {
listen 192.168.100.151:80;
server_name harry78;
charset utf-8;
access_log logs/harry.access.log main;
location / {
root /var/www/harry/;
index index.html index.php;
}
}
server {
listen 192.168.100.151:80;
server_name www.harry.cn;
charset utf-8;
access_log logs/harry.access.log main;
location / {
root /var/www/harry/;
index index.html index.php;
}
}
}
[root@linharry# killall -9 nginx
[root@linuxrharryginx
客戶端訪問測試:
方式三:相同IP,不同端口,相同域名;
[root@harry ~]# vi /usr/local/nginx/conf/nginx.conf
worker_processes 1;
events {
use epoll;
worker_connections 4096;
}
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;
keepalive_timeout 65;
server {
listen 192.168.100.151:80;
server_name harry78;
charset utf-8;
access_log logs/harry.access.log main;
location / {
root /var/www/harry/;
index index.html index.php;
}
}
server {
listen 192.168.100.151:8080;
server_name harry78;
charset utf-8;
access_log logs/harry.access.log main;
location / {
root /var/www/harry/;
index index.html index.php;
}
}
}
[root@linharry# killall -9 nginx
[root@linuxrharryginx
客戶端訪問測試:
六、平滑升級;
原理:
1.Nginx 的主進程( master process)啟動后完成配置加載和端口綁定等動作, fork 出指定數量的工作進程( worker process),這些子進程會持有監聽端口的文件描述符( fd),並通過在該描述符上添加監聽事件來接受連接( accept);
2.Nginx 主進程在啟動完成后會進入等待狀態,負責響應各類系統消息,如 SIGCHLD、 SIGHUP、SIGUSR2 等;
3.主進程支持的信號:
TERM, INT: 立刻退出; QUIT: 等待工作進程結束后再退出
KILL: 強制終止進程; HUP: 重新加載配置文件,使用新的配置啟動工作進程,並逐步關閉舊進程。
USR1: 重新打開日志文件; USR2: 啟動新的主進程,實現熱升級
WINCH: 逐步關閉工作進程及工作進程支持的信號;
過程:
1.查看舊版 nginx 的編譯參數;
2.編譯新版本 Nginx 源碼包,安裝路徑需與舊版一致,注意:不要執行 make install;
3.備份二進制文件,用新版本的替換;
4.確保配置文件無報錯;
5.發送 USR2 信號:向主進程( master)發送 USR2 信號, Nginx 會啟動一個新版本的 master 進程和對應工作進程,和舊版一起處理請求;
6.發送 WINCH 信號:向舊的 Nginx 主進程( master)發送 WINCH 信號,它會逐步關閉自己的工作進程(主進程不退出),這時所有請求都會由新版 Nginx 處理;
7.發送 QUIT 信號:升級完畢,可向舊的 Nginx 主進程( master)發送( QUIT、 TERM、或者 KILL)信號,使舊的主進程退出;
8.驗證 nginx 版本號,並訪問測試;
配置:
Ø 准備軟件包並查看舊版安裝選項;
[root@harry ~]# ls nginx-1.*
nginx-1.12.0.tar.gz nginx-1.6.2.tar.gz
[root@harry ~]# nginx -V
nginx version: nginx/1.6.2
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC)
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-file-aio --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_ssl_module --with-pcre
Ø 安裝新版本Nginx;
[root@harry ~]# tar zxvf nginx-1.12.0.tar.gz -C /usr/src/
[root@harry ~]# cd /usr/src/nginx-1.12.0/
[root@harry nginx-1.12.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-file-aio --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_ssl_module --with-pcre && make ##不能加make install,如若添加,則覆蓋了
[root@harry nginx-1.12.0]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old
[root@harry nginx-1.12.0]# cp objs/nginx /usr/local/nginx/sbin/
[root@harry nginx-1.12.0]# 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@harry nginx-1.12.0]# cd
Ø 使用信號實現熱升級;
[root@harry ~]# ps aux |grep nginx |grep -v grep
root 6059(老版本主進程) 0.0 0.2 45000 1032 ? Ss 13:03 0:00 nginx: master process nginx
nginx 6060 0.0 0.3 45432 1624 ? S 13:03 0:00 nginx: worker process
[root@harry ~]# kill -USR2 6059 ##發送 USR2 信號:向主進程( master)發送 USR2 信號, Nginx 會啟動一個新版本的 master 進程和對應工作進程,和舊版一起處理請求。
[root@harry ~]# kill -WINCH $(cat /usr/local/nginx/logs/nginx.pid) ##關閉老版本的worker進程
[root@harry ~]# kill -QUIT $(cat /usr/local/nginx/logs/nginx.pid) ##關閉老版本的master進程
[root@harry ~]# /usr/local/nginx/sbin/nginx ##重新加載新版本的命令
[root@harry ~]# ps aux |grep nginx |grep -v grep
root 3864 0.0 0.2 45192 1188 ? Ss 03:24 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 3865 0.0 0.6 46904 3052 ? S 03:24 0:00 nginx: worker process
[root@harry ~]# nginx -V
nginx version: nginx/1.12.0
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-file-aio --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_ssl_module --with-pcre
七、加載第三方模塊;
第三方模塊下載地址:**https://www.nginx.com/resources/wiki/modules/**
模塊:**echo-nginx-module-0.60.tar.gz**
[root@harry ~]# ls
echo-nginx-module-0.60.tar.gz nginx-1.6.2.tar.gz
[root@harry ~]# tar zxvf echo-nginx-module-0.60.tar.gz -C /usr/src/
[root@harry ~]# tar zxvf nginx-1.6.2.tar.gz -C /usr/src/
[root@harry ~]# cd /usr/src/nginx-1.6.2/
[root@harry nginx-1.6.2# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-file-aio --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_ssl_module --with-pcre --add-module=/usr/src/echo-nginx-module-0.60/ &&make &&make install
[root@harry nginx-1.6.2# cd
[root@harry ~]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
[root@harry ~] # vi /usr/local/nginx/conf/nginx.conf
worker_processes 1;
events {
use epoll;
worker_connections 4096;
}
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;
keepalive_timeout 65;
server {
listen 192.168.1.78:80;
server_name harry78;
charset utf-8;
access_log logs/harry.access.log main;
location / {
echo "nginx";
}
}
}
[root@harry ~]# killall -9 nginx
[root@harry ~]# nginx
[root@harry ~]# curl 192.168.1.78
nginx
[root@harry ~]# curl -I 192.168.1.78
HTTP/1.1 200 OK
Server: nginx/1.6.2
Date: Fri, 13 Jul 2018 18:06:42 GMT
Content-Type: application/octet-stream
Connection: keep-alive