Nginx-文章總綱
友情鏈接
1寝蹈、基礎(chǔ)篇:ngxin 的簡介翘悉,安裝炼幔,目錄說明夏跷,配置文件詳解(理論為主)
http://www.reibang.com/p/d46dc6bce6ed
2、應(yīng)用篇:靜態(tài)部署削樊,后端配置割捅,反向代碼碧囊,負(fù)載均衡,緩存集成(實(shí)操為主)
http://www.reibang.com/p/3a39e18550b5
3供嚎、集群篇:集群搭建黄娘,高可用解決方案峭状,制作下載站點(diǎn)和用戶認(rèn)證(提升)
http://www.reibang.com/p/2e9ca678ef73
4、模塊篇:ngx-lua的結(jié)合使用逼争,lua基本語法介紹(綜合)
http://www.reibang.com/p/2e9ca678ef73
一优床、Nginx反向代理
1、正向代理案例
(1)編寫配置
http {
log_format main 'client send request=>clientIp=$remote_addr serverIp=>$host';
server{
listen 80;
server_name localhost;
access_log logs/access.log main;
location / {
root html;
index index.html index.htm;
}
}
}
(2)打開日志文件誓焦,logs/access.log
(3)代理服務(wù)器設(shè)置:
server {
listen 82;
resolver 8.8.8.8;
location /{
proxy_pass http://$host$request_uri;
}
}
(4)設(shè)置代理IP
(5)再次訪問日志 ip 就不同了胆敞,正向代理應(yīng)用的很少。
2杂伟、Nginx反向代理的配置語法(介紹)
Nginx反向代理模塊的指令是由ngx_http_proxy_module模塊進(jìn)行解析移层,該模塊在安裝Nginx的時候已經(jīng)自己加裝到
Nginx中了,接下來我們把反向代理中的常用指令一一介紹下:
proxy_pass
proxy_set_header
proxy_redirect
1赫粥、proxy_pass
該指令用來設(shè)置被代理服務(wù)器地址观话,可以是主機(jī)名稱、IP地址加端口號形式越平。
URL:為要設(shè)置的被代理服務(wù)器地址频蛔,包含傳輸協(xié)議( http , https:// )、主機(jī)名稱或IP地址加端口號秦叛、URI等要素晦溪。
server {
listen 80;
server_name localhost;
location /{
#proxy_pass http://192.168.200.146;
proxy_pass http://192.168.200.146/;
}
}
#當(dāng)客戶端訪問 http://localhost/index.html,效果是一樣的
server{
listen 80;
server_name localhost;
location /server{
#proxy_pass http://192.168.200.146;
proxy_pass http://192.168.200.146/;
}
}
#當(dāng)客戶端訪問 http://localhost/server/index.html
#這個時候,第一個proxy_pass就變成了 http://localhost/server/index.html
#第二個proxy_pass就變成了http://localhost/index.html效果 就不一樣了挣跋。
2尼变、proxy_set_header
該指令可以更改Nginx服務(wù)器接收到的客戶端請求的請求頭信息,然后將新的請求頭發(fā)送給代理的服務(wù)器
需要注意的是浆劲,如果想要看到結(jié)果嫌术,必須在被代理的服務(wù)器上來獲取添加的頭信息。
被代理服務(wù)器: [192.168.200.146]
server {
listen 8080;
server_name localhost;
default_type text/plain;
return 200 $http_username;
}
代理服務(wù)器: [192.168.200.133]
server {
listen 8080;
server_name localhost;
location /server {
proxy_pass http://192.168.200.146:8080/;
proxy_set_header username TOM;
}
}
3牌借、proxy_redirect
該指令是用來重置頭信息中的"Location"和"Refresh"的值
服務(wù)端[192.168.200.146]:
server {
listen 8081;
server_name localhost;
if (!-f $request_filename){
return 302 http://192.168.200.146;
}
}
代理服務(wù)端[192.168.200.133]