Nginx summary (d) the domain name-based virtual host configuration

Speaking in front of how to install configure Nginx, you can go here to see nginx series: https: //

Nginx say today is how to configure name-based virtual hosts.

Note: Because this article is nginx in a series of articles, article there are many other configurations, may have said previous article talked about, then there would be no follow-up in the introduction, did not say if some configuration occurs, you may need to to see the previous article.



Nginx two domain names point to the same server, different users access the domain name to display different web content.

Two domain names are and

nginx server using a virtual machine


Preparing the environment

1. Create virtual machines and virtual networks to ensure smooth local computer.

2. Install nginx on

3. and correspond virtual machine specified by the host file:

Modify window hosts file: (C: \ Windows \ System32 \ drivers \ etc)


html directory creation

1. Create the / usr / local / aaa_html on, this directory is a directory of domain names

2. Create the / usr / local / bbb_html on, this directory is a directory of domain names

3. Copy the contents of / usr / local / nginx / html respectively copied to the upper two directories, to facilitate testing need to modify the contents of each directory index.html make personalized.


Virtual Host Configuration

Modify /usr/local/nginx/conf/nginx.conf file, add two virtual hosts, as follows:

server {


;     # Virtual host name is, requesting the domain name url will thus server configuration parsing     server_name;     # All requests are to

/开始,所有的请求都可以匹配此location location /

{         # Using directive specifies the virtual host root directory that is web storage directory         # For example, visit http:


ip / test.html will find /usr/local/aaa_html/test.html

# For example, visit http:


ip / item / test.html will find /usr/local/aaa_html/item/test.html

root /usr/local/aaa_html; #指定欢迎页面,按从左到右顺序查找 index index.html index.htm; } } #配置虚拟主机 server { listen; server_name; location / { root /usr/local/bbb_html; index index.html index.htm; } }



Separate visits,, to see how effective:



At last

Or more, put nginx configuration finished based virtual hosting. By these three articles, we introduced three ways nginx virtual host configuration, namely ip-based virtual host configuration, configuration, and configuration-based virtual host domain name-based virtual host ports.

One of the most commonly used or configuration based virtual hosting.


Leave a Reply