Nginx虚拟主机配置实例
发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,Nginx虚拟主机结合上篇文章:手工编译NginxNginx虚拟主机的搭建过程,虚拟主机的概念在之前的Apache虚拟主机搭建实验时已讲述过有关知识点,原文链接:Apache web 虚拟主机结合上篇
千家信息网最后更新 2026年11月03日Nginx虚拟主机配置实例
Nginx虚拟主机
结合上篇文章:手工编译NginxNginx虚拟主机的搭建过程,虚拟主机的概念在之前的Apache虚拟主机搭建实验时已讲述过有关知识点,原文链接:Apache web 虚拟主机
结合上篇文章的配置进行下面的配置操作(Nginx服务是开启状态)
[root@localhost named]# netstat -natp | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 79214/nginx: master
Nginx虚拟主机配置
1.域名解析配置(环境准备)
[root@localhost ~]# yum install -y bind...//省略部分内容 dhclient.x86_64 12:4.2.5-77.el7.centos dhcp-common.x86_64 12:4.2.5-77.el7.centos dhcp-libs.x86_64 12:4.2.5-77.el7.centos Complete![root@localhost ~]# vim /etc/named.conf [root@localhost ~]# head -21 /etc/named.conf |tail options { listen-on port 53 { any; }; listen-on-v6 port 53 { ::1; }; directory "/var/named"; dump-file "/var/named/data/cache_dump.db"; statistics-file "/var/named/data/named_stats.txt"; memstatistics-file "/var/named/data/named_mem_stats.txt"; recursing-file "/var/named/data/named.recursing"; secroots-file "/var/named/data/named.secroots"; allow-query { any; };[root@localhost ~]# vim /etc/named.rfc1912.zones [root@localhost ~]# vim /etc/named.rfc1912.zones [root@localhost ~]# head -34 /etc/named.rfc1912.zones | tail zone "ll.com" IN { type master; file "ll.com.zone"; allow-update { none; };};zone "cc.com" IN { type master; file "cc.com.zone"; allow-update { none; };[root@localhost ~]# cd /var/named/[root@localhost named]# lsdata dynamic named.ca named.empty named.localhost named.loopback slaves[root@localhost named]# cp -p named.localhost ll.com.zone[root@localhost named]# vim ll.com.zone [root@localhost named]# cp -p ll.com.zone cc.com.zone[root@localhost named]# cat ll.com.zone $TTL 1D@ IN SOA @ rname.invalid. ( 0 ; serial 1D ; refresh 1H ; retry 1W ; expire 3H ) ; minimum NS @ A 127.0.0.1www IN A 192.168.68.144[root@localhost named]# cat cc.com.zone $TTL 1D@ IN SOA @ rname.invalid. ( 0 ; serial 1D ; refresh 1H ; retry 1W ; expire 3H ) ; minimum NS @ A 127.0.0.1www IN A 192.168.68.144[root@localhost named]# systemctl start named[root@localhost named]# systemctl stop firewalld.service [root@localhost named]# setenforce usage: setenforce [ Enforcing | Permissive | 1 | 0 ][root@localhost named]# setenforce 02.在win10虚拟机上使用nslookup命令测试是否正常解析
3.创建站点
[root@localhost ~]# mkdir -p /var/www/html/ll[root@localhost ~]# mkdir -p /var/www/html/cc[root@localhost ~]# cd /var/www/html/[root@localhost html]# lscc ll[root@localhost html]# echo "this is ll test web" > ll/index.html[root@localhost html]# echo "this is cc test web" > cc/index.html[root@localhost html]# ls ll/index.html[root@localhost html]# ls cc/index.html4.基于不同域名的服务解析设置
[root@localhost html]# cd /usr/local/nginx/conf/[root@localhost conf]# vim /usr/local/nginx/conf/nginx.conf[root@localhost conf]# sed -n '35,63p' nginx.conf server { listen 80; server_name www.ll.com; charset utf-8; access_log logs/www.ll.com.access.log; location / { root /var/html/ll; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 80; server_name www.cc.com; charset utf-8; access_log logs/www.cc.com.access.log; location / { root /var/html/cc; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }[root@localhost conf]# nginx -tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful[root@localhost conf]# service nginx restart 5.不同域名的测试
Nginx基于不同端口访问
继续根据上面的第四步的配置
[root@localhost conf]# sed -n '35,63p' nginx.conf server { listen 192.168.68.144:80; server_name www.ll.com; charset utf-8; access_log logs/www.ll.com.access.log; location / { root /var/www/html/ll; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 192.168.68.144:8080; server_name www.cc.com; charset utf-8; access_log logs/www.cc8080.com.access.log; location / { root /var/www/html/cc8080; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } nginx -t[root@localhost conf]# nginx -tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful[root@localhost conf]# service nginx restart 检测:

Nginx基于不同IP地址访问
添加一块网卡选择nat模式
我的是192.168.68.150
1.修改区域数据配置文件
[root@localhost conf]# vim /var/named/cc.com.zone [root@localhost conf]# cat /var/named/cc.com.zone $TTL 1D@ IN SOA @ rname.invalid. ( 0 ; serial 1D ; refresh 1H ; retry 1W ; expire 3H ) ; minimum NS @ A 127.0.0.1www IN A 192.168.68.150[root@localhost conf]# systemctl restart named查看解析是否成功:
2.更改配置文件
[root@localhost conf]# vim nginx.conf[root@localhost conf]# sed -n '35,63p' nginx.conf server { listen 192.168.68.144:80; server_name www.ll.com; charset utf-8; access_log logs/www.ll.com.access.log; location / { root /var/www/html/ll; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 192.168.68.150:80; server_name www.cc.com; charset utf-8; access_log logs/www.cc.com.access.log; location / { root /var/www/html/cc; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }[root@localhost conf]# nginx -tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful[root@localhost conf]# service nginx restart 检查测试:

总结
本文主要是通过Nginx手工编译安装的基础上对Nginx的虚拟主机的相关配置,分别对应的是基于不同域名、不同端口和不同ip进行的相关配置。重要的是对Nginx的配置文件nginx.conf的配置。这里的域名解析的相关配置需要比较娴熟。
下一篇我们将介绍LNMP架构的搭建过程
配置
主机
虚拟主机
不同
域名
文件
测试
上篇
手工
文章
端口
过程
面的
服务
编译
重要
娴熟
成功
内容
区域
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
中关村互联网科技集团
互联网的科技
互联网最新的科技
互联网品科技
深圳科技互联网
互联网科技ppt模板
深圳艾佳互联网科技
新华互联网科技校企合作
中国科技创新与世界互联网大会
浙南科技城互联网大厦建设工程
佛山市翊博互联网络科技有限公司
江苏互联网科技小贷
上海同邻互联网科技有限公司规模
互联网科技平台产品运营
云南互联网科技学费
互联网的科技体现在哪里
安徽九吞吴互联网科技有限公司
互联网企业和网络科技有限公司
伍佰万互联网科技是干嘛的
武汉亿视互联网科技有限公司
互联网金融科技
互联网科技深圳
合肥互联网科技公司
顺玮阁互联网科技
互联网金融和金融科技
工业互联网山东科技大学
互联网时代高科技布局
众联互联网络科技
互联网科技创新创业项目
广东猫王互联网科技有限公司电话