需求(以sina.com为例):
http://abc.sina.com 到 http://www.sina.com/blog/abc
http://abc.sina.com/pics 到 http://www.sina.com/blog/abc/pics
网址要隐式的,地址栏显示 http://abc.sina.com/pics
server{
listen 80; ## listen for ipv4; this line is default and implied
server_name sina.com www.sina.com *.sina.com;
set $subdomain '';
if ( $host ~* (\b(?!www\b)\w+)\.sina\.com ) {
set $subdomain $1;
set $suburl $request_uri;
}
location / {
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://sina.com;
if ( $suburl ~ "/$" ) {
rewrite ^/(.*) /blog/$subdomain break;
}
root /var/www/sina.com/;
uwsgi_pass 127.0.0.1:8082;
include uwsgi_params;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$
{
root /var/www/sina.com/;
access_log off;
expires 30d;
break;
}
}