For small traffic sites, only running a Web site on the Nginx server is too wasteful of server resources, if our company has more than one website business, in a single server running multiple sites at the same time, can not help to take full advantage of the performance of the server, but also to reduce the company costs. So how does this feature work?
Let's take a look at the Nginx configuration file:
Vim/usr/local/nginx/conf/nginx.conf
http
{
Server
{
Listen 81; #监听的端口, the IP can be added in front.
server_name localhost; #主机名
Access_log Logs/access.log combined; #日志位置
Location/
{
root HTML; #网页文件存放的目录
Index index.html index.htm; #默认首页文件, priority left-to-right
}
}
}
You just have to look at this part. Each server module (red part) is equivalent to a separate host. We just need to add sever to the HTTP module.
Example:
If we have a, b two sites, respectively, www.a.com and www.b.com, while running in this nginx. You first need to add two directories, such as www.a.com and www.b.com, to your website's home directory:
Mkdir/usr/local/nginx/html/www.a.com
Mkdir/usr/local/nginx/html/www.b.com
Add two index.html files to each of the two directories as follows:
This is A!
This is B!
Then modify the HTTP portion of the Nginx configuration file as:
HTTP {
Include Mime.types;
Default_type Application/octet-stream;
Sendfile on;
Keepalive_timeout 65;
server {
Listen 81;
server_name localhost;
Location/{
root HTML;
Index index.html index.htm;
}
}
server {
Listen 81;
server_name www.a.com;
Access_log Logs/www.a.com.access.log combined;
Location/{
Root html/www.a.com;
Index index.html index.htm;
}
}
server {
Listen 81;
server_name www.b.com;
Access_log Logs/www.b.com.access.log combined;
Location/{
Root html/www.b.com;
Index index.html index.htm;
}
Error_page 502 503 504/50x.html;
Location =/50x.html {
root HTML;
}
}
}
Then, after restarting the Nginx service, visit the website:
http://115.159.184.248:81/a/--------This is a!
http://115.159.184.248:81/b/--------This is b!
In this way, a simple virtual host is configured.
Nginx Virtual Host Configuration