当访问http://cbs.test.com跳转到http://www.test.com/test/cbs/
方法一: (这种方法浏览器地址会变www.test.com/test/cbs)
server {
listen 80;
server_name www.test.com;
location / {
root /data/test;
index index.html;
}
}
server {
listen 80;
server_name *.test.com;
if ( $http_host ~* "^(.*)\.test\.com$") {
set $domain $1;
rewrite ^(.*) http://www.test.com/test/$domain/ break;
}
}
方法二: (这样配置浏览器的地址就会显示成http://cbs.test.com)
server {
listen 80;
server_name *.test.com;
root /usr/local/www;
location ~ ^/(test|images|styles)/ 这是里可以加多个目录,如果不加目录,会无法访问到cbs.test.com/目录下的文件,如图片目录/images
{
proxy_redirect off;
proxy_set_header Host www.test.com;
proxy_pass http://192.168.1.3:80;
}
location / {
set $domain default;
if ( $http_host ~* "^(.*)\.test\.com$") {
set $domain $1;
}
rewrite ^/(.*) /test/$domain/$1 last;
}
}
access_log off;
}
详细过程
如:bs.myweb.com 访问/data0/htdocs/bs
vi /usr/local/webserver/nginx/conf/nginx.conf
server
{
listen 80;
server_name bs.myweb.com;
index index.html index.htm index.php;
root /data0/htdocs/bs;
location ~ .*\.(php|php5)?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
}
/usr/local/webserver/nginx/sbin/nginx -t
/usr/local/webserver/nginx/sbin/nginx -s reload