现在有一个主域名www.example.com,通过nginx代理访问到192.168.1.10的8080端口,要实现aaa.example.com访问192.168.1.11这个机器的8080端口,bbb.example.com访问192.168.1.11这个机器的8081端口
第一步,要确保*.example.com的域名已经指定到对应的ip上
第二步,在nginx的配置文件中server部分配置泛域名:
server { listen 80; server_name *.example.com;}第三步,通过正则表达式,匹配二级域名部分,并把匹配到的内容存储到变量中 if ( $http_host ~* "^(.*?)/.example/.com$" ) { set $domain $1; }第四步,location 中进行设置location / { root /home/example; index index.html index.htm; PRoxy_pass http://example_www if ( $domain ~* "aaa" ) { proxy_pass http://example_aaa; } if ( $domain ~* "bbb" ) { proxy_pass http://example_bbb; }}第五步,设置upstream,这里可以进行负载均衡,具体不详细说 upstream example_www{ server 192.168.1.10:8080 weight=1;#server 192.168.1.10:8081 weight=2; } upstream example_aaa{ server 192.168.1.11:8080 weight=2; } upstream example_bbb{ server 192.168.1.11:8081 weight=3; }至此,二级网站就配置完成了,最后再整理一下整个配置文件…… upstream example_www{ server 192.168.1.10:8080 weight=1;#server 192.168.1.10:8081 weight=2; } upstream example_aaa{ server 192.168.1.11:8080 weight=2; } upstream example_bbb{ server 192.168.1.11:8081 weight=3; }server { listen 80; server_name *.example.com; if ( $http_host ~* "^(.*?)/.example/.com$" ) { set $domain $1; }location / {root /home/example;index index.html index.htm; proxy_pass http://example_wwwif ( $domain ~* "aaa" ) { proxy_pass http://example_aaa;}if ( $domain ~* "bbb" ) {proxy_pass http://example_bbb;}}}新闻热点
疑难解答