Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
nginx.conf to switch proxy for mobile
http {
upstream app-pc {
server 127.0.0.1:8001;
}
upstream app-mobile {
server 127.0.0.1:8002;
}
server {
listen 8080;
server_name localhost;
# check user agent
if ($http_user_agent ~* '(iPhone|iPod|Opera Mini|Android.*Mobile|NetFront|PSP|BlackBerry|Windows Phone)') {
set $ua_type "@mobile";
}
location / {
# root
if ($ua_type = "@mobile"){
set $page_cache_path /var/www/app_pc/public;
}
if ($ua_type != "@mobile"){
set $page_cache_path /var/www/app_mobile/public;
}
root $page_cache_path;
# for page cache
if (-f $request_filename) {
break;
}
if (-f $request_filename/index.html) {
rewrite (.*) $1/index.html break;
}
if (-f $request_filename.html) {
rewrite (.*) $1.html break;
}
# proxy
if ($ua_type = "@mobile"){
proxy_pass http://app-mobile;
}
if ($ua_type != "@mobile"){
proxy_pass http://app-pc;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment