Nginx反向代理、負載均衡、緩存、URL重寫及讀寫分離
Nginx配置文件
server {
listen 80;
server_name www.mylinux.com;
location / {
後端服務器:
}
}
反向代理:
proxy_pass
Nginx通過proxy模塊實現反向代理,在作爲web反向代理服務器時,nginx負責接收客戶端請求,並能夠根據URI,客戶端參數或其他
的處理邏輯將用戶請求調度至上游服務器上(upstream server),nginx在實現反向代理功能時的最重要指令爲proxy_pass,她能夠將,
location定義爲某URI代理至指定的上游服務器(組)上,如下面所示列中,localtion的/forum將被替換爲上游服務器上的/bbs
location /forum/ {
proxy_pass http://172.16.100.11:8080/bbs/;
}
http://www/mylinux.com/forum
-->http://172.16.100.11:8080/bbs/;
-----------------------------------------------------
不過,這種處理機制中有兩個例外,一個是如果location的URI是通過模式匹配定義的,其URI將直接被傳遞至上游服務器,而不能爲
其指定轉換的另一個URI,列如下面
location ~* ^/forum {
proxy_pass http://172.16.100.11:8080/;
}
http://www/mylinux.com/forum
-->http://172.16.100.11:8080/forum/;
-----------------------------------------------------
第二個列外是,如果在location中使用URL重定向,那麼nginx將使用重定向後的URI處理請求,而不再考慮上游服務器定義URI,如下
面所示的列子中,傳送給上游服務器的URI爲index.php?page=<match>,而不是/index.
location / {
rewrite / (.*)$ /index.php?page=$1 break;
proxy_pass http://localhost:8080/index;
}
--------------------------------------------------
實驗:
在192.168.1.11上轉上web服務 作爲後端服務器;
通過192.168.1.10上的nginx代理;
192.168.1.11:
[root@localhost ~]# yum -y install httpd
[root@localhost ~]# mkdir /var/www/html/bbs
[root@localhost ~]# vim /var/www/html/bbs/index.html
[root@localhost ~]# service httpd start
192.168.1.10:
location /forum/ {
proxy_pass http://192.168.1.11/bbs/;
}
----------------------------------------------------
實驗2:
192.168.1.11:
[root@localhost html]# mv bbs forum
192.168.1.10:
location ~* ^/forum {
proxy_pass http://192.168.1.11;
}
[root@localhost ~]# service httpd restart
----------------------------------------------------
讓後臺的http服務器日誌信息記錄客戶端訪問ip而不是nginx的ip
192.168.1.10:
location ~* ^/forum {
proxy_pass http://192.168.1.11;
proxy_set_header X-Real-IP $remote_addr;
}
192.168.1.11:
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
LogFormat "%{X-Real-IP}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
#LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
h%代表客戶端ip地址。替換成X-Real-IP}i i表示引用這個值
---------------------------------------------------
把整個網頁全部轉向後端:
192.168.1.10:
location / {
proxy_pass http://192.168.1.11/;
proxy_set_header X-Real-IP $remote_addr;
}
192.168.1.11:
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
LogFormat "%{X-Real-IP}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
#LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
--------------------------------------------------------------
nginx實現負載均勻,以及後臺健康狀態檢查:
upstream:定義一個組 實現負載均勻:
192.168.1.11 http
192.168.1.12 http
192.168.1.10 nginx
一.192.168.1.10: 定義upstream,必須在server以外,weight=權重
#gzip on;
upstream websrvs {
ip_hash;將同一個客戶端請求始終定義到同一臺服務器(此時權重就沒多大意義了)
server 192.168.1.11 weight=1 max_fails=2 fail_timeout=2; max_fails最多檢查幾次
server 192.168.1.12 weight=1 max-fails=2 fail_timeout=2; fail_timeout幾秒後檢查超時,直接切換
#server 127.0.0.1:8080 backup;錯誤頁面(在ip_hash模式下不允許使用此項)
}
server {
二.反向代理不再是某一個主機,而是定義的upstream組
location / {
proxy_pass http://websrvs/;
proxy_set_header X-Real-IP $remote_addr;
}
############################################
在server以外重新定義一個server用作錯誤頁面。
upstream
#server 127.0.0.1:8080 backup;錯誤頁面(在ip_hash模式下不允許使用此項)
server {
listen 8080;
server_name localhost;
root /web/errorpages;
index index.html;
}
[root@localhost ~]# mkdir /web/errorpages
[root@localhost ~]# vim /web/errorpages/index.html
-------------------------------------------
nginx: 三種負載均衡算法
round-robin 加權循環調度算法(默認)
ip_hash 同一個客戶端請求始終定義到一臺服務器
least_conn 挑選當前連接數最少的服務器來響應
-------------------------------------------------------
[root@localhost ~]# netstat -ant |awk '/:80\>/{S[$NF]++}END{for(A in S){print A,S[A]}}'
LISTEN 1
------------------------------------------------------------------
nginx使用緩存
cache:共享內存(存儲鍵和緩存對象元數據)
磁盤空間:存儲數據
proxy_cache_path:不能定義在server{}上下文中:
緩存目錄的子目錄級別
proxy_cache_path /nginx/cache/first levels=1:2:1 keys_zone=first:20m max_size=1g;
cache_manager: LRU 清除緩存
/nginx/cache/first 緩存目錄
levels:定義有三級緩存,名稱長度:1級緩存目錄1個字符,2級緩存目錄2個字符,3級緩存目錄1個字符,最多只能有3級,每一級目
錄字符最多只能有2個
keys_zone 給共享內存命名,用來存儲鍵的區域,定義內存區域有多大,可以定義多個共享內存。
max_size 最大使用多少空間來存儲緩存對象(/nginx/cache/first)
實驗:
location / {
proxy_pass http://websrvs/;
proxy_set_header X-Real-IP $remote_addr;
proxy_cache first;定義使用緩存
proxy_cache_valid 200 10m; 定義返回值爲200的網頁緩存10分鐘
}
。。。。。
proxy_cache_path /nginx/cache/first levels=1:2 keys_zone=first:20m max_size=1g;
add_header X-Cache "$upstream_cache_status from $server_addr";顯示緩存是否命中
[root@localhost nginx]# mkdir -pv /nginx/cache/first
[root@localhost nginx]# service nginx reload
-------------------------------------------
proxy_cache_valid any 1m 指定某些頁面緩存的時間,any 1m表示多有都緩存1分鐘
404 1h
----------------------------------------------------------------------------------------
另外三種緩存:
open_log_cache 日誌文件的緩存,可以先把日誌保存在內存 之後在同步到磁盤,降低磁盤IO
open_file_cache 將打開的文件直接緩存到內存中。
fastcgi_cache fastcgi的緩存,慎用。
---------------------------------------
fastcgi也可以實現負載均勻
upstream phpsrvs { php服務器的負載均勻
server
server
}
upstream imgsrvs { 圖片
server
serber
}
upstream staticfilesrvs { 靜態文件
server
server
}
location /{
#root /web/htdocs;
#index index.php index.html;
proxy_pass http://staticfilesrvs/;
proxy_set_header X-Real-IP $remote_addr;
}
location ~* \.php$ {
fastcgi_pass http://phpsrvs;
}
location ~* "\.(jpg|jpeg|gif|png)$" {
proxy_pass http://imgsrvs;
}
---------------------------------------
rewirte:URL重寫模塊 ,if語句放在location中使用。
if(condition){
}
測試:
雙目測試:
~和!~ ~表示是否匹配,匹配爲真,!表示取反
"和!"
~*和!~*
if ($request_method="POST") {
}
if ($request_url ~* "/froum"){ 如果請求的url中包含了froum字符串就滿足條件
}
單目測試:
----------------------------------------
refrter: 跳轉之前從哪裏過來的,防止盜鏈。
例如
location /photos/ {
valid_referers none blocked www.mydomain.com mydomain.com;
###none表示從瀏覽器中直接輸入的,blocked定義合法規則,blocked www.mydomain.com mydomain.com###
if ($invalid_referer) { 如果不滿足上面的條件則返回403
retuen 403;
}
}
------------------------------------------
URL重寫
last:本次重寫完成之後,重啓下一輪檢查(location條件);
break:本次重寫完成以後,直接執行後續操作;
location /p_w_picpaths/ {
rewrite http://172.16.100.19/p_w_picpaths/
}
支持正則表達式
location / {
root html;
index index.html;
rewrite ^/bbs/(.*) http://172.16.100.19/forum/$1 last; 如果url裏包含/bbs開頭,則替換成172.16.....,$1表示引用前面
的()
#################################################################################################
#rewrite "^/bbs/(.*)/p_w_picpaths/(.*)\.jpg$" http://mylinux.com/bbs/$2/p_w_picpaths/$1.jpg last; 寫成來回循環了,自動循環10
#次
#http//:www.mylinux.com/bbs/a/p_w_picpaths/b.jpg -->http://www.mylinux.com/bbs/b/p_w_picpaths/a/.jpg-->
##################################################################################################
}
--------------------------------------------------
location / {
root html;
index index.html;
rewrite ^/bbs/(.*) ^/forum/$1; 訪問本機的/bbs時直接跳轉至本機的/forum頁面
}
-----------------------------------
讀寫分離:
在http編輯配置文件:dav和dav-fs 兩個模塊啓動就允許用戶上傳操作
在 <Directiry "/var/www/html">下面加一項
Dav on
如果有虛擬主機則在虛擬主機裏添加
賦予寫權限
setfacl -m u:apach:rwx /var/www/html/
#cur http://172.16.100.7
#curl -T /etc/issue http://172.16.100.7 測試上傳
http://172.16.100.7/issue
location / {
proxy_pass http://172.16.100.6/;
if ($request_method = "PUT") {
proxy_pass http://172.16.100.7;
}
}