一、配置狀態頁:
[root@200 html]#vim /apps/nginx/conf/nginx.conf
location = /nginx_status {
stub_status;
}
php
二、Nginx中設置json格式日誌:
vim /apps/nginx/conf/nginx.conf
http {
include mime.types;
default_type application/octet-stream;html
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"$request_time'; log_format log_json '{ "@timestamp";"$time_local"; "remote_addr": "$remote_addr"; "referer": "$http_referer"; "request": "$request"; "status": $status"; "bytes": $body_bytes_sent"; "agent": "$http_user_agent"; "x_forwarded": "$http_x_forwarded_for"; "up_addr": "$upstream_addr"; "up_host": "$upstream_http_host"; "up_resp_time": "$upstream_response_time"; "request_time": "$request_time"; }'
access_log logs/access.log main json;
重啓並訪問測試!!!node
自定義Nginx日誌轉換成json格式日誌:
日誌中的變量的做用:
\$remote_addr: #客戶端的公網IP,
\$args: #變量中存放了URL的指令,「?:搜索符以後的搜索指令」例如http://www.magedu.net/main/index.do?
id=20190221&partner=search中的id=20190221&partner=search
\$document_root:#保存了針對當前的資源的請求的系統根目錄,如:/apps/nginx/html.
\$document_uri: #保存了當前請求中不包含指令的URI,注意是不包含請求的指令,好比:http://www.magedu.net/main/index.do?id=20190221&partner=search會被定義爲/main/index.do.
\$request_filename: #當前請求的資源文件的路徑名稱,由root或alias指令與URI請求生成的文件絕對路徑,如/apps/nginx/html/main/index.html
\$host: #存放了請求的host名稱。
\$http_user_agent::#客戶端瀏覽器的詳細信息。:"Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:65.0) Gecko/2
0100101 Firefox/65.0"
\$http_cookie: #客戶端的cookie信息。
limit_rate 10240: echo $limit_rate:
#若是Nginx服務器還用limit_rate配置了顯示網絡速率,則會顯示,若是沒設置,則顯示0。
$remote_port: #客戶端請求Nginx服務器時隨機打開的端口,這是每一個客戶端本身的端口。
\$remote_user: #已經通過Auth Basic Module 驗證的用戶名。
\$request_body_file: #作反向代理時發給後端服務器的本地資源的名稱。
\$request_method: #請求資源的方式,GET/PUT/DELETE等。
\$request_uri: #包含請求參數的原始URI,不包含主機名,如:/main/index.do?id=20190221&partner=search.
\$scheme: #請求的協議,如ftp,https,http等。
\$server_protocol: #保存了客戶端請求資源使用的協議的版本。
\$server_addr: #保存了服務器的IP地址。
\$server_name: #保存了服務器的主機名。
\$server_port: #請求的服務器的端口號。
\$time_local: #[22/Feb/2019:08:44:14 +0800]
\$request: #經過哪一個協議作的請求.: "GET /favicon.ico HTTP/1.1"
\$status 404
\$body_bytes_sent : 主機發送的字節數:162mysql
1、自定義訪問日誌:
訪問日誌是記錄客戶端即用戶的具體請求內容信息,全局配置模塊中的error_log是記錄Nginx服務器運行時的日誌保存路徑和記錄日誌的level,所以有着本質的區別,並且Nginx的錯誤日誌通常只有一個,可是訪問日誌能夠在不一樣server中定義多個,定義一個日誌須要使用access_log指定日誌的 保存路徑,使用log_format指定日誌的格式,格式中定義要保護的具體日誌內容。linux
log_format nginx_formatl '$remote_addr - $remote_user [$time_local]
"$request" '
'$status $ body_bytes_sent "$http_referer」'
'$http_user_agent" "http_x_forwarded_for" '
"$server_name: $server_port' ;
access_log logs/access.log nginx_formatl ;nginx
#重啓Nginx並訪問測試日誌格式
==> /apps/nginx/logs/access.log <==
192.168.0.1 - - [22/Feb/2019:08:44:14 +0800] "GET /favicon.ico HTTP/1.1" 404 162 "-
" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:65.0) Gecko/2
0100101 Firefox/65.0" "-"www.magedu.net:80web
2、自定義json格式日誌:
Nginx的默認訪問日誌記錄內容相對較單一,默認的格式也不方便後期作日誌統計分析,生產環境中一般將Nginx日誌轉換爲json日誌,而後配合使用ELK作日誌收集-統計-分析。算法
log_format access_json '{"@timestamp":"$time_iso8601",'
'"host":"$server_addr",'
'"clientip":"$remote_addr",'
'"size":$body_bytes_sent,'
'"responsetime":$request_time,'
'"upstreamtime":"$upstream_response_time",'
'"upstreamhost":"$upstream_addr",'
'"http_host":"$host",'
'"uri":"$uri",'
'"domain":"$host",'
'"xff":"$http_x_forwarded_for",'
'"referer":"$http_referer",'
'"tcp_xff":"$proxy_protocol_addr",'
'"http_user_agent":"$http_user_agent",'
'"status":"$status"}';
access_log /apps/nginx/logs/access_json.log access_json。sql
重啓Nginx並訪問測試日誌格式express
{"@timestamp":"2019-02-
22T08:55:32+08:00","host":"192.168.7.102","clientip":"192.168.0.1","size":162,"resp
onsetime":0.000,"upstreamtime":"-","upstreamhost":"-
","http_host":"www.magedu.net","uri":"/favicon.ico","domain":"www.magedu.net","xff"
:"-","referer":"-","tcp_xff":"","http_user_agent":"Mozilla/5.0 (Windows NT 6.1;
Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0","status":"404"}
one、自簽名證書
三、單個域名的證書頒發:
1 cd /apps/nginx/conf
2 mkdir certs cd certs/
3 openssl req -newkey rsa:4096 -nodes -sha256 -keyout ca.key -x509 -days 3650 -out ca.crt #自簽名CA證書
4 openssl req -newkey rsa:4096 -nodes -sha256 -keyout www.magedu.net.key -out www.magedu.net.csr #自制key和csr
5 openssl x509 -req -days 3650 -in www.magedu.net.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out www.magedu.net.crt #簽發證書
6 openssl x509 -in www.magedu.net.crt -noout -text #驗證證書內容
7 [root@200 certs]#ll
total 36
-rw-r--r--. 1 root root 2017 Mar 17 20:52 ca.crt
-rw-r--r--. 1 root root 3272 Mar 17 20:52 ca.key
-rw-r--r--. 1 root root 17 Mar 17 21:45 ca.srl
-rw-r--r--. 1 root root 1891 Mar 17 21:45 mobile.magedu.net.crt
-rw-r--r--. 1 root root 1691 Mar 17 21:44 mobile.magedu.net.csr
-rw-r--r--. 1 root root 3272 Mar 17 21:44 mobile.magedu.net.key
-rw-r--r--. 1 root root 1903 Mar 17 20:59 www.magedu.net.crt
-rw-r--r--. 1 root root 1704 Mar 17 20:57 www.magedu.net.csr
-rw-r--r--. 1 root root 3272 Mar 17 20:57 www.magedu.net.key crt與key是配套的
8 [root@200 certs]#vim /apps/nginx/conf/conf.d/pc.conf
server {
listen 80;
listen 443 ssl;
server_name www.magedu.net;
ssl_certificate /apps/nginx/conf/certs/www.magedu.net.crt;
ssl_certificate_key /apps/nginx/certs/www.magedu.net.key;
/apps/nginx/sbin/nginx -t /-s reload
去測試一下:(注:第一次訪問有些慢,服務器端把證書發給客戶端,客戶端生成隨機數,把隨機數發給服務器,服務器加密後再發給客戶端,因此會有延遲) 第二段結束。
三、將mobile web移動端變成https:
[root@200 certs]#vim /apps/nginx/conf/conf.d/mobile.conf
server {
listen 80;
listen 443 ssl;
server_name mobile.magedu.net;
ssl_certificate /apps/nginx/conf/certs/mobile.magedu.net.crt;
ssl_certificate_key /apps/nginx/conf/certs/mobile.magedu.net.key;
在訪問網頁以前,先在c盤hosts文件中加入「mobile magedu.net」
two、HTTPs功能:
Web網站的登陸頁面都是使用https加密傳輸的,加密數據以保障數據的安全,HTTPS可以加密信息,以避免敏感信息被第三方獲取,因此不少銀行網站或電子郵箱等等安全級別較高的服務都會採用HTTPS協議,HTTPS實際上是有兩部分組成:HTTP + SSL / TLS,也就是在HTTP上又加了一層處理加密信息的模塊。服務端和客戶端的信息傳輸都會經過TLS進行加密,因此傳輸的數據都是加密後的數據。
https 實現過程以下:
1.客戶端發起HTTPS請求: 客戶端訪問某個web端的https地址,通常都是443端口
2.服務端的配置:
採用https協議的服務器必需要有一套證書,能夠經過一些組織申請,也能夠本身製做,目前國內不少網站都本身作的,當你訪問一個網站的時候提示證書不可信任就表示證書是本身作的,證書就是一個公鑰和私鑰匙,就像一把鎖和鑰匙,正常狀況下只有你的鑰匙能夠打開你的鎖,你能夠把這個送給別人讓他鎖住一個箱子,裏面放滿了錢或祕密,別人不知道里面放了什麼並且別人也打不開,只有你的鑰匙是能夠打開的。
3.傳送證書:
服務端給客戶端傳遞證書,其實就是公鑰,裏面包含了不少信息,例如證書獲得頒發機構、過時時間等等。
4.客戶端解析證書:
這部分工做是有客戶端完成的,首先會驗證公鑰的有效性,好比頒發機構、過時時間等等,若是發現異常則會彈出一個警告框提示證書可能存在問題,若是證書沒有問題就生成一個隨機值,而後用證書對該隨機值進行加密,就像2步驟所說把隨機值鎖起來,不讓別人看到。
5.傳送4步驟的加密數據:
就是將用證書加密後的隨機值傳遞給服務器,目的就是爲了讓服務器獲得這個隨機值,之後客戶端和服務端的通訊就能夠經過這個隨機值進行加密解密了。
6.服務端解密信息:
服務端用私鑰解密5步驟加密後的隨機值以後,獲得了客戶端傳過來的隨機值(私鑰),而後把內容經過該值進行對稱加密,對稱加密就是將信息和私鑰經過某種算法混合在一塊兒,這樣除非你知道私鑰,否則是沒法獲取其內部的內容,而正好客戶端和服務端都知道這個私鑰,因此只要機密算法夠複雜就能夠保證數據的安全性。
7.傳輸加密後的信息:
服務端將用私鑰加密後的數據傳遞給客戶端,在客戶端能夠被還原出原數據內容。
8.客戶端解密信息:
客戶端用以前生成的私鑰獲解密服務端傳遞過來的數據,因爲數據一直是加密得,所以即便第三方獲取到數據也沒法知道其詳細內容。
three、Nginx Rewrite功能:
Nginx服務器利用ngx_http_rewrite_module 模塊解析和處理rewrite請求,因此說此功能依靠 PCRE(perlcompatible regularexpression),所以編譯以前要安裝PCRE庫,rewrite是nginx服務器的重要功能之一,用於實現URL的重寫,URL的重寫是很是有用的功能,好比它能夠在咱們改變網站結構以後,不須要客戶端修改原來的書籤,也無需其餘網站修改咱們的連接,就能夠設置爲訪問,另外還能夠在必定程度上提升網站的安全性。
rewrite flag :
利用Nginx的rewrite的指令,能夠實現url的從新跳轉,四種不一樣的flag:
1、redirect(臨時重定向)、 2、permanent(永久重定向)、 :跳轉型的flag,是指有客戶端瀏覽器從新對新地址進行請求,3、break 4、last :代理型是在WEB服務器內部實現的跳轉。
redirect;#臨時重定向,重寫完成後以臨時重定向方式直接返回重寫後生成的新URL給客戶端,由客戶端從新發起請求;使用相對路徑,或者http://或https://開頭,狀態碼:302
permanent;#重寫完成後以永久重定向方式直接返回重寫後生成的新URL給客戶端,由客戶端從新發起請求,狀態碼:301
last;#重寫完成後中止對當前URI在當前location中後續的其它重寫操做,然後對新的URL啓動新一輪重寫檢查;提早重啓新一輪循環,不建議在location中使用
break;#重寫完成後中止對當前URL在當前location中後續的其它重寫操做,然後直接跳轉至重寫規則配置塊以後的其它配置;結束循環,建議在location中使用
示例:將訪問源域名 www.magedu.net 的請求永久重定向到www.magedu.com。
注意:臨時重定向不會緩存域名解析記錄(A記錄),可是永久重定向會緩存,例如:京東早期的域名 www.360buy.com 因爲與360公司相似,因而後期永久重定向到了 www.jd.com ,永久重定向會緩存DNS解析記錄。
vim /apps/nginx/conf/conf.d/pc.conf
location / {
root /data/nginx/html/pc;
index index.html;
rewrite / http://www.magedu.com permanent;
}
#重啓Nginx並訪問域名www.magedu.net進行測試:(訪問前)
訪問後:
臨時重定向:域名臨時重定向,告訴瀏覽器域名不是固定重定向到當前目標域名,後期可能隨時會更改,所以瀏覽器不會緩存當前域名的解析記錄。
vim /apps/nginx/conf/conf.d/pc.conf
location / {
root /data/nginx/html/pc;
index index.html;
rewrite / http://www.magedu.com redirect;
}
#重啓Nginx並訪問域名www.magedu.net進行測試:(訪問前)
訪問後:
rewrite自動跳轉https
server {
listen 443 ssl;
listen 80;
ssl_certificate /apps/nginx/certs/www.magedu.net.crt;
ssl_certificate_key /apps/nginx/certs/www.magedu.net.key;
ssl_session_cache shared:sslcache:20m;
ssl_session_timeout 10m;
server_name www.magedu.net;
location / {
root /data/nginx/html/pc;
index index.html;
if ($scheme = http ){ #未加條件判斷,會致使死循環
rewrite / https://www.magedu.net permanent;
}
}
[root@200 ~]#vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.18.9.200 www.magedu.net 把該IP加入hosts中,當curl時纔會被找到:
[root@200 ~]#curl -L -k -i https://www.magedu.net
HTTP/1.1 200 OK
Server: nginx/1.14.2
Date: Mon, 18 Mar 2019 14:39:26 GMT
Content-Type: text/html
Content-Length: 7
Last-Modified: Fri, 15 Mar 2019 10:43:23 GMT
Connection: keep-alive
Keep-Alive: timeout=65
ETag: "5c8b81cb-7"
Accept-Ranges: bytes
pc web
rewrite判斷文件是否存在
當用戶訪問到公司網站的時輸入了一個錯誤的URL,能夠將用戶重定向至官網首頁。 最經常使用!!!!
location / {
root /data/nginx/html/pc;
index index.html;
if (!-f $request_filename) {
#return 404 "linux35";
rewrite (.*) http://www.magedu.net/index.html;
}
}
#重啓Nginx並訪問測試:訪問前:
訪問後跳轉:
four、Nginx防盜鏈
防盜鏈基於客戶端攜帶的referer實現,referer是記錄打開一個頁面以前記錄是從哪一個頁面跳轉過來的標記信息,若別人只連接了本身網站圖片或某個單獨的資源,而不是打開了網站的整個頁面,這就是盜鏈,referer就是以前的那個網站域名,
例題:在一個web 站點盜鏈另外一個站點的資源信息,好比圖片、視頻等
先作盜鏈再作防盜鏈:
一、mkdir /data/nginx/html/mageedu
vim /data/nginx/html/mageedu 準備盜鏈web頁面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>盜鏈頁面</title>
</head>
<body>
<a href="http://www.magedu.net">測試盜鏈</a>;
<img src="http://www.magedu.net/1.jpg">;
</body>
</html>
二、vim /data/nginx/html/mageedu/index.html 加上這個配置文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>盜鏈頁面</title>
</head>
<body>
<a href="http://www.magedu.net">測試盜鏈</a>;
<img src="http://www.magedu.net/1.jpg">;
</body>
</html>
三、vim /apps/nginx/conf/conf.d/mageedu.conf 定義配置文件
server {
listen 80;
server_name www.mageedu.net;
location / {
index index.html;
root "/data/nginx/html/mageedu";
access_log /apps/nginx/logs/www.mageedu.net.log access_json;
}
}
四、在c盤hosts文件中加入「172.18.9.200 www.mageedu.net」 讓互聯網可以解析到本主機的地址。
五、訪問並測試:
five、Nginx HTTP 反向代理
上圖解釋:同構:web服務器(nginx)替用戶構建請求的資源發日後端,再從新將數據封裝發給用戶,異構:用戶使用http請求來的php頁面,nginx沒法用tcp向後端轉發,tcp就用fastcgi向後端轉發,4層負載均衡服務器負責負載,7層負載均衡服務器負責代理,tcp纔有負載均衡的功能。
反向代理:也叫reverse proxy,指的是代理外網用戶的請求到內部的指定web服務器,並將數據返回給用戶的一種方式,這是用的比較多的一種方式
gx_http_proxy_module: 將客戶端的請求以http協議轉發至指定服務器進行處理。
ngx_stream_proxy_module:將客戶端的請求以tcp協議轉發至指定服務器處理。
ngx_http_fastcgi_module:將客戶端對php的請求以fastcgi協議轉發至指定服務器處理。
例題:將用戶對域www.magedu.net的請求轉發至後端服務器處理
環境準備:172.18.9.200 :nginx代理服務器、172.18.9.150 :後端web1 apache部署、172.18.9.100 :後端web2 apache部署
一、100/150主機:yum install httpd -y echo "web1 192.168.7.103" > /var/www/html/index.html systemctl start httpd && systemctl enable httpd
A狀況:
location /web {
index index.html;
proxy_pass http://172.18.9.150:80;
#不帶斜線將訪問的/web,等於訪問後端服務器 http://172.18.9.150:80/web/index.html,即後端服務器配置的站點根目錄要有web目錄才能夠被訪問,這是一個追加/web到後端服務器http://servername:port/WEB/INDEX.HTML的操做
#重啓Nginx測試訪問效果:
二、vim /apps/nginx/conf/conf.d/pc.conf
location /web {
index index.html;
proxy_pass http://172.18.9.150:80;
}
/apps/nginx/sbin/nginx -t
[root@200 ~]#/apps/nginx/sbin/nginx -s reload
三、訪問測試,由於150主機沒有東西,因此顯示找不到
B狀況:
proxy_pass http://172.18.9.150:80/;
#帶斜線,等於訪問後端服務器的http://172.18.9.150:80/;內容返回給客戶端
訪問測試:以下
C狀況:
proxy_pass http://172.18.9.150:80; #不帶斜線將訪問的/web
在150主機:一、cd /var/www/html 二、mkdir web 三、vim web/index.html : test web_page
四、訪問測試:
例題:反向代理示例--指定location
一、vim /apps/nginx/conf/conf.d/pc.cpnf
server {
listen 80;
listen 443 ssl;
server_name www.magedu.net;
root /data/nginx/html/pc;
index index.html index.php;
}
location /web {
proxy_pass http://172.18.9.100:80/;
}
/apps/nginx/sbin/ngix -t / -s reload
二、[root@s150 ~]# mkdir /var/www/html/web
[root@s150 ~]# echo "web1 page for apache" > /var/www/html/web/index.html
[root@s100 ~]# mkdir /var/www/html/web
[root@s100 ~]# echo "web2 page for apache" > /var/www/html/web/index.html
三、重啓訪問測試:
[root@150 ~]#curl http://www.magedu.net/
pc web
[root@·50 ~]#curl http://www.magedu.net/web
web2 172.18.9.100
四、#Apache的訪問日誌:
[root@100 ~]#tail -f /var/log/httpd/access_log
172.18.140.177 - - 2019-03-20 19:37:25 "GET / HTTP/1.1" 200 13 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36"
172.18.140.177 - - 2019-03-20 19:37:25 "GET /favicon.ico HTTP/1.1" 404 209 "http://172.18.9.100/" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36"
172.18.9.200 - - 2019-03-20 22:26:51 "GET / HTTP/1.0" 200 18 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36"
172.18.9.200 - - 2019-03-20 22:28:28 "GET / HTTP/1.0" 200 18 "-" "curl/7.29.0"
172.18.9.200 - - 2019-03-20 22:34:59 "GET / HTTP/1.0" 200 18 "-" "curl/7.29.0" 着條記錄說明剛剛訪問已被記錄在冊。
反向代理的緩存功能:
一、vim /apps/nginx/conf/nginx.conf
proxy_cache_path /data/nginx/proxycache levels=1:1:1 keys_zone=proxycache:20m
inactive=120s max_size=1g; #配置在nginx.conf http配置段
二、vim /apps/nginx/conf/conf.d/pc.conf
location /web { #要緩存的URL 或者放在server配置項對全部URL都進行緩存
proxy_pass http://172.18.9.100:80/;
proxy_set_header clientip $remote_addr;
proxy_cache proxycache;
proxy_cache_key $request_uri;
proxy_cache_valid 200 302 301 1h;
proxy_cache_valid any 1m;
}
三、 /apps/nginx/sbin/nginx -t 四、systemctl start nginx
五、驗證緩存目錄結構及文件大小:
[root200 ~]#tree /data/nginx/proxycache/
/data/nginx/proxycache/
├── 8
│ └── c0
│ └── 4a
│ └── 21f470a48e953a199103a3a4c064ac08
└── f
└── 60
└── b0
└── 50b643197ae7d66aaaa5e7e1961b060f
6 directories, 2 files
six:添加頭部報文信息:
nginx的配置:
vim /apps/nginx/conf/conf.d/pc.conf
location /web {
proxy_pass http://192.168.7.103:80/;
proxy_set_header clientip $remote_addr;
proxy_cache proxycache;
proxy_cache_key $request_uri;
proxy_cache_valid 200 302 301 1h;
proxy_cache_valid any 1m;
add_header X-Via $server_addr;
add_header X-Cache $upstream_cache_status;
add_header X-Accel $server_name;最後三項是添加的自定義頭部。
驗證頭部信息:
[root@centos7 ~]#curl -I http://www.magedu.net/web/index.html
HTTP/1.1 200 OK
Server: nginx/1.14.2
Date: Thu, 21 Mar 2019 01:15:11 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 18
Connection: keep-alive
Keep-Alive: timeout=65
Last-Modified: Wed, 20 Mar 2019 11:41:18 GMT
ETag: "12-58485199e4174"
X-Via: 172.18.9.200
X-Cache: MISS 第一次訪問沒有使用緩存
X-Accel: www.magedu.net
Accept-Ranges: bytes
[root@centos7 ~]#curl -I http://www.magedu.net/web/index.html
HTTP/1.1 200 OK
Server: nginx/1.14.2
Date: Thu, 21 Mar 2019 01:15:25 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 18
Connection: keep-alive
Keep-Alive: timeout=65
Last-Modified: Wed, 20 Mar 2019 11:41:18 GMT
ETag: "12-58485199e4174"
X-Via: 172.18.9.200
X-Cache: HIT 第二次訪問命中緩存
X-Accel: www.magedu.net
Accept-Ranges: bytes
vim /apps/nginx/conf/conf.d/pc.conf
upstream webserver {
server 172.18.9.100 weight=1 max_fails=3 fail_timeout=5; 添加權重等信息。
}
location /web {
#proxy_cache proxycache;
#proxy_cache_key $request_uri;
#proxy_cache_valid 200 302 301 1h;
#proxy_cache_valid any 1m;
#add_header X-Via $server_addr;
#add_header X-Cache $upstream_cache_status;
#add_header X-Accel $server_name; 緩存給註釋。
}
訪問測試:下圖是輪詢效果:
上圖解析:一致性哈希就是把後端服務器放在左表,該圈圈的值是0-(2^32-1),咱們訪問某一個服務器down機,它會依次往返,因此,當一個服務器發生變化時,其餘的不受影響,例如右圖,×××部分,添加一個服務器,它只會影響哈希值是×××部分的的用戶請求,用一致性哈希的狀況較多。
當咱們標註了一致性哈希,因爲訪問的url是一個,因此會被調度到同一個服務器上,以下:
vim /apps/nginx/conf/conf.d/pc.conf
upstream webserver {
server 172.18.9.100 weight=1 max_fails=3 fail_timeout=5;
hash $request_uri consistent;
} 下圖是100主機頁面:
一、vim /apps/nginx/conf/conf.d/pc.conf
location ~* .(gif|jpg|bmp|tiff|ico|wmf|js)$ {
root /data/nginx/html/images;
index index.html;
}
二、cat /data/nginx/html/images/
total 84
-rw-r--r--. 1 root root 61790 Mar 16 19:54 1.jpg
-rw-r--r--. 1 root root 20255 Feb 7 14:19 2.jpg 上傳一張圖片命名爲2.jpg
訪問測試:
seven、負載均衡實例:MySQL
一、在100主機:[root@100 ~]# yum install mariadb mariadb-server -y / systemctl start mariadb
[root@centos7 ~]#ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 50 :3306 :*
二、[root@100 ~]#mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.60-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> GRANT ALL PRIVILEGES ON . TO 'root'@'%' IDENTIFIED BY '123gxy' WITH
Query OK, 0 rows affected (0.02 sec) 進行受權,不然沒法登錄。
三、 在200主機:[root@200 ~]#cd /apps/nginx/conf
mkdir tcp ---> cd tcp/ ---> vim mariadb.conf
stream {
upstream mariadb_server {
server 172.18.9.100:3306 max_fails=3 fail_timeout=30s;
}
server {
listen 3306;
proxy_connect_timeout 10s;
proxy_timeout 10s;
proxy_pass mariadb_server;
}
}
四、在200主機、vim /apps/nginx/conf/nginx.conf
將「include /apps/nginx/conf/tcp/mariadb.conf; 」添加在http { 配置項外面。
/apps/nginx/sbin/nginx -t / -s reload
五、[root@100 ~]#mysql -h172.18.9.200 -p123gxy
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 5.5.60-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.01 sec)
eight、實現Fastcgi:
fastcgi官網:http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html#fastcgi_param
1、yum安裝安裝php5.4
若沒有epel源,要先下載:yum install epel-release
yum install php-fpm php-mysql 會生成/etc/php.ini文件,是編譯安裝不會生成的。
[root@150 ~]#grep -v ";" /etc/php-fpm.d/www.conf |grep -v "^$" 將配置文件過濾掉多餘的信息。
[www]
listen = 127.0.0.1:9000
listen.allowed_clients = 127.0.0.1
user = nginx
group = nginx
pm = dynamic
pm.max_children = 100
pm.start_servers = 20
pm.min_spare_servers =20
pm.max_spare_servers = 35
slowlog = /var/log/php-fpm/www-slow.log
php_admin_value[error_log] = /var/log/php-fpm/www-error.log
php_admin_flag[log_errors] = on
php_value[session.save_handler] = files
php_value[session.save_path] = /var/lib/php/session
systemctl start php-fpm ss -ntl:
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 127.0.0.1:9000 :
會生成4個主要的目錄:
一、conf:保存nginx全部的配置文件,其中nginx.conf是nginx服務器的最核心最主要的配置文件,其餘的.conf則是用來配置nginx相關的功能的,例如fastcgi功能使用的是fastcgi.conf和fastcgi_params兩個文件,配置文件通常都有個樣板配置文件,是文件名.default結尾,使用的使用將其複製爲並將default去掉便可。
二、html目錄中保存了nginx服務器的web文件,可是能夠更改成其餘目錄保存web文件,另外還有一個50x的web文件是默認的錯誤頁面提示頁面。
三、logs:用來保存nginx服務器的訪問日誌錯誤日誌等日誌,logs目錄能夠放在其餘路徑,好比/var/logs/nginx裏面。
四、sbin:保存nginx二進制啓動腳本,能夠接受不一樣的參數以實現不一樣的功能
示例:準備php測試頁面:
1 mkdir /data/php
2 vim /data/php/index.php
<?php
phpinfo();
?>
3 vim /apps/nginx/conf/conf.d/pc.conf 在指定文件配置 FastCGI,
location ~ .php$ {
#root /data/nginx/php;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/php$fastcgi_script_name;
#fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#若是SCRIPT_FILENAME是絕對路徑則能夠省略root /data/nginx/php;
include fastcgi_params;
4 systemctl start php-fpm 並訪問測試:
2、咱們也能夠以下:在配置fastcgi_param時,利用$document_root變量取到/data/php,再使用$fastcgi_script_name獲取到index.php頁面。(最經常使用)
vim /apps/nginx/conf/conf.d/pc.conf
location ~ .php$ {
root /data/php; 定義根目錄
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
訪問測試:
3、1 vim /etc/php-fpm.d/www.conf
pm.status_path = /pm_status ping.path = /ping ping.response = pong 這三項取消註釋。
2 vim /apps/nginx/conf/conf.d/pc.conf
location ~ ^/(pm_status|ping)$ {
#access_log off;
#allow 127.0.0.1;
#deny all;
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name;
}
三、下圖就是實現了小型的監控:
FastCGI示例--Nginx與php不在同一個服務器:yum安裝較新版本的php-fpm
環境:200主機:Nginx服務器、150主機:php-fpm服務器
php-fpm安裝在個人150主機上,和Nginx服務器分開,
一、[root@150 data]#wget https://mirrors.tuna.tsinghua.edu.cn/remi/enterprise/7/
二、[root@150 data]#yum install php56-php-fpm php56-php-mysql
三、建立Nginx用戶:useradd nginx -s /sbin/nologin -u 2000 (每臺主機的用戶id都要同樣才行)。
id nginx uid=2000(nginx) gid=2000(nginx) groups=2000(nginx)
[root@150 yum.repos.d]#rpm -ql php56-php-fpm
/etc/logrotate.d/php56-php-fpm
/etc/systemd/system/php56-php-fpm.service.d
/opt/remi/php56/root/etc/php-fpm.conf
/opt/remi/php56/root/etc/php-fpm.d
/opt/remi/php56/root/etc/php-fpm.d/www.conf
/opt/remi/php56/root/etc/sysconfig/php-fpm
/opt/remi/php56/root/usr/sbin/php-fpm
/opt/remi/php56/root/usr/share/doc/php56-php-fpm-5.6.40
/opt/remi/php56/root/usr/share/doc/php56-php-fpm-5.6.40/php-fpm.conf.default
/opt/remi/php56/root/usr/share/fpm
/opt/remi/php56/root/usr/share/fpm/status.html
/opt/remi/php56/root/usr/share/licenses/php56-php-fpm-5.6.40
/opt/remi/php56/root/usr/share/licenses/php56-php-fpm-5.6.40/fpm_LICENSE
/opt/remi/php56/root/usr/share/man/man8/php-fpm.8.gz
/opt/remi/php56/root/var/lib/php/session
/opt/remi/php56/root/var/lib/php/wsdlcache
/opt/remi/php56/root/var/log/php-fpm
/opt/remi/php56/root/var/run/php-fpm
/usr/lib/systemd/system/php56-php-fpm.service
四、[root@150 yum.repos.d]#vim /opt/remi/php56/root/etc/php-fpm.d/www.conf
user = nginx
group = nginx
listen = 172.18.9.150:9000vim
;listen.allowed_clients = 127.0.0.1 這一行務必註釋掉,容許外網訪問,不然會報502錯誤,
五、[root@150 web]#systemctl restart php56-php-fpm
[root@150 web]#ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 172.18.9.150:9000 :
六、mkdir /data/php -p
vim /data/php/index.php
<?php
phpinfo();
?>
七、如今去配置Nginx訪問:
location ~ .php$ {
root /data/php;
fastcgi_pass 172.18.9.150:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
八、/apps/nginx/sbin/nginx -t / -s reload
九、訪問測試:新版5.6已呈現: