Linux Nginx implementation of virtual Host (3 methods: domain-based, port-based, IP-based address)

Source: Internet
Author: User
Tags epoll

In 3.17 days have written an article about the APAHCE based on the domain name, port, IP address 3 ways of virtual host implementation. The principle is the same, now record Nginx virtual host These three ways of implementation.

The system version is Rhel5.6,nginx version 1.1.6.

1. Domain-based:

Based on the way the domain name, to first have a DNS server, here for convenience, can be configured in the/etc/hosts file, it as a DNS on the line, you can refer to the blog for 3.17 days about the configuration of DNS or other blog posts also have. The installation of Nginx here is also omitted.

[[email protected] ~]# cat/etc/hosts///Add www.xiaowei_1.com, www.xiaowei_2.com two domain names in the Hosts file

# don't remove the following line, or various programs

# that require network functionality would fail.

127.0.0.1localhost.localdomain localhost

:: 1localhost6.localdomain6 localhost6

10.10.16.29www.xiaowei_1.com

10.10.16.29www.xiaowei_2.com


[[email protected] conf]# CP nginx.conf Nginx.conf_bak//backup Nginx configuration file

[email protected] conf]# cat nginx.conf

User nobody;

Worker_processes 1; Worker process easily do not tune, otherwise there will be problems, the number of worker processes should not be greater than the number of CPU cores, the number of worker processes here, the corresponding Nginx will generate how many worker processes.

Error_log Logs/error.log;

PID Logs/nginx.pid;


Events {

Worker_connections 1024; The maximum number of connections per process, which can be adjusted up a bit, the maximum is 65535,nginx the final number of concurrent connections is the number of each process connection here multiplied by the worker process above

Use Epoll; Using the Epoll mode, Nginx's power is mainly due to the use of Epoll mode, of course, Nginx is the most important because it is used as a reverse proxy, (used as a Web server is also very cow).

}


HTTP {//////Put the virtual host in the HTTP node, you can put all the configuration files inside the HTTP node, you can also separate the virtual host configuration file into a configuration file, the HTTP node reference it, here I chose the latter.

Include Mime.types;

Default_type Application/octet-stream;


Sendfile on;

Keepalive_timeout 65;

Include extra/virtualhost.conf; I have the virtual host configuration file independent into virtualhost.conf, this file in the extra directory, that is, the location of the file is/usr/local/nginx/conf/extra/virtualhost.conf.

}

Description: In the actual production in the master profile of the nginx.conf can also add additional configuration information, such as the addition of gzip compression configuration, log format and other configuration information (optimized configuration), where my above configuration information is sufficient, there is a chance to post and explain the more full nginx configuration file.


Now create the extra directory under the/usr/local/nginx/conf directory and create the virtualhost.conf file in the extra directory.

[Email protected] conf]# mkdir extra

[email protected] extra]# cat virtualhost.conf///Here I have made two virtual hosts, respectively, the www.xiaowei_1.com domain name and the www.xiaowei_2.com domain name that I have just configured in the Hosts file.

server {

Listen 80; Listen to all 80 ports in the host

server_name www.xiaowei_1.com; Define the domain name that the client uses to access the site


Location/{

Root html/xiaowei_1; Define the location where the site www.xiaowei_1.com is located, in the/usr/local/nginx/html/xiaowei_1 directory.

Index index.html index.php; Define the index file, you can add a few more

}

}


server {

Listen 80;

server_name www.xiaowei_2.com;


Location/{

Root html/xiaowei_2;

Index index.html index.php;

}

}


[Email protected] html]# mkdir xiaowei_1 xiaowei_2

[email protected] xiaowei_1]# cat index.html//Configure website www.xiaowei_1.com home page

11111

[email protected] xiaowei_2]# cat index.html

222222

[[email protected] ~]#/usr/local/nginx/sbin/nginx-s reload//Restart Nginx service to make the above configuration effective


Because I do not configure the DNS server here, but simply add two domain name information in the Hosts file, so you can only use the domain name on this computer to access two domain names, as follows

[Email protected] ~]# Curl http://www.xiaowei_1.com

11111

[Email protected] ~]# Curl http://www.xiaowei_2.com

222222

The results show that the domain-based virtual host configuration succeeds, by default the default Web site is the corresponding Web site in the first server node in the virtualhost.conf file, and the following results:

[Email protected] ~]# Curl http://localhost

11111

Okay, here we go. The configuration of virtual host based on domain name is successful!


2. Port-based:

Based on the port is also very simple, here I directly above "based on the domain name" on the basis of the virtualhost.conf configuration file added two section of port-based configuration information, can also be the above domain-based configuration information is all deleted, do a pure port-based configuration file

[email protected] extra]# cat virtualhost.conf

server {

Listen 80;

server_name www.xiaowei_1.com;


Location/{

Root html/xiaowei_1;

Index index.html index.php;

}

}

server {

Listen 80;

server_name www.xiaowei_2.com;


Location/{

Root html/xiaowei_2;

Index index.html index.php;

}

}


server {////From here, add the following two server fields

Listen 8080;


Location/{

Root Html/xiaowei_3; The IP of this machine is 10.10.16.29, the native 8080 port site Directory is xiaowei_3, this directory will be created below.

Index index.html index.php;

}

}

server {

Listen 8081;


Location/{

Root Html/xiaowei_4;

Index index.html index.php;

}

}


[Email protected] html]# mkdir xiaowei_3 xiaowei_4

[email protected] xiaowei_3]# cat index.html

33333

[email protected] xiaowei_4]# cat index.html

444444

[Email protected] ~]#/usr/local/nginx/sbin/nginx-s Reload

You can now enter http://10.10.16.29:8080 or http://10.10.16.29:8081 on the browser of the native or other machine to see different content: (If there is a DNS server in the LAN, Add a domain name on the DNS server www.xiaowei.com corresponding IP address 10.10.16.29, then enter http://www.xiaowei.com:8080 or http://www.xiaowei.com:8081 on the browser will have the same effect)

[Email protected] xiaowei_4]# Curl http://10.10.16.29:8080

33333

[Email protected] xiaowei_4]# Curl http://10.10.16.29:8081

444444

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/44/54/wKioL1PgnluSYs0cAADqrQlQdRk797.jpg "style=" float: none; "title=" 1.png "alt=" Wkiol1pgnlusys0caadqrqlqdrk797.jpg "/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/44/54/wKiom1PgnUKw1SfZAAEVORDJst0867.jpg "style=" float: none; "title=" 2.png "alt=" Wkiom1pgnukw1sfzaaevordjst0867.jpg "/>

The results show that access via port is also successful!

You can also separate port-based configuration information into virtualport.conf, also in the extra directory, and add an include extra/in the nginx.conf configuration file Virtualport.conf, the effect can also be accessed through the port.

Here, the port-based virtual host configuration was successful!


3. IP-based:

The IP-based approach is easier, as long as the domain name is based on the name of the IP address to be replaced.


This article from "Personal Feelings" blog, declined reprint!

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.