Home Backend Development PHP Tutorial Nginx configures virtual hosts based on IP, port, and domain name

Nginx configures virtual hosts based on IP, port, and domain name

Jul 29, 2016 am 08:49 AM
gt html index lt nginx

Nginx (pronounced the same as engine x) is a lightweight web server/reverse proxy server and email (IMAP/POP3) proxy server, and is released under a BSD-like protocol. Its characteristics are that it occupies less memory and has strong concurrency capabilities. In fact, nginx's concurrency capabilities do perform better among web servers of the same type. Like Apache httpd, Nginx also provides IP-based, port-based and domain name-based methods to configure virtual hosts.

1. What is a virtual host? A virtual host uses special software and hardware technology to divide a real physical server host into multiple logical storage units. Each logical unit has no physical entity, but each logical unit can work on the network like a real physical host, with a separate IP address (or shared IP address), an independent domain name, and a complete Internet server (support WWW, FTP, E-mail, etc.) functions.

The key technology of virtual hosting is that even if different server programs opened for multiple users are running on the same hardware and the same operating system, they will not interfere with each other. Each user has his own part of the system resources (IP address, document storage space, memory, CPU, etc.). Each virtual host is completely independent from each other. To the outside world, each virtual host behaves exactly the same as a separate host. Therefore, this virtualized logical host is vividly called a "virtual host".

2. Port-based virtual host

<code>1、准备环境
#当前环境
# more /etc/issue
Red Hat Enterprise Linux Server release 6.3 (Santiago)
Kernel \r on an \m

# uname -rm
2.6.32-279.el6.x86_64 x86_64

# nginx -v
nginx version: nginx/1.8.0

# 创建3个目录用于存放不同形式虚拟主机index.html文件
# mkdir -p /website/baseport
# mkdir -p /website/baseip
# mkdir -p /website/basedomain

# vi /website/baseport/index.html 
<span>&lt;!DOCTYPE html&gt;</span><span>&lt;<span>html</span>&gt;</span><span>&lt;<span>head</span>&gt;</span><span>&lt;<span>title</span>&gt;</span>Base port sample<span>&lt;/<span>title</span>&gt;</span><span>&lt;/<span>head</span>&gt;</span><span>&lt;<span>body</span>&gt;</span><span>&lt;<span>h1</span>&gt;</span>This is an based port website sample(prot:8080).<span>&lt;/<span>h1</span>&gt;</span><span>&lt;/<span>body</span>&gt;</span><span>&lt;/<span>html</span>&gt;</span>2、配置nginx.conf
#第一个虚拟主机
server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }

#第二个虚拟主机        
server {       
        listen       8080;
        server_name  localhost;

        location / {
            root   /website/port;
            index  index.html index.htm;
        }
    }

3、验证   
# nginx -t              #语法检查
# service nginx reload  #服务重载
# curl http://192.168.1.120:8080  #验证基于端口访问
<span>&lt;!DOCTYPE html&gt;</span><span>&lt;<span>html</span>&gt;</span><span>&lt;<span>head</span>&gt;</span><span>&lt;<span>title</span>&gt;</span>Base port sample<span>&lt;/<span>title</span>&gt;</span><span>&lt;/<span>head</span>&gt;</span><span>&lt;<span>body</span>&gt;</span><span>&lt;<span>h1</span>&gt;</span>This is an based port website sample(prot:8080).<span>&lt;/<span>h1</span>&gt;</span><span>&lt;/<span>body</span>&gt;</span><span>&lt;/<span>html</span>&gt;</span></code>
Copy after login
3. IP-based virtual host

<code>1、先添加IP
# ifconfig|grep "inet addr"
          inet addr:192.168.1.120  Bcast:192.168.1.255  Mask:255.255.255.0
          inet addr:127.0.0.1  Mask:255.0.0.0
# ifconfig eth0:0 192.168.1.220 netmask 255.255.255.0 up  #添加IP到eth0:0
# ifconfig|grep "inet addr"
          inet addr:192.168.1.120  Bcast:192.168.1.255  Mask:255.255.255.0
          inet addr:192.168.1.220  Bcast:192.168.1.255  Mask:255.255.255.0
          inet addr:127.0.0.1  Mask:255.0.0.0

2、配置nginx.conf
#第一个虚拟主机
server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;

#第二个虚拟主机                      
 server {
        listen       192.168.1.220:80;
        server_name  localhost;

        location / {
            root   /website/baseip;
            index  index.html index.htm;
        }
    }

