


Nginx configures virtual hosts based on IP, port, and domain name
Jul 29, 2016 am 08:49 AMNginx (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".3. IP-based virtual host2. 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><!DOCTYPE html></span><span><<span>html</span>></span><span><<span>head</span>></span><span><<span>title</span>></span>Base port sample<span></<span>title</span>></span><span></<span>head</span>></span><span><<span>body</span>></span><span><<span>h1</span>></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><!DOCTYPE html></span><span><<span>html</span>></span><span><<span>head</span>></span><span><<span>title</span>></span>Base port sample<span></<span>title</span>></span><span></<span>head</span>></span><span><<span>body</span>></span><span><<span>h1</span>></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>Copy after login
<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><!DOCTYPE html></span><span><<span>html</span>></span><span><<span>head</span>></span><span><<span>title</span>></span>Base ip sample<span></<span>title</span>></span><span></<span>head</span>></span><span><<span>body</span>></span><span><<span>h1</span>></span>This is an based ip website sample.<span></<span>h1</span>></span><span></<span>body</span>></span><span></<span>html</span>></span></code>
<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><!DOCTYPE html></span><span><<span>html</span>></span><span><<span>head</span>></span><span><<span>title</span>></span>Welcome to nginx!<span></<span>title</span>></span><span><<span>style</span>></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><<span>body</span>></span><span><<span>h1</span>></span>Welcome to nginx!<span></<span>h1</span>></span><span><<span>p</span>></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><<span>p</span>></span>For online documentation and support please refer to <span><<span>a</span><span>href</span>=<span>"http://nginx.org/"</span>></span>nginx.org<span></<span>a</span>></span>.<span><<span>br</span>/></span> Commercial support is available at <span><<span>a</span><span>href</span>=<span>"http://nginx.com/"</span>></span>nginx.com<span></<span>a</span>></span>.<span></<span>p</span>></span><span><<span>p</span>></span><span><<span>em</span>></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><!DOCTYPE html></span><span><<span>html</span>></span><span><<span>head</span>></span><span><<span>title</span>></span>Base port sample<span></<span>title</span>></span><span></<span>head</span>></span><span><<span>body</span>></span><span><<span>h1</span>></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>
').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.
Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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