Nginx配置虛擬主機具體的代碼配置
作者:佚名 
  Nginx配置虛擬主機需要我們不斷的進行學(xué)習(xí),我們在使用的時候有不少的問題,接下來我們就看看如何才能更好的進行Nginx配置虛擬主機。
 Vps 上安裝了 Nginx。但是我們還需要Nginx配置虛擬主機,那么要如何才能進行Nginx配置虛擬主機呢?接下來我們就詳細看看有關(guān)虛擬主機在Nginx上的配置問題。用多個子域名,每個子域名到不同的目錄。
如:
- http {
 - server {
 - listen 80;
 - server_name a.chenlb.com;
 - access_log logs/a.access.log main;
 - server_name_in_redirect off;
 - location / {
 - index index.html;
 - root /home/www/host_a/;
 - }
 - }
 - server {
 - listen 80;
 - server_name b.chenlb.com;
 - access_log logs/b.access.log main;
 - server_name_in_redirect off;
 - location / {
 - index index.html;
 - root /home/www/host_b/;
 - }
 - }
 - }
 - http {
 - server {
 - listen 80;
 - server_name a.chenlb.com;
 - access_log logs/a.access.log main;
 - server_name_in_redirect off;
 - location / {
 - index index.html;
 - root /home/www/host_a/;
 - }
 - }
 - server {
 - listen 80;
 - server_name b.chenlb.com;
 - access_log logs/b.access.log main;
 - server_name_in_redirect off;
 - location / {
 - index index.html;
 - root /home/www/host_b/;
 - }
 - }
 - }
 
結(jié)果發(fā)現(xiàn)用 b.chenlb.com 還是指到 host_a 目錄。后來看了官方示例:http://wiki.Nginx.org/NginxVirtualHostExample,提到有個 default 的匹配,如:
- http {
 - server {
 - listen 80 default;
 - server_name _;
 - access_log logs/default.access.log main;
 - server_name_in_redirect off;
 - location / {
 - index index.html;
 - root /var/www/default/htdocs;
 - }
 - }
 - }
 - http {
 - server {
 - listen 80 default;
 - server_name _;
 - access_log logs/default.access.log main;
 - server_name_in_redirect off;
 - location / {
 - index index.html;
 - root /var/www/default/htdocs;
 - }
 - }
 - }
 
加上這個 default 就可使 a.chenlb.com 和 b.chenlb.com 正常工作了。以上就是對Nginx配置虛擬主機的詳細介紹。
【編輯推薦】
責任編輯:張浩 
                    來源:
                    互聯(lián)網(wǎng)
 














 
 
 
 
 
 
 