2013-12-14 57 views
1

我有多個本地站點,我想配置nginx以使每個站點都有不同的主機。在nginx中更改localhost主機名

在/ var /萬維網我有2個位點:1和Site2

然後在/ etc/nginx的/位點可用/ I 2個創建不同的配置爲每一個服務器。我有文件1和Site2哪些內容是這樣的:

server { 
     listen 80; 

     root /var/www/site1; 
     index index.html index.htm; 

     server_name localhost; 

     location/{ 
       try_files $uri $uri/ /index.html; 
     } 
} 

server { 
      listen 7777; 

      root /var/www/site2; 
      index index.html index.htm; 

      server_name localhost; 

      location/{ 
        try_files $uri $uri/ /index.html; 
      } 
    } 

我訪問他們http://localhost:80爲Site1和http://localhost:7777的站點2。這是完美的。我還可以在/ etc添加主機名/主機這樣的:

127.0.0.1 localhost site1 site2 

,我可以用http://site1:80http://site2:7777訪問它們。但我必須始終訪問端口號。我想通過http://site1http://site2訪問它們。

有沒有解決方案來做到這一點?

+0

我通過將server_name更改爲site1和site2,然後放置相同的端口80找到了解決方案。 –

回答

1

你已經想通了,但讓我解釋一下爲什麼它的工作。

第一現場site1應該有工作就好了,因爲默認http端口是80,而這正是site1在聽,所以http://site1.com會工作得很好。

site2第二個配置文件被監聽端口7777這樣一個正常http://site2.com就沒有工作,實際上它很可能會拿起您的默認網站,並擔任該相反,由於nginx的是不是要匹配server_name與因爲端口不匹配,所以在配置中。

您應該創建端口80和nginx的網站會被它自己做匹配和知道哪個網站服務器,除非它是一個https的網站,那麼你會使用端口443代替,這是默認SSL端口

+0

謝謝,我現在已經更清楚了。 –