通過(guò)博文Nginx初步優(yōu)化就已經(jīng)了解了Nginx的基礎(chǔ)概念,已經(jīng)可以對(duì)Nginx進(jìn)行初步的優(yōu)化了,包括:Nginx平滑升級(jí)
、更改Nginx版本信息、Nginx虛擬主機(jī)配置、nginx配置文件中l(wèi)ocation選項(xiàng)的作用等等。本篇博文主要針對(duì)Nginx進(jìn)行進(jìn)一步的優(yōu)化。
創(chuàng)新互聯(lián)建站是一家集網(wǎng)站建設(shè),市北企業(yè)網(wǎng)站建設(shè),市北品牌網(wǎng)站建設(shè),網(wǎng)站定制,市北網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷,網(wǎng)絡(luò)優(yōu)化,市北網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
博文大綱:
一、Nginx配置反向代理
二、Nginx的proxy緩存使用
三、優(yōu)化Nginx服務(wù)的壓縮功能
配置Nginx作為反向代理和負(fù)載均衡,同時(shí)利用其緩存功能,將靜態(tài)頁(yè)面在Nginx中緩存,以達(dá)到降低后端服務(wù)器連接數(shù)的目的并檢查后端web服務(wù)器的檢查狀態(tài)。
如圖:
環(huán)境需求:
一臺(tái)Nginx服務(wù)器(Centos系統(tǒng))IP地址:192.168.1.1;
兩臺(tái)httpd服務(wù)器(Centos系統(tǒng))IP地址:192.168.1.2 192.168.1.3;
下載Nginx軟件包
安裝Nginx:
[root@localhost ~]# yum -y install gcc gcc-c++ make libtool zlib zlib-devel pcre pcre-devel openssl openssl-devel
//如果安裝系統(tǒng)時(shí),是最小化安裝,則需要安裝以上依賴包
[root@localhost ~]# yum -y install pcre-devel zlib-devel openssl-devel
//如果系統(tǒng)不是最小安裝,則安裝以上幾個(gè)依賴包即可
[root@localhost ~]# unzip nginx-sticky-module.zip -d /usr/src/
//使用 nginx-sticky-module 擴(kuò)展模塊實(shí)現(xiàn) Cookie 會(huì)話黏貼(保持會(huì)話)
[root@localhost ~]# tar zxf ngx_brotli.tar.gz -C /usr/src/
[root@localhost ~]# tar zxf ngx_cache_purge-2.3.tar.gz -C /usr/src/
//使用 ngx_cache_purge 實(shí)現(xiàn)更強(qiáng)大的緩存清除功能
//安裝Nginx源碼依賴包
[root@localhost ~]# tar zxf nginx-1.14.0.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/nginx-1.14.0/
[root@localhost nginx-1.14.0]# ./configure --prefix=/usr/local/nginx --user=nginx \
--group=nginx --with-http_stub_status_module --with-http_realip_module \
--with-http_ssl_module --with-http_gzip_static_module \
--http-client-body-temp-path=/var/tmp/nginx/client \
--http-proxy-temp-path=/var/tmp/nginx/proxy \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi --with-pcre \
--add-module=/usr/src/ngx_cache_purge-2.3/ --with-http_flv_module \
--add-module=/usr/src/nginx-sticky-module/ && make && make install
配置選項(xiàng)含義:
- --prefix=/usr/local/nginx:指定Nginx存放路徑;
- --with-http_stub_status_module:通過(guò)網(wǎng)頁(yè)方式監(jiān)控nginx狀態(tài);
- --with-http_realip_module:顯示客戶端真是IP;
- --with-http_ssl_module:開(kāi)啟Nginx的加密傳輸功能;
- --with-http_gzip_static_module:開(kāi)啟Nginx擴(kuò)展壓縮模塊;
- --http-client-body-temp-path=/var/tmp/nginx/client:客戶端訪問(wèn)數(shù)據(jù)臨時(shí)存放路徑;
- --http-proxy-temp-path=/var/tmp/nginx/proxy:同上;
- --http-fastcgi-temp-path=/var/tmp/nginx/fcgi:同上;
- --with-pcre:支持正則匹配表達(dá)式;
- --add-module=/usr/src/ngx_cache_purge-2.3: 添加第三方模塊,并指定第三方模塊路徑,支持緩存;
- --with-http_flv_module:支持flv視頻流;
- --add-module=/usr/src/nginx-sticky-module: 添加第三方模塊,并指定第三方模塊路徑,添加第三方模塊格式:--add-module=源碼解壓后的路徑;
[root@localhost ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
//創(chuàng)建符號(hào)鏈接
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
//編寫Nginx主配置文件
………… //省略部分內(nèi)容
http {
include mime.types;
default_type application/octet-stream;
upstream lzj { //定義服務(wù)器群組,名稱為lzj
sticky; //session會(huì)話保持
server 192.168.1.2:80 weight=1 max_fails=2 fail_timeout=10s;
server 192.168.1.3:80 weight=1 max_fails=2 fail_timeout=10s;
}
//定義了后臺(tái)兩臺(tái)服務(wù)器,權(quán)重分別為1,最大失敗次數(shù)為2,最大超時(shí)時(shí)間為10S
location / {
proxy_pass http://lzj;
}
//將原本的location規(guī)則注釋,并重新定義轉(zhuǎn)發(fā)到定義的lzj
[root@localhost ~]# nginx -t //檢測(cè)配置文件
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: [emerg] getpwnam("nginx") failed
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed
[root@localhost ~]# useradd -s /sbin/nologin -M nginx
//創(chuàng)建Nginx用戶,并不允許登錄操作系統(tǒng)
[root@localhost ~]# nginx -t //檢測(cè)配置文件
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: [emerg] mkdir() "/var/tmp/nginx/client" failed (2: No such file or directory)
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed
[root@localhost ~]# mkdir -p /var/tmp/nginx/client
//創(chuàng)建目錄,用于存放客戶端訪問(wèn)數(shù)據(jù)臨時(shí)存放路徑
[root@localhost ~]# 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
//表示配置文件沒(méi)有問(wèn)題
[root@localhost ~]# nginx //啟動(dòng)Nginx
[root@localhost ~]# nginx -V //可以查看編譯時(shí),使用的配置參數(shù)
nginx version: nginx/1.14.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_realip_module --with-http_ssl_module --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client --http-proxy-temp-path=/var/tmp/nginx/proxy --http-fastcgi-temp-path=/var/tmp/nginx/fcgi --with-pcre --add-module=/usr/src/ngx_cache_purge-2.3/ --with-http_flv_module --add-module=/usr/src/nginx-sticky-module/
測(cè)試機(jī)操作如下:
第一臺(tái):
[root@localhost ~]# yum -y install httpd
[root@localhost ~]# echo "192.168.1.2" > /var/www/html/index.html
[root@localhost ~]# systemctl start httpd
第二臺(tái):
[root@localhost ~]# yum -y install httpd
[root@localhost ~]# echo "192.168.1.3" > /var/www/html/index.html
[root@localhost ~]# systemctl start httpd
Nginx測(cè)試效果:
[root@localhost ~]# curl 127.0.0.1
192.168.1.2
[root@localhost ~]# curl 127.0.0.1
192.168.1.3
注意:如果需要在已經(jīng)安裝好的Nginx服務(wù)器上添加第三方模塊,依然需要重新編譯,但為了不覆蓋原本的配置信息,請(qǐng)不要執(zhí)行make install,而是直接復(fù)制可執(zhí)行文件即可!
添加Nginx為系統(tǒng)服務(wù)腳本:
[root@localhost ~]# vim /etc/init.d/nginx
#!/bin/bash
# chkconfig: 2345 99 20
# description: Nginx Service Control Script
PROG="/usr/local/nginx1.10/sbin/nginx"
PIDF="/usr/local/nginx1.10/logs/nginx.pid"
case "$1" in
start)
netstat -anplt |grep ":80" &> /dev/null && pgrep "nginx" &> /dev/null
if [ $? -eq 0 ]
then
echo "Nginx service already running."
else
$PROG -t &> /dev/null
if [ $? -eq 0 ] ; then
$PROG
echo "Nginx service start success."
else
$PROG -t
fi
fi
;;
stop)
netstat -anplt |grep ":80" &> /dev/null && pgrep "nginx" &> /dev/null
if [ $? -eq 0 ]
then
kill -s QUIT $(cat $PIDF)
echo "Nginx service stop success."
else
echo "Nginx service already stop"
fi
;;
restart)
$0 stop
$0 start
;;
status)
netstat -anplt |grep ":80" &> /dev/null && pgrep "nginx" &> /dev/null
if [ $? -eq 0 ]
then
echo "Nginx service is running."
else
echo "Nginx is stop."
fi
;;
reload)
netstat -anplt |grep ":80" &> /dev/null && pgrep "nginx" &> /dev/null
if [ $? -eq 0 ]
then
$PROG -t &> /dev/null
if [ $? -eq 0 ] ; then
kill -s HUP $(cat $PIDF)
echo "reload Nginx config success."
else
$PROG -t
fi
else
echo "Nginx service is not run."
fi
;;
*)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1
esac
[root@localhost ~]# chmod +x /etc/init.d/nginx
[root@localhost ~]# chkconfig --add nginx
[root@localhost ~]# systemctl restart nginx
緩存也就是將一些靜態(tài)文件從后端服務(wù)器緩存到nginx指定的緩存目錄下,既可以減輕后端服務(wù)器負(fù)擔(dān),也可以加快訪問(wèn)速度,但這樣緩存及時(shí)清理就成了一個(gè)頭疼的問(wèn)題。所以需要第三方模塊ngx_cache_purge來(lái)在過(guò)期時(shí)間未到之前,手動(dòng)清理緩存。
proxy模塊常用的指令是proxy_pass和proxy_cache
nginx的web緩存功能只要就是由proxy_cache、fastcgi_cache指令集和相關(guān)指令集完成:
為了使nginx能夠擁有緩存功能,需要修改其配置文件,如下:
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
………… //省略部分內(nèi)容
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'
'"$upstream_cache_status"'; //記錄緩沖命中率,注意這是一個(gè)整段,所以只在末尾有一個(gè)分號(hào)
//以上內(nèi)容原本已經(jīng)存在,只需添加最后一行即可!
access_log logs/access.log main;
proxy_buffering on; //代理時(shí),開(kāi)啟緩沖后端服務(wù)器的響應(yīng)
proxy_temp_path /usr/local/nginx/proxy_temp; //定義緩存臨時(shí)目錄
proxy_cache_path /usr/local/nginx/proxy_cache levels=1:2 keys_zone=my-cache:100m inactive=600m max_size=2g;
//定義緩存目錄,具體信息已在配置文件外部進(jìn)行說(shuō)明
………… //省略部分內(nèi)容
location ~/purge(/.*) { //定義緩存清除策略
allow 127.0.0.1;
allow 192.168.1.0/24;
deny all;
proxy_cache_purge my-cache $host$1$is_args$args;
}
location / {
proxy_pass http://lzj; //請(qǐng)求轉(zhuǎn)向lzj定義的服務(wù)器列表
proxy_redirect off; 指定是否修改被代理服務(wù)器返回的響應(yīng)頭中的 location 頭域跟 refresh 頭域數(shù)值
#例如:
# 設(shè)置后端服務(wù)器“Location”響應(yīng)頭和“Refresh”響應(yīng)頭的替換文本。 假設(shè)后端服務(wù)器返回的
# 響應(yīng)頭是 “Location: http://localhost:8000/two/some/uri/”,那么指令proxy_redirect
# http://localhost:8000/two/ http://frontend/one/;將把字符串改寫為 “Location:
# http://frontend/one/some/uri/”。
proxy_set_header Host $host; //允許重新定義或者添加發(fā)往后端服務(wù)器的請(qǐng)求頭
#Host 的含義是表明請(qǐng)求的主機(jī)名,nginx 反向代理服務(wù)器會(huì)向后端真實(shí)服務(wù)器發(fā)送請(qǐng)求,
#并且請(qǐng)求頭中的host字段重寫為proxy_pass指令設(shè)置的服務(wù)器。因?yàn)閚ginx作為反向代理使
#用,而如果后端真實(shí)的服務(wù)器設(shè)置有類似防盜鏈或者根據(jù) http 請(qǐng)求頭中的 host 字段來(lái)進(jìn)行
#路由或判斷功能的話,如果反向代理層的nginx不重寫請(qǐng)求頭中的host字段,將會(huì)導(dǎo)致請(qǐng)求失敗。
proxy_set_header X-Real-IP $remote_addr;
//web 服務(wù)器端獲得用戶的真實(shí) ip 但是,實(shí)際上要獲得用戶的真實(shí) ip,也可以通過(guò)下面的X-Forward-For
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#后端的 Web服務(wù)器可以通過(guò) X-Forwarded-For 獲取用戶真實(shí) IP,X_Forward_For 字段
#表示該條 http 請(qǐng)求是有誰(shuí)發(fā)起的?如果反向代理服務(wù)器不重寫該請(qǐng)求頭的話,那么后端
#真實(shí)服務(wù)器在處理時(shí)會(huì)認(rèn)為所有的請(qǐng)求都來(lái)自反向代理服務(wù)器,如果后端有防護(hù)策略
#的話,那么機(jī)器就被封掉了。因此,在配置用作反向代理的 nginx 中一般會(huì)增加兩條配置,以便修改 http 的請(qǐng)求頭部
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
#增加故障轉(zhuǎn)移,如果后端的服務(wù)器返回 502、504、執(zhí)行超時(shí)等錯(cuò)誤,
#自動(dòng)將請(qǐng)求轉(zhuǎn)發(fā)到upstream 負(fù)載均衡池中的另一臺(tái)服務(wù)器,實(shí)現(xiàn)故障轉(zhuǎn)移。
proxy_cache my-cache;
add_header Nginx-Cache $upstream_cache_status;
proxy_cache_valid 200 304 301 302 8h;
proxy_cache_valid 404 1m;
proxy_cache_valid any 1d;
proxy_cache_key $host$uri$is_args$args;
expires 30d;
}
[root@localhost ~]# 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
//檢測(cè)配置文件沒(méi)有問(wèn)題
[root@localhost ~]# nginx -s reload //重新加載nginx配置文件
配置選項(xiàng)詳解:
- levels=1:2 keys_zone=my-cache:100m 表示采用 2 級(jí)目錄結(jié)構(gòu),第一層目錄只有一個(gè)字符,是由 levels=1:2 設(shè)置,總共二層目錄,子目錄名字由二個(gè)字符組成。Web 緩存區(qū)名稱為 my-cache,內(nèi)存緩存空間大小為 100MB,這個(gè)緩沖 zone 可以被多次使用;
- inactive=600 max_size=2g 表示 600 分鐘沒(méi)有被訪問(wèn)的內(nèi)容自動(dòng)清除,硬盤最大緩存空間為2GB,超過(guò)這個(gè)大學(xué)將清除最近最少使用的數(shù)據(jù);
- proxy_cache : 引用前面定義的緩存區(qū) my-cache;
- proxy_cache_key :定義如何生成緩存的鍵,設(shè)置 web 緩存的 key 值,nginx 根據(jù) key 值 md5哈希存儲(chǔ)緩存;
- proxy_cache_valid : 為不同的響應(yīng)狀態(tài)碼設(shè)置不同的緩存時(shí)間,比如 200、302 等正常結(jié)果可以緩存的時(shí)間長(zhǎng)點(diǎn),而 404、500 等緩存時(shí)間設(shè)置短一些,這個(gè)時(shí)間到了文件就會(huì)過(guò)期,
而不論是否剛被訪問(wèn)過(guò);- add_header 指令來(lái)設(shè)置 response header, 語(yǔ)法: add_header name value;
- $upstream_cache_status 這個(gè)變量來(lái)顯示緩存的狀態(tài),我們可以在配置中添加一個(gè) http 頭來(lái)顯示這一狀態(tài);
$upstream_cache_status 包含以下幾種狀態(tài):
客戶端瀏覽器訪問(wèn):
使用F5刷新頁(yè)面之后,出現(xiàn)如下頁(yè)面:
清除緩存訪問(wèn)以下路徑,如圖:
如果訪問(wèn)時(shí)訪問(wèn)的URL是:http:192.168.1.1/index.html,那么清除緩存則需要http:192.168.1.1/purge/index.html。
這些從nginx的訪問(wèn)日志中,也可以看出,如圖:
注意:測(cè)試時(shí),注意清除客戶端瀏覽器的緩存!
優(yōu)化Nginx服務(wù)的壓縮功能就需要進(jìn)行以下操作:
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
……………… //省略部分內(nèi)容
http {
include mime.types;
default_type application/octet-stream;
upstream lzj {
sticky;
server 192.168.1.2:80 weight=1 max_fails=2 fail_timeout=10s;
server 192.168.1.3:80 weight=1 max_fails=2 fail_timeout=10s;
}
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'
'"$upstream_cache_status"';
access_log logs/access.log main;
brotli on;
brotli_types text/plain text/css text/xml application/xml application/json;
brotli_static off; //是否允許查找預(yù)處理好的、以 .br結(jié)尾的壓縮文件,可選值為on、off、always。
brotli_comp_level 11; //壓縮的級(jí)別,范圍是“1~14”,值越大,壓縮比越高
brotli_buffers 16 8k; //讀取緩沖區(qū)數(shù)量和大小
brotli_window 512k; //滑動(dòng)窗口大小
brotli_min_length 20; //指定壓縮數(shù)據(jù)的最小字節(jié)
server_tokens off; //隱藏版本信息
sendfile on; //開(kāi)啟高效文件傳輸
keepalive_timeout 65; //長(zhǎng)連接超時(shí)時(shí)間,單位是秒
gzip on; //開(kāi)啟 gzip 壓縮
gzip_comp_level 6; //壓縮的級(jí)別,范圍是“1~6”,值越大,壓縮比越高
gzip_http_version 1.1; //http版本為1.1
gzip_proxied any; // Nginx 作為反向代理的時(shí)候啟用,根據(jù)某些請(qǐng)求和應(yīng)答來(lái)決定是否在對(duì)代理請(qǐng)求的應(yīng)答啟用 gzip 壓縮,是否壓縮取決于請(qǐng)求頭中的“Via”字段,指令中可以同時(shí)指定多個(gè)不同的參數(shù),
常用的參數(shù)如下:
off – 關(guān)閉所有的代理結(jié)果數(shù)據(jù)的壓縮;
expired – 啟用壓縮,如果 header 頭中包含 “Expires” 頭信息;
no-cache – 啟用壓縮,如果 header 頭中包含 “Cache-# Control:no-cache” 頭信息;
private – 啟用壓縮,如果 header 頭中包含 “Cache-Control:private” 頭信息;
no_last_modified – 啟用壓縮,如果 header 頭中不包含 “Last-Modified” 頭信息;
no_etag – 啟用壓縮 ,如果 header 頭中不包含 “ETag” 頭信息;
auth – 啟用壓縮 , 如果 header 頭中包含 “Authorization” 頭信息;
any – 無(wú)條件啟用壓縮;
gzip_min_length 1k;
gzip_buffers 16 8k;
gzip_types text/plain text/css text/xml application/xml application/json;
gzip_vary on;
client_max_body_size 10m;
client_body_buffer_size 128k; //緩沖區(qū)代理緩沖用戶端請(qǐng)求的最大字節(jié)數(shù)
proxy_connect_timeout 75; //nginx 跟后端服務(wù)器連接超時(shí)時(shí)間(代理連接超時(shí))
proxy_read_timeout 75; //定義從后端服務(wù)器讀取響應(yīng)的超時(shí)
proxy_buffer_size 4k; //設(shè)置緩沖區(qū)的大小為 size
proxy_buffers 4 32k; //每塊緩沖區(qū)的大小
proxy_busy_buffers_size 64k; //高負(fù)荷下緩沖大小
proxy_temp_file_write_size 64k; //每次寫臨時(shí)文件的大小
proxy_buffering on;
proxy_temp_path /usr/local/nginx/proxy_temp;
proxy_cache_path /usr/local/nginx/proxy_cache levels=1:2 keys_zone=my-cache:100m inactive=600m max_size=2g;
#sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
#keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
charset utf-8;
……………… //省略部分內(nèi)容,在location規(guī)則最后添加
location /nginx_status {
stub_status on;
access_log off;
allow 192.168.1.0/24;
deny all;
}
[root@localhost ~]# nginx -t
nginx: [emerg] unknown directive "brotli" in /usr/local/nginx/conf/nginx.conf:32
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed
//檢查配置文件發(fā)現(xiàn)brotli這個(gè)工具在編譯時(shí)忘記安裝了(故意的)
接下進(jìn)行安裝原本的模塊:
[root@localhost ~]# cd /usr/src/nginx-1.14.0/ //進(jìn)入源碼包路徑
[root@localhost nginx-1.14.0]# nginx -V //-V查詢編譯安裝時(shí),使用的那些參數(shù)
nginx version: nginx/1.14.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_realip_module --with-http_ssl_module --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client --http-proxy-temp-path=/var/tmp/nginx/proxy --http-fastcgi-temp-path=/var/tmp/nginx/fcgi --with-pcre --add-module=/usr/src/ngx_cache_purge-2.3/ --with-http_flv_module --add-module=/usr/src/nginx-sticky-module/
[root@localhost nginx-1.14.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_realip_module --with-http_ssl_module --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client --http-proxy-temp-path=/var/tmp/nginx/proxy --http-fastcgi-temp-path=/var/tmp/nginx/fcgi --with-pcre --add-module=/usr/src/ngx_cache_purge-2.3/ --with-http_flv_module --add-module=/usr/src/nginx-sticky-module/ --add-module=/usr/src/ngx_brotli && make && make install
//將上述查到的已加載的模塊復(fù)制以下,重新編譯以下,同時(shí),加上需要添加的模塊
//比如我在上面添加了第三方模塊“--add-module=/usr/src/ngx_brotli”
[root@localhost ~]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
//將原本的命令進(jìn)行備份
[root@localhost ~]# cp /usr/src/nginx-1.14.0/objs/nginx /usr/local/nginx/sbin/
//復(fù)制新生成的nginx命令到指定的目錄中
[root@localhost ~]# ln -sf /usr/local/nginx/sbin/nginx /usr/local/sbin/
//對(duì)新命令做一個(gè)強(qiáng)制軟連接
[root@localhost ~]# nginx -s reload //重新加載nginx配置文件
[root@localhost ~]# 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@localhost ~]# nginx -s stop
[root@localhost ~]# nginx
//重新啟動(dòng)nginx服務(wù),注意清除瀏覽器本地的緩存信息
客戶端驗(yàn)證訪問(wèn):
鑒于復(fù)制的問(wèn)題,最后附上本篇博文有關(guān)沒(méi)有注釋的完整的Nginx配置文件:
#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;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
upstream lzj {
sticky;
server 192.168.1.2:80 weight=1 max_fails=2 fail_timeout=10s;
server 192.168.1.3:80 weight=1 max_fails=2 fail_timeout=10s;
}
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'
'"$upstream_cache_status"';
access_log logs/access.log main;
brotli on;
brotli_types text/plain text/css text/xml application/xml application/json;
brotli_static off;
brotli_comp_level 11;
brotli_buffers 16 8k;
brotli_window 512k;
brotli_min_length 20;
server_tokens off;
sendfile on;
keepalive_timeout 65;
gzip on;
gzip_comp_level 6;
gzip_http_version 1.1;
gzip_proxied any;
gzip_min_length 1k;
gzip_buffers 16 8k;
gzip_types text/plain text/css text/xml application/xml application/json;
gzip_vary on;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 75;
proxy_send_timeout 75;
proxy_read_timeout 75;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_buffering on;
proxy_temp_path /usr/local/nginx/proxy_temp;
proxy_cache_path /usr/local/nginx/proxy_cache levels=1:2 keys_zone=my-cache:100m inactive=600m max_size=2g;
#sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
#keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
charset utf-8;
#charset koi8-r;
#access_log logs/host.access.log main;
location ~/purge(/.*) {
allow 127.0.0.1;
allow 192.168.1.0/24;
deny all;
proxy_cache_purge my-cache $host$1$is_args$args;
}
location / {
proxy_pass http://lzj;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_cache my-cache;
add_header Nginx-Cache $upstream_cache_status;
proxy_cache_valid 200 304 301 302 8h;
proxy_cache_valid 404 1m;
proxy_cache_valid any 1d;
proxy_cache_key $host$uri$is_args$args;
expires 30d;
}
location /nginx_status {
stub_status on;
access_log off;
allow 192.168.1.0/24;
deny all;
}
//以下的內(nèi)容沒(méi)有進(jìn)行修改所以就不復(fù)制了
———————— 本文至此結(jié)束,感謝閱讀 ————————
當(dāng)前標(biāo)題:深度優(yōu)化Nginx(一)
文章URL:http://jinyejixie.com/article40/ggsgho.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗(yàn)、網(wǎng)站排名、網(wǎng)站維護(hù)、商城網(wǎng)站、網(wǎng)頁(yè)設(shè)計(jì)公司、網(wǎng)站收錄
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)