十年網(wǎng)站開發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營(yíng)維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
nginx是一個(gè)高性能的HTTP和反向代理服務(wù)器,也是一個(gè)IMAP/POP3/SMTP代理服務(wù)器,nginx是由igor sysoev為俄羅斯訪問量第二的Rambler.ru站點(diǎn)開發(fā)的。
1、nginx安裝與配置
下載地址:http://nginx.org/download/nginx-1.12.1.tar.gz
安裝準(zhǔn)備:nginx依賴于pcre庫,需要先安裝pcre pcre-devel
yum install pcre pcre-devel -y mkdir /root/tools cd /root/tools wget http://nginx.org/download/nginx-1.12.1.tar.gz tar zxf nginx-1.12.1.tar.gz cd nginx-1.12.1 useradd -s /sbin/nologin nginx -M ./configure \ --user=nginx \ --group=nginx \ --prefix=/usr/local/nginx \ --with-http_ssl_module \ --with-http_stub_status_module make && make install echo $? ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/nginx cd /usr/local/nginx[root@localhost nginx]# tree
. ├── conf #配置文件 │ ├── fastcgi.conf │ ├── fastcgi.conf.default │ ├── fastcgi_params │ ├── fastcgi_params.default │ ├── koi-utf │ ├── koi-win │ ├── mime.types │ ├── mime.types.default │ ├── nginx.conf │ ├── nginx.conf.default │ ├── scgi_params │ ├── scgi_params.default │ ├── uwsgi_params │ ├── uwsgi_params.default │ └── win-utf ├── html #網(wǎng)頁文件 │ ├── 50x.html │ └── index.html ├── logs #日志文件 └── sbin #主要二進(jìn)制程序 ├── nginx1)nginx 信號(hào)控制:
TERM,INT quick shutdown
QUIT 優(yōu)雅的關(guān)閉進(jìn)程,即等待請(qǐng)求結(jié)束后再關(guān)閉
HUP 改變配置文件,平滑的重讀配置文件
USR1 重讀日志,在日志按月、日分隔時(shí)使用
USR2 平滑升級(jí)
WINCH 優(yōu)雅關(guān)閉進(jìn)程,配合USR2來進(jìn)行升級(jí)
啟動(dòng)nginx:
/usr/local/nginx/sbin/nginx
重啟動(dòng)nginx:
kill -QUIT `cat /usr/local/nginx/logs/nginx.pid` && /usr/local/nginx/sbin/nginx
重新加載配置文件:
kill -HUP `cat /usr/local/nginx/logs/nginx.pid`
/usr/local/nginx/sbin/nginx -s reload
停止nginx:
kill -INT `cat /usr/local/nginx/logs/nginx.pid`
/usr/local/nginx/sbin/nginx -s stop/quit
重讀日志文件:
kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
/usr/local/nginx/sbin/nginx -s reopen
2)nginx配置文件:
access_log日志格式變量說明:
$remote_addr #遠(yuǎn)程客戶端IP
$remote_user #客戶端用戶信息
$time_local # 本地時(shí)間
$request #請(qǐng)求方式,路徑和版本
$status #響應(yīng)狀態(tài)
$body_bytes_sent #發(fā)送到客戶端的字節(jié)數(shù)
$http_referer #上一次頁面來自哪
$http_user_agent #客戶端代理信息
$http_x_forwarded_for #代理服務(wù)器轉(zhuǎn)發(fā)地址
#user nobody; #運(yùn)行用戶 worker_processes 1; #工作進(jìn)程,一般為CPU數(shù)*核數(shù) #全局錯(cuò)誤日志 #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #PID文件 #pid logs/nginx.pid; events { use epoll; #epoll是多路復(fù)用IO(I/OMultiplexing)中的一種方式,但是僅用于linux2.6以上內(nèi)核,可以大大提高nginx的性能 #一般是配置nginx鏈接的特性,如一個(gè)worK能同時(shí)允許多個(gè)個(gè)鏈接 worker_connections 1024; #指一個(gè)進(jìn)程大允許1024個(gè)鏈接 } http { #配置http服務(wù)器的主要段 include mime.types; #設(shè)定mime類型,類型由mime.type文件定義 default_type application/octet-stream; #設(shè)定日志格式 #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; #指令指定 nginx 是否調(diào)用 sendfile 函數(shù)(zero copy 方式)來輸出文件,對(duì)于普通應(yīng)用,必須設(shè)為 on,如果用來進(jìn)行下載等應(yīng)用磁盤IO重負(fù)載應(yīng)用,可設(shè)置為 off,以平衡磁盤與網(wǎng)絡(luò)I/O處理速度,降低系統(tǒng)的uptime. #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #連接超時(shí)時(shí)間 #gzip on; #開啟gzip壓縮 #設(shè)定請(qǐng)求緩沖 client_header_buffer_size 1k; large_client_header_buffers 4 4k; #設(shè)定負(fù)載均衡的服務(wù)器列表 upstream mysvr { #weigth參數(shù)表示權(quán)值,權(quán)值越高被分配到的幾率越大 #本機(jī)上的Squid開啟3128端口 server 192.168.8.1:3128 weight=5; server 192.168.8.2:80 weight=1; server 192.168.8.3:80 weight=6; } server { #虛擬主機(jī) listen 80; #偵聽80端口 server_name localhost; #定義使用www.xx.com訪問 #charset koi8-r; #設(shè)定本虛擬主機(jī)的訪問日志 #access_log logs/host.access.log main; #默認(rèn)請(qǐng)求 location / { root html; #定義服務(wù)器的默認(rèn)網(wǎng)站根目錄位置 index index.html index.htm; #定義首頁索引文件的名稱 } # 定義錯(cuò)誤提示頁面 #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } #反向代理 #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} #轉(zhuǎn)發(fā)PHP動(dòng)態(tài)頁面給PHP程序處理 #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; }3)實(shí)際應(yīng)用: shell+定時(shí)任務(wù)+nginx信號(hào)管理,完成日志按日期存儲(chǔ)
分析思路:
凌晨00:00:01,把昨天的日志重命名,放在相應(yīng)的目錄下
再USR1信息號(hào)控制nginx重新生成新的日志文件
具體腳本:
#!/bin/bash base_path='/usr/local/nginx/logs' log_path=$(date -d yesterday +"%Y%m") day=$(date -d yesterday +"%d") mkdir -p $base_path/$log_path mv $base_path/access.log $base_path/$log_path/access_$day.log #echo $base_path/$log_path/access_$day.log kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`定時(shí)任務(wù)
Crontab 編輯定時(shí)任務(wù)
01 00 * * * /xxx/path/b.sh 每天0時(shí)1分(建議在02-04點(diǎn)之間,系統(tǒng)負(fù)載小)
4)location 語法
location 有”定位”的意思, 根據(jù)Uri來進(jìn)行不同的定位.
在虛擬主機(jī)的配置中,是必不可少的,location可以把網(wǎng)站的不同部分,定位到不同的處理方式上.
比如, 碰到.php, 如何調(diào)用PHP解釋器? --這時(shí)就需要location
location 的語法
location [=|~|~*|^~] patt {
}
中括號(hào)可以不寫任何參數(shù),此時(shí)稱為一般匹配
也可以寫參數(shù)
因此,大類型可以分為3種
location = patt {} [精準(zhǔn)匹配]
location patt{} [一般匹配]
location ~ patt{} [正則匹配]
如何發(fā)揮作用?:
首先看有沒有精準(zhǔn)匹配,如果有,則停止匹配過程.
location = patt { config A } 如果 $uri == patt,匹配成功,使用config A location = / { root /var/www/html/; index index.htm index.html; } location / { root /usr/local/nginx/html; index index.html index.htm; }如果訪問 http://xxx.com/
定位流程是
1: 精準(zhǔn)匹配中 ”/” ,得到index頁為 index.htm
2: 再次訪問 /index.htm , 此次內(nèi)部轉(zhuǎn)跳uri已經(jīng)是”/index.htm” ,
根目錄為/usr/local/nginx/html
3: 最終結(jié)果,訪問了 /usr/local/nginx/html/index.htm
正則也來參與.
location / { root /usr/local/nginx/html; index index.html index.htm; } location ~ p_w_picpath { root /var/www/p_w_picpath; index index.html; }如果我們?cè)L問 https://cache.yisu.com/upload/information/20200309/32/37286.jpg
此時(shí), “/” 與”/p_w_picpath/logo.png” 匹配
同時(shí),”p_w_picpath”正則 與”p_w_picpath/logo.png”也能匹配,誰發(fā)揮作用?
正則表達(dá)式的成果將會(huì)使用.
圖片真正會(huì)訪問 /var/www/p_w_picpath/logo.png
location / { root /usr/local/nginx/html; index index.html index.htm; } location /foo { root /var/www/html; index index.html; }我們?cè)L問 http://xxx.com/foo
對(duì)于uri “/foo”, 兩個(gè)location的patt,都能匹配他們
即 ‘/’能從左前綴匹配 ‘/foo’, ‘/foo’也能左前綴匹配’/foo’,
此時(shí), 真正訪問 /var/www/html/index.html
原因:’/foo’匹配的更長(zhǎng),因此使用之.;
5)rewrite 重寫
重寫中用到的指令
if (條件) {} 設(shè)定條件,再進(jìn)行重寫
set #設(shè)置變量
return #返回狀態(tài)碼
break #跳出rewrite
rewrite #重寫
If 語法格式
If 空格 (條件) {
重寫模式
}
條件又怎么寫?
答:3種寫法
1: “=”來判斷相等, 用于字符串比較
2: “~” 用正則來匹配(此處的正則區(qū)分大小寫)
~* 不區(qū)分大小寫的正則
3: -f -d -e來判斷是否為文件,為目錄,是否存在.
例子:
if ($remote_addr = 192.168.1.100) { return 403; } if ($http_user_agent ~ MSIE) { rewrite ^.*$ /ie.htm; break; #(不break會(huì)循環(huán)重定向) } if (!-e $document_root$fastcgi_script_name) { rewrite ^.*$ /404.html break; }注, 此處還要加break,以 xx.com/dsafsd.html這個(gè)不存在頁面為例,我們觀察訪問日志, 日志中顯示的訪問路徑,依然是GET /dsafsd.html HTTP/1.1
提示: 服務(wù)器內(nèi)部的rewrite和302跳轉(zhuǎn)不一樣. 跳轉(zhuǎn)的話URL都變了,變成重新http請(qǐng)求404.html, 而內(nèi)部rewrite, 上下文沒變,就是說 fastcgi_script_name 仍然是 dsafsd.html,因此 會(huì)循環(huán)重定向.set 是設(shè)置變量用的, 可以用來達(dá)到多條件判斷時(shí)作標(biāo)志用.達(dá)到apache下的 rewrite_condition的效果
如下: 判斷IE并重寫,且不用break; 我們用set變量來達(dá)到目的
if ($http_user_agent ~* msie) { set $isie 1; } if ($fastcgi_script_name = ie.html) { set $isie 0; } if ($isie 1) { rewrite ^.*$ ie.html; }Rewrite語法
Rewrite 正則表達(dá)式 定向后的位置 模式
Goods-3.html ---->Goods.php?goods_id=3 goods-([\d]+)\.html ---> goods.php?goods_id =$1 location /ecshop { index index.php; rewrite goods-([\d]+)\.html$ /ecshop/goods.php?id=$1; rewrite article-([\d]+)\.html$ /ecshop/article.php?id=$1; rewrite category-(\d+)-b(\d+)\.html /ecshop/category.php?id=$1&brand=$2; 注意:用url重寫時(shí), 正則里如果有”{}”,正則要用雙引號(hào)包起來6)gzip壓縮
原理:瀏覽器---請(qǐng)求----> 聲明可以接受 gzip壓縮 或 deflate壓縮 或compress 或 sdch壓縮
從http協(xié)議的角度看--請(qǐng)求頭 聲明 acceopt-encoding: gzip deflate sdch (是指壓縮算法,其中sdch是google倡導(dǎo)的一種壓縮方式,目前支持的服務(wù)器尚不多)服務(wù)器-->回應(yīng)---把內(nèi)容用gzip方式壓縮---->發(fā)給瀏覽器瀏覽<-----解碼gzip-----接收gzip壓縮內(nèi)容----
gzip配置的常用參數(shù)
gzip on|off; #是否開啟gzip
gzip_buffers 32 4K| 16 8K #緩沖(壓縮在內(nèi)存中緩沖幾塊? 每塊多大?)
gzip_comp_level [1-9] #推薦6 壓縮級(jí)別(級(jí)別越高,壓的越小,越浪費(fèi)CPU計(jì)算資源)
gzip_disable #正則匹配UA 什么樣的Uri不進(jìn)行g(shù)zip
gzip_min_length 200 # 開始?jí)嚎s的最小長(zhǎng)度(再小就不要壓縮了,意義不在)
gzip_http_version 1.0|1.1 # 開始?jí)嚎s的http協(xié)議版本(可以不設(shè)置,目前幾乎全是1.1協(xié)議)
gzip_proxied # 設(shè)置請(qǐng)求者代理服務(wù)器,該如何緩存內(nèi)容
gzip_types text/plain application/xml # 對(duì)哪些類型的文件用壓縮 如txt,xml,html ,css
gzip_vary on|off # 是否傳輸gzip壓縮標(biāo)志
注意:圖片/mp3這樣的二進(jìn)制文件,不必壓縮
因?yàn)閴嚎s率比較小, 比如100->80字節(jié),而且壓縮也是耗費(fèi)CPU資源的.比較小的文件不必壓縮
7)nginx的緩存設(shè)置 提高網(wǎng)站性能
對(duì)于網(wǎng)站的圖片,尤其是新聞?wù)? 圖片一旦發(fā)布, 改動(dòng)的可能是非常小的.我們希望能否在用戶訪問一次后, 圖片緩存在用戶的瀏覽器端,且時(shí)間比較長(zhǎng)的緩存.
可以, 用到 nginx的expires設(shè)置 .
nginx中設(shè)置過期時(shí)間,非常簡(jiǎn)單,在location或if段里,來寫.
格式:expires 30s;
expires 30m;
expires 2h;
expires 30d;
(注意:服務(wù)器的日期要準(zhǔn)確,如果服務(wù)器的日期落后于實(shí)際日期,可能導(dǎo)致緩存失效)
另: 304 也是一種很好的緩存手段
原理是: 服務(wù)器響應(yīng)文件內(nèi)容是,同時(shí)響應(yīng)etag標(biāo)簽(內(nèi)容的簽名,內(nèi)容一變,他也變), 和 last_modified_since 2個(gè)標(biāo)簽值
瀏覽器下次去請(qǐng)求時(shí),頭信息發(fā)送這兩個(gè)標(biāo)簽, 服務(wù)器檢測(cè)文件有沒有發(fā)生變化,如無,直接頭信息返回 etag,last_modified_since
瀏覽器知道內(nèi)容無改變,于是直接調(diào)用本地緩存.
這個(gè)過程,也請(qǐng)求了服務(wù)器,但是傳著的內(nèi)容極少.
對(duì)于變化周期較短的,如靜態(tài)html,js,css,比較適于用這個(gè)方式
8)nginx反向代理服務(wù)器+負(fù)載均衡
用nginx做反向代理和負(fù)載均衡非常簡(jiǎn)單,
支持兩個(gè)用法 1個(gè)proxy, 1個(gè)upstream,分別用來做反向代理,和負(fù)載均衡
upstream負(fù)載平衡機(jī)制:
輪詢-向應(yīng)用服務(wù)器的請(qǐng)求以循環(huán)方式分發(fā):(默認(rèn)配置為輪詢)
http { upstream myapp1 { server srv1.example.com; server srv2.example.com; server srv3.example.com; } server { listen 80; location / { proxy_pass http://myapp1; } } }
最少連接 - 下一個(gè)請(qǐng)求被分配給具有最少數(shù)量的活動(dòng)連接的服務(wù)器:
upstream myapp1 { least_conn; server srv1.example.com; server srv2.example.com; server srv3.example.com; }
ip-hash - 哈希函數(shù)用于確定下一個(gè)請(qǐng)求應(yīng)該選擇哪個(gè)服務(wù)器(基于客戶端的IP地址)。
upstream myapp1 { ip_hash; server srv1.example.com; server srv2.example.com; server srv3.example.com; }
upstream參數(shù)說明:
weight=1 #服務(wù)器的權(quán)重,越大越高,默認(rèn)為1
max_conns=1 #限制與代理服務(wù)器同時(shí)活動(dòng)鏈接的大值,默認(rèn)為0
max_fails=1 #與服務(wù)器連接失敗的次數(shù)內(nèi)將服務(wù)器變?yōu)椴豢捎?,與fail_timeout一起使用,默認(rèn)為1
fail_timeout=10 #指定次數(shù)連接不成功內(nèi)考慮服務(wù)器不可用大時(shí)間
backup #將服務(wù)器標(biāo)記為備份服務(wù)器
down #將服務(wù)器標(biāo)記為永久不可用
以反向代理為例, nginx不自己處理php的相關(guān)請(qǐng)求,而是把php的相關(guān)請(qǐng)求轉(zhuǎn)發(fā)給apache來處理.
反向代理后端如果有多臺(tái)服務(wù)器,自然可形成負(fù)載均衡,
但proxy_pass如何指向多臺(tái)服務(wù)器?
把多臺(tái)服務(wù)器用upstream指定綁定在一起并起個(gè)組名,然后proxy_pass指向該組:
將圖片服務(wù)器做反向代理和負(fù)載均衡:
#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; 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; upstream imagserver{ #配置負(fù)載均衡服務(wù)器 server 172.16.32.100:81 weight=1 max_fails=3 fail_timeout=10; server 172.16.32.100:82 weight=1 max_fails=3 fail_timeout=10; } server { listen81; server_namelocalhost; location / { root html; index index.php index.html index.htm; access_log logs/81_access.log main; } } server { listen82; server_namelocalhost; location / { root html; index index.php index.html index.htm; access_log logs/82_access.log main; } } server { listen 80; server_name localhost; gzip on; gzip_buffers 32 4k; gzip_comp_level 6; gzip_types text/plain p_w_picpath/jpeg application/xml; gzip_vary on; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.php index.html index.htm; } location ~* \.(jpg|jpeg|gif|png){ #配置反向代理,將圖片請(qǐng)求轉(zhuǎn)發(fā)到服務(wù)器組 proxy_set_header X-Forwarded-For $remote_addr; #將客戶端請(qǐng)求IP傳遞到代理服務(wù)器日志$http_x_forwarded_for proxy_pass http://imagserver; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { root html; proxy_set_header X-Forwarded-For $remote_addr; fastcgi_pass 127.0.0.1:9000; include fastcgi.conf; } }---------------------------------end-------------------------------------------------------
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。