Nginx配置虛擬主機(jī)具體的代碼配置
Vps 上安裝了 Nginx。但是我們還需要Nginx配置虛擬主機(jī),那么要如何才能進(jìn)行Nginx配置虛擬主機(jī)呢?接下來(lái)我們就詳細(xì)看看有關(guān)虛擬主機(jī)在Nginx上的配置問(wèn)題。用多個(gè)子域名,每個(gè)子域名到不同的目錄。
如:
- 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 目錄。后來(lái)看了官方示例:http://wiki.Nginx.org/NginxVirtualHostExample,提到有個(gè) 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;
- }
- }
- }
加上這個(gè) default 就可使 a.chenlb.com 和 b.chenlb.com 正常工作了。以上就是對(duì)Nginx配置虛擬主機(jī)的詳細(xì)介紹。
【編輯推薦】