Nginx虚拟主机配置的方式有哪些
发表于:2026-11-02 作者:千家信息网编辑
千家信息网最后更新 2026年11月02日,这篇文章主要为大家展示了"Nginx虚拟主机配置的方式有哪些",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"Nginx虚拟主机配置的方式有哪些"这篇文章吧。
千家信息网最后更新 2026年11月02日Nginx虚拟主机配置的方式有哪些
这篇文章主要为大家展示了"Nginx虚拟主机配置的方式有哪些",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"Nginx虚拟主机配置的方式有哪些"这篇文章吧。
Nginx配置虚拟主机支持3种方式:基于IP的虚拟主机配置,基于端口的虚拟主机配置,基于域名的虚拟主机配置。
Nginx基于域名的虚拟主机配置
使用基于域名的虚拟主机配置是比较流行的方式,可以在同一个IP上配置多个域名并且都通过80端口访问。
3.1 假设服务器有个IP地址为192.168.2.155
[root@localhost ~]# ifconfig ens33:5 192.168.2.155/24 up[root@localhost ~]# ifconfigens33:5: flags=4163mtu 1500 inet 192.168.2.155 netmask 255.255.255.0 broadcast 192.168.2.255 ether 00:0c:29:16:90:ae txqueuelen 1000 (Ethernet)
3.2 192.168.2.155对应的域名如下,配置主机的host文件便于测试
[root@localhost ~]# vim /etc/hosts[root@localhost ~]# cat /etc/hosts|grep 192.168.2.155192.168.2.155 www.oa.com192.168.2.155 www.bbs.com192.168.2.155 www.test.com
3.3 建立虚拟主机存放网页的根目录,并创建首页文件index.html
[root@localhost ~]# cd /data/www/[root@localhost www]# mkdir www.oa.com[root@localhost www]# mkdir www.bbs.com[root@localhost www]# mkdir www.test.com[root@localhost www]# echo www.oa.com > www.oa.com/index.html[root@localhost www]# echo www.bbs.com > www.bbs.com/index.html[root@localhost www]# echo www.test.com > www.test.com/index.html
3.4 修改nginx.conf,将虚拟主机配置文件包含进主文件
[root@localhost /]# cd /usr/local/nginx/conf/[root@localhost conf]# lsfastcgi.conf fastcgi_params koi-utf mime.types nginx.conf scgi_params uwsgi_params win-utffastcgi.conf.default fastcgi_params.default koi-win mime.types.default nginx.conf.default scgi_params.default uwsgi_params.default[root@localhost conf]# vim nginx.conf
在nginx.conf文件末尾加入以下配置
# 在http段中找到以下内容并删除每行前面的"#" log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';# 配置文件结尾的最后一个"}"之前加入以下语句,如下所示include vhost/*.conf
3.5 编辑每个域名的配置文件(每个虚拟主机的配置文件)
[root@localhost conf]# cd vhost/[root@localhost vhost]# cat www.oa.com.conf server { listen 192.168.2.155:80; server_name www.oa.com; access_log /data/logs/www.oa.com.log main; error_log /data/logs/www.oa.com.error.log; location / { root /data/www/www.oa.com; index index.html index.htm; } }[root@localhost vhost]# cat www.bbs.com.conf server { listen 192.168.2.155:80; server_name www.bbs.com; access_log /data/logs/www.bbs.com.log main; error_log /data/logs/www.bbs.com.error.log; location / { root /data/www/www.bbs.com; index index.html index.htm; } }[root@localhost vhost]# cat www.test.com.conf server { listen 192.168.2.155:80; server_name www.test.com; access_log /data/logs/www.test.com.log main; error_log /data/logs/www.test.com.error.log; location / { root /data/www/www.test.com; index index.html index.htm; } }[root@localhost vhost]# cat /data/www/www.oa.com/index.htmlwww.oa.com[root@localhost vhost]# cat /data/www/www.bbs.com/index.htmlwww.bbs.com[root@localhost vhost]# cat /data/www/www.test.com/index.htmlwww.test.com3.6 创建日志文件,否则无法启动nginx
[root@localhost /]# mkdir -p /data/logs[root@localhost /]# touch /data/logs/www.oa.com.log[root@localhost /]# touch /data/logs/www.oa.com.error.log[root@localhost /]# touch /data/logs/www.bbs.com.log[root@localhost /]# touch /data/logs/www.bbs.com.error.log[root@localhost /]# touch /data/logs/www.test.com.log[root@localhost /]# touch /data/logs/www.test.com.error.log[root@localhost /]# ls /data/logs/www.oa.com.error.log www.bbs.com.error.log www.test.com.error.logwww.oa.com.log www.bbs.com.log www.test.com.log
3.7 先测试配置文件然后再启动nginx
[root@localhost /]# cd /usr/local/nginx/sbin/[root@localhost sbin]# ./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# 启动nginx[root@localhost sbin]# ./nginx
3.8 测试文件
[root@localhost vhost]# curl http://www.oa.comwww.oa.com[root@localhost vhost]# curl http://www.bbs.comwww.bbs.com[root@localhost vhost]# curl http://www.test.comwww.test.com
附:配置过程中的问题
1、最后测试时发生的问题
[root@localhost ~]# curl http://www.oa.comcurl: (7) Failed connect to www.oa.com:80; 拒绝连接
解决方法:
查看Nginx是否在监听相应的端口。
[root@localhost ~]# netstat -lntActive Internet connections (only servers)Proto Recv-Q Send-Q Local Address Foreign Address Statetcp 0 0 0.0.0.0:111 0.0.0.0:* LISTENtcp 0 0 192.168.2.155:80 0.0.0.0:* LISTENtcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTENtcp 0 0 0.0.0.0:22 0.0.0.0:* LISTENtcp 0 0 127.0.0.1:25 0.0.0.0:* LISTENtcp6 0 0 :::111 :::* LISTENtcp6 0 0 :::22 :::* LISTENtcp6 0 0 :::23 :::* LISTENtcp6 0 0 ::1:25 :::* LISTEN
1、配置虚拟主机文件时要加上监听的IP地址,每个虚拟主机配置文件都一样。
listen 192.168.2.155:80;
2、配置完成后要重启服务器
以上是"Nginx虚拟主机配置的方式有哪些"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!
配置
主机
虚拟主机
文件
域名
方式
内容
测试
端口
篇文章
地址
服务器
问题
学习
帮助
服务
监听
多个
方法
日志
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
互联网科技公司简介
伟宏互联网科技
科技互联网技术
港股互联网科技股
高科技与互联网
远信互联网科技
互联网科技媒体
浩特互联网科技
新华互联网科技2019
宁夏众和广鑫互联网科技有限公司
栖莱科技互联网股份有限公司
互联网为啥属于高科技
互联网科技创新的路径
高科技互联网发展前景如何
雨阳互联网卡科技有限公司
互联网硬科技公司
广西科技产业互联网是什么
科技互联网公司两年变化
互联网科技对化妆品行业
知域互联网科技有限公司官网
互联网科技卖菜
互联网与科技金融数据
菲比寻常互联网科技
互联网科技小额贷款
中国最先进互联网科技公司
互联网 科技馆
互联网金融科技展
区块链结合互联网科技
版纳互联网科技选哪家
北京天晋互联网科技有限公司