Nginx Virtual Host Configuration

Source: Internet
Author: User
Tags nginx server

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.