How to configure a virtual host in Nginx

Source: Internet
Author: User

Virtual Host: will be a server virtual more than one host, each virtual host can be a stand-alone site, can have a separate domain name, with a complete Intemet server function. The virtual hosts on the same host are completely independent of each other.

Simply put, you have two completely independent web sites that can run on a single server using a virtual host.

With Apache-like, Nginx can also configure a variety of Virtual GUI machine: One is based on the IP virtual host, the second is based on the domain name of the virtual host, the third is based on the virtual host port.

Nginx.conf

HTTP {
         ...
                                      
... #server就是定义主机的
#server {
   #    listen       8000;
   #    Listen       somename:8080;
   #    server_name  somename alias  Another.alias;
   #    Location/{
   #        root   html;
   #        Index  index.html index.htm;
   #    }
#}
}

This section is the example of defining a virtual host

Can be copied directly inside the change can be added

For ease of management, it is common to create a single file (vhost.conf) and then specify the HTTP in the Nginx configuration file

Steps:

First create a file that defines a virtual host

Vim/usr/local/nginx/conf/vhost.conf

      server {                   #添加一个server
        listen      ;                 #监听80端口
        server_name www.baidu.com;      #域名
        location/{                    #指定位置
            root  html/baidu;           #主页所在目录
            index index.html index.htm #索引文件
        }
}
server {
        listen      ;
        server_name www.qq.com;
        Location/{
            root  html/qq;
            Index index.html index.htm;
        }
}

Add two virtual host two domain name, one is www.baidu.com one is www.qq.com

Then you need to specify in the Nginx master configuration file

Vim/usr/local/nginx/conf/nginx.conf

Add a row inside HTTP {}

Include vhost.conf;

Be sure to add to HTTP {} and avoid adding to the outside or other definition areas inside

Restart Service

respectively in/usr/local/nginx/html/baidu and/USR/LOCAL/NGINX/HTML/QQ under the creation of two index.html files, respectively written in Baidu and QQ test

[Root@rh5-4 conf]# ELinks Http://www.qq.com--dump

Qq

[Root@rh5-4 conf]# elinkshttp://www.baidu.com--dump

Baidu

Configuration Complete!

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Servers/web/

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.