search
HomeBackend DevelopmentPHP TutorialNginx configures virtual hosts based on IP, port, and domain name

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><span>html</span>></span><span>head</span>><span>title</span>>Base port sample<span><span>title</span>></span><span><span>head</span>></span><span>body</span>><span>h1</span>>This is an based port website sample(prot:8080).<span><span>h1</span>></span><span><span>body</span>></span><span><span>html</span>></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><span>html</span>></span><span>head</span>><span>title</span>>Base port sample<span><span>title</span>></span><span><span>head</span>></span><span>body</span>><span>h1</span>>This is an based port website sample(prot:8080).<span><span>h1</span>></span><span><span>body</span>></span><span><span>html</span>></span></code>
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><span>html</span>></span><span>head</span>><span>title</span>>Base ip sample<span><span>title</span>></span><span><span>head</span>></span><span>body</span>><span>h1</span>>This is an based ip website sample.<span><span>h1</span>></span><span><span>body</span>></span><span><span>html</span>></span></code>
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
> ">>/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><span>html</span>></span><span>head</span>><span>title</span>>Welcome to nginx!<span><span>title</span>></span><span>style</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><span>style</span>></span><span><span>head</span>></span><span>body</span>><span>h1</span>>Welcome to nginx!<span><span>h1</span>></span><span>p</span>>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.<span><span>p</span>></span><span>p</span>>For online documentation and support please refer to
<span>a</span><span>href</span>=<span>"http://nginx.org/"</span>>nginx.org<span><span>a</span>></span>.<span>br</span>/>
Commercial support is available at
<span>a</span><span>href</span>=<span>"http://nginx.com/"</span>>nginx.com<span><span>a</span>></span>.<span><span>p</span>></span><span>p</span>><span>em</span>>Thank you for using nginx.<span><span>em</span>></span><span><span>p</span>></span><span><span>body</span>></span><span><span>html</span>></span># curl http://bbs.ycdata.net
<span><span>html</span>></span><span>head</span>><span>title</span>>Base port sample<span><span>title</span>></span><span><span>head</span>></span><span>body</span>><span>h1</span>>This is an based port website sample(prot:8080).<span><span>h1</span>></span><span><span>body</span>></span><span><span>html</span>></span></code>
').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
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
华为GT3 Pro和GT4的差异是什么?华为GT3 Pro和GT4的差异是什么?Dec 29, 2023 pm 02:27 PM

许多用户在选择智能手表的时候都会选择的华为的品牌,其中华为GT3pro和GT4都是非常热门的选择,不少用户都很好奇华为GT3pro和GT4有什么区别,下面就就给大家介绍一下二者。华为GT3pro和GT4有什么区别一、外观GT4:46mm和41mm,材质是玻璃表镜+不锈钢机身+高分纤维后壳。GT3pro:46.6mm和42.9mm,材质是蓝宝石玻璃表镜+钛金属机身/陶瓷机身+陶瓷后壳二、健康GT4:采用最新的华为Truseen5.5+算法,结果会更加的精准。GT3pro:多了ECG心电图和血管及安

HTML超文本标记语言--超在那里?(文档分析)HTML超文本标记语言--超在那里?(文档分析)Aug 02, 2022 pm 06:04 PM

本篇文章带大家了解一下HTML(超文本标记语言),介绍一下HTML的本质,HTML文档的结构、HTML文档的基本标签和图像标签、列表、表格标签、媒体元素、表单,希望对大家有所帮助!

修复:截图工具在 Windows 11 中不起作用修复:截图工具在 Windows 11 中不起作用Aug 24, 2023 am 09:48 AM

为什么截图工具在Windows11上不起作用了解问题的根本原因有助于找到正确的解决方案。以下是截图工具可能无法正常工作的主要原因:对焦助手已打开:这可以防止截图工具打开。应用程序损坏:如果截图工具在启动时崩溃,则可能已损坏。过时的图形驱动程序:不兼容的驱动程序可能会干扰截图工具。来自其他应用程序的干扰:其他正在运行的应用程序可能与截图工具冲突。证书已过期:升级过程中的错误可能会导致此issu简单的解决方案这些适合大多数用户,不需要任何特殊的技术知识。1.更新窗口和Microsoft应用商店应用程

如何修复无法连接到iPhone上的App Store错误如何修复无法连接到iPhone上的App Store错误Jul 29, 2023 am 08:22 AM

第1部分:初始故障排除步骤检查苹果的系统状态:在深入研究复杂的解决方案之前,让我们从基础知识开始。问题可能不在于您的设备;苹果的服务器可能会关闭。访问Apple的系统状态页面,查看AppStore是否正常工作。如果有问题,您所能做的就是等待Apple修复它。检查您的互联网连接:确保您拥有稳定的互联网连接,因为“无法连接到AppStore”问题有时可归因于连接不良。尝试在Wi-Fi和移动数据之间切换或重置网络设置(“常规”>“重置”>“重置网络设置”>设置)。更新您的iOS版本:

web前端笔试题库之HTML篇web前端笔试题库之HTML篇Apr 21, 2022 am 11:56 AM

总结了一些web前端面试(笔试)题分享给大家,本篇文章就先给大家分享HTML部分的笔试题(附答案),大家可以自己做做,看看能答对几个!

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code<form name="myform"

HTML5中画布标签是什么HTML5中画布标签是什么May 18, 2022 pm 04:55 PM

HTML5中画布标签是“<canvas>”。canvas标签用于图形的绘制,它只是一个矩形的图形容器,绘制图形必须通过脚本(通常是JavaScript)来完成;开发者可利用多种js方法来在canvas中绘制路径、盒、圆、字符以及添加图像等。

总结HTML中a标签的使用方法及跳转方式总结HTML中a标签的使用方法及跳转方式Aug 05, 2022 am 09:18 AM

本文给大家总结介绍a标签使用方法和跳转方式,希望对大家有所帮助!

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools