企业中的应用
1. 实现动静太页面的分离
/upload 集群-192.168.246.8:80 html/www/upload upload服务器集群
/static 集群-192.168.246.7:80 html/www/static static服务器集群
/ 集群-192.168.246.9:80 html/www default服务器集群
1.1 各个web集群的nginx配置文件
web01 web02 web03
vim /etc/nginx/conf.d/www.conf
server{
listen 80;
server_name www.yq.com;
location / {
root /html/www;
index yq.html;
}
}
web01 192.168.246.7
mkdir /html/www/static
echo "static web01 7" > /html/www/static/yq.html
echo "web01 7" > html/www/yq.html
chown -R www.www /html
web 02 192.168.246.8
mkdir /html/www/upload
echo "upload web02 8" > /html/www/static/yq.html
echo "web02 8" > html/www/yq.html
chown -R www.www /html
web03 192.168.246.9
mkdir /html/www/
echo "defaultweb03 9" > /html/www/yq.html
chown -R www.www /html
web 01 02 03
systemctl reload nginx
1.2 负载均衡服务器的nginx配置文件
vim /etc/nginx/conf.d/lb.conf
upstream static {
server 192.168.246.7:80;
}
upstream upload {
server 192.168.246.8:80;
}
upstream default {
server 192.168.246.9:80;
}
server {
listen 80;
server_name www.yq.com;
location / {
proxy_pass http://default;
proxy_set_header Host $host;
proxy_next_upstream error timeout http_404 http_502 http_403;
proxy_set_header X-Forwarded-For $remote_addr;
}
location /upload {
proxy_pass http://upload;
proxy_set_header Host $host;
proxy_next_upstream error timeout http_404 http_502 http_403;
proxy_set_header X-Forwarded-For $remote_addr;
}
location /static {
proxy_pass http://static;
proxy_set_header Host $host;
proxy_next_upstream error timeout http_404 http_502 http_403;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
systemctl reload nginx
1.3 动静分离测试
修改windows的hosts
192.168.246.5 www.yq.com
浏览器输入:
www.yq.com
www.yq.com/upload
www.yq.com/static
负载均衡服务器访问:
curl -H host:www.yq.com 192.168.246.5
curl -H host:www.yq.com 192.168.246.5/upload/
curl -H host:www.yq.com 192.168.246.5/static/
2. 根据不同的访问终端显示不同的页面(手机,浏览器)
if ($http_user_agent ~* iphone){
proxy_pass http://mobile;
}
2.1 环境的准备
iphone www.oldboy.com — iphone_access 192.168.246.7:80 mobile移动端集群
谷歌 www.oldboy.com — google_access 192.168.246.8:80 web端集群
IE 360 www.oldboy.com — default_access 192.168.246.9:80 default端集群
web01:
echo "iphone_access 192.168.246.7" >/html/www/yq.html
web02:
echo "google_access 192.168.246.8" >/html/www/yq.html
web03:
echo "default_access 192.168.246.9" >/html/www/yq.html
2.配置文件的编写
负载均衡服务器192.168.246.5
vim /etc/nginx/conf.d/lb.conf
upstream mobile {
server 192.168.246.7:80;
}
upstream web {
server 192.168.246.8:80;
}
upstream default {
server 192.168.246.9:80;
}
server {
listen 80;
server_name www.yq.com;
location / {
if ($http_user_agent ~* iphone){
proxy_pass http://mobile;
}
if ($http_user_agent ~* Chrome){
proxy_pass http://web;
}
proxy_pass http://default;
proxy_set_header Host $host;
proxy_next_upstream error timeout http_404 http_502 http_403;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
systemctl reload nginx
浏览器访问即可