Categories
Uncategorized

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: //www.cnblogs.com/zhangweizhong/category/1529997.html

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.

 

Scenarios

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

Two domain names are aaa.test.com and bbb.test.com

nginx server using a virtual machine 192.168.78.132

 

Preparing the environment

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

2. Install nginx on 192.168.78.132.

3. aaa.test.com and bbb.test.com correspond 192.168.78.132 virtual machine specified by the host file:

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

192.168.78.132 aaa.test.com
192.168.78.132 bbb.test.com

 

html directory creation

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

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

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:

#配置虚拟主机aaa.test.com
server {

    #监听的ip和端口,配置本机ip和端口
    listen 192.168.78.132:80

;     # Virtual host name is aaa.test.com, requesting the domain name aaa.test.com url will thus server configuration parsing     server_name aaa.test.com;     # 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; } } #配置虚拟主机bbb.test.com server { listen 192.168.78.132:80; server_name bbb.test.com; location / { root /usr/local/bbb_html; index index.html index.htm; } }

 

test

Separate visits aaa.test.com, bbb.test.com, 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