Nginx判斷手機電腦應用案例
根據不同的瀏覽器, 以及不同的?機, 訪問的效果都將不?樣。
//通過瀏覽器來分別連接不同的瀏覽器訪問不同的效果。
http {
...
upstream firefox {
server 172.31.57.133:80;
}
upstream chrome {
server 172.31.57.133:8080;
}
upstream iphone {
server 172.31.57.134:8080;
}
upstream android {
server 172.31.57.134:8081;
}
upstream default {
server 172.31.57.134:80;
}
...
}
//server根據判斷來訪問不同的??
server {
listen 80;
server_name www.xuliangwei.com;
#safari瀏覽器訪問的效果
location / {
if ($http_user_agent ~* "Safari"){
proxy_pass http://dynamic_pools;
}
#firefox瀏覽器訪問效果
if ($http_user_agent ~* "Firefox"){
proxy_pass http://static_pools;
}
#chrome瀏覽器訪問效果
if ($http_user_agent ~* "Chrome"){
proxy_pass http://chrome;
}
#iphone?機訪問效果
if ($http_user_agent ~* "iphone"){
proxy_pass http://iphone;
}
#android?機訪問效果
if ($http_user_agent ~* "android"){
proxy_pass http://and;
}
#其他瀏覽器訪問默認規則
proxy_pass http://dynamic_pools;
include proxy.conf;
}
}
}
image.png
image.png
根據訪問不同?錄, 代理不同的服務器
//默認動態,靜態直接找設置的static,上傳找upload
upstream static_pools {
server 10.0.0.9:80 weight=1;
}
upstream upload_pools {
server 10.0.0.10:80 weight=1;
}
upstream default_pools {
server 10.0.0.9:8080 weight=1;
}
server {
listen 80;
server_name www.xuliangwei.com;
#url: http://www.xuliangwei.com/
location / {
proxy_pass http://default_pools;
nclude proxy.conf;
}
#url: http://www.xuliangwei.com/static/
location /static/ {
proxy_pass http://static_pools;
include proxy.conf;
}
#url: http://www.xuliangwei.com/upload/
location /upload/ {
proxy_pass http://upload_pools;
include proxy.conf;
}
}
//?案2:以if語句實現。
if ($request_uri ~* "^/static/(.*)$")
{
proxy_pass http://static_pools/$1;
}
if ($request_uri ~* "^/upload/(.*)$")
{
proxy_pass http://upload_pools/$1;
}
location / {
proxy_pass http://default_pools;
include proxy.conf;
}