現在有一個主域名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;}}}新聞熱點
疑難解答