Nginx配置虚拟主机支持3种方式:基于IP的虚拟主机配置,基于端口的虚拟主机配置,基于域名的虚拟主机配置。
详解Nginx 虚拟主机配置的三种方式(基于IP) https://www.Vevb.com/article/149774.htm
详解Nginx 虚拟主机配置的三种方式(基于端口) https://www.Vevb.com/article/14978.htm
3、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=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 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.com
新闻热点
疑难解答