3、验证    
# nginx -t                     #语法检查      Author:Leshami                     
# service nginx reload         #服务重载      Blog  :http://blog.csdn.net/leshami
# curl http://192.168.1.220    #验证基于IP访问
<span>&lt;!DOCTYPE html&gt;</span><span>&lt;<span>html</span>&gt;</span><span>&lt;<span>head</span>&gt;</span><span>&lt;<span>title</span>&gt;</span>Base ip sample<span>&lt;/<span>title</span>&gt;</span><span>&lt;/<span>head</span>&gt;</span><span>&lt;<span>body</span>&gt;</span><span>&lt;<span>h1</span>&gt;</span>This is an based ip website sample.<span>&lt;/<span>h1</span>&gt;</span><span>&lt;/<span>body</span>&gt;</span><span>&lt;/<span>html</span>&gt;</span></code>
Copy after login
4. Domain name-based virtual host

<code>1、修改/etc/hosts文件
# echo "
192.168.1.120 bbs.ycdata.net bbs
192.168.1.120 mail.ycdata.net mail
&gt; "&gt;&gt;/etc/hosts

2、配置nginx.conf
#第一个虚拟主机
server {
        listen       80;
        server_name mail.ycdata.net;

        location / {
            root   html;
            index  index.html index.htm;
        }

#第二个虚拟主机        
server {
        listen       80;
        server_name  bbs.ycdata.net;

        location / {
            root   /website/baseport;
            index  index.html index.htm;
        }
    }

3、验证
# curl http://mail.ycdata.net
<span>&lt;!DOCTYPE html&gt;</span><span>&lt;<span>html</span>&gt;</span><span>&lt;<span>head</span>&gt;</span><span>&lt;<span>title</span>&gt;</span>Welcome to nginx!<span>&lt;/<span>title</span>&gt;</span><span>&lt;<span>style</span>&gt;</span><span><span>body</span><span>{
        <span><span>width</span>:<span><span>35</span>em</span></span>;
        <span><span>margin</span>:<span><span>0</span> auto</span></span>;
        <span><span>font-family</span>:<span> Tahoma, Verdana, Arial, sans-serif</span></span>;
    <span>}</span></span></span><span>&lt;/<span>style</span>&gt;</span><span>&lt;/<span>head</span>&gt;</span><span>&lt;<span>body</span>&gt;</span><span>&lt;<span>h1</span>&gt;</span>Welcome to nginx!<span>&lt;/<span>h1</span>&gt;</span><span>&lt;<span>p</span>&gt;</span>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.<span>&lt;/<span>p</span>&gt;</span><span>&lt;<span>p</span>&gt;</span>For online documentation and support please refer to
<span>&lt;<span>a</span><span>href</span>=<span>"http://nginx.org/"</span>&gt;</span>nginx.org<span>&lt;/<span>a</span>&gt;</span>.<span>&lt;<span>br</span>/&gt;</span>
Commercial support is available at
<span>&lt;<span>a</span><span>href</span>=<span>"http://nginx.com/"</span>&gt;</span>nginx.com<span>&lt;/<span>a</span>&gt;</span>.<span>&lt;/<span>p</span>&gt;</span><span>&lt;<span>p</span>&gt;</span><span>&lt;<span>em</span>&gt;</span>Thank you for using nginx.<span>&lt;/<span>em</span>&gt;</span><span>&lt;/<span>p</span>&gt;</span><span>&lt;/<span>body</span>&gt;</span><span>&lt;/<span>html</span>&gt;</span># curl http://bbs.ycdata.net
<span>&lt;!DOCTYPE html&gt;</span><span>&lt;<span>html</span>&gt;</span><span>&lt;<span>head</span>&gt;</span><span>&lt;<span>title</span>&gt;</span>Base port sample<span>&lt;/<span>title</span>&gt;</span><span>&lt;/<span>head</span>&gt;</span><span>&lt;<span>body</span>&gt;</span><span>&lt;<span>h1</span>&gt;</span>This is an based port website sample(prot:8080).<span>&lt;/<span>h1</span>&gt;</span><span>&lt;/<span>body</span>&gt;</span><span>&lt;/<span>html</span>&gt;</span></code>
Copy after login
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i

').text(i)); }; $numbering.fadeIn(1700); }); });

The above has introduced Nginx to configure a virtual host based on IP, port, and domain name, including nginx and virtual host content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

Nested Table in HTML

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Table Border in HTML

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

HTML margin-left

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

HTML Table Layout

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Moving Text in HTML

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

HTML Ordered List

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

How do you parse and process HTML/XML in PHP?

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

HTML onclick Button

See all articles