靜態(tài)分離
yuan@lnmp:/etc/nginx/conf.d$
動(dòng)靜分離也是利用負(fù)載均衡的原理來實(shí)現(xiàn)的,為了便于管理,我們把ip分配的配置
寫在conf.d這個(gè)文件夾里面:
cd conf.d
vim upstream.conf
里面寫上動(dòng)靜分離的分配(以PHP和靜態(tài)文件為例子):
upstream php {
server 192.168.10.10:80 #php給這個(gè)服務(wù)器處理
}
upstream static {
server 192.168.10.11:80 #html給這個(gè)服務(wù)器處理
}
然后在server服務(wù)器里面這樣配置:
server{
listen 80; server_name a.com;
location / {
#匹配所有靜態(tài)資源文件用這個(gè)代理
proxy_pass http://static; #這個(gè)地址一定是上面定義的負(fù)載均衡的名字 proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For
$proxy_add_x_forwarded_for;
}
location ~ \.php$ { #匹配php文件用這個(gè)代理
proxy_pass http://me.cao; #這個(gè)地址一定是上面定義的負(fù)載均衡的名字
proxy_set_header Host $host;
proxy_set_header X-Real-IP
$remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~ \.html$ {
proxy_pass http://brother.cao;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}