Nginx虚拟主机配置实例
Nginx虚拟主机
结合上篇文章:手工编译NginxNginx虚拟主机的搭建过程,虚拟主机的概念在之前的Apache虚拟主机搭建实验时已讲述过有关知识点,原文链接:Apache web 虚拟主机
结合上篇文章的配置进行下面的配置操作(Nginx服务是开启状态)
[ named]# netstat -natp | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 79214/nginx: master
Nginx虚拟主机配置
1.域名解析配置(环境准备)
[ ~]# 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! [ ~]# vim /etc/named.conf [ ~]# 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; }; [ ~]# vim /etc/named.rfc1912.zones [ ~]# vim /etc/named.rfc1912.zones [ ~]# 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; }; [ ~]# cd /var/named/ [ named]# ls data dynamic named.ca named.empty named.localhost named.loopback slaves [ named]# cp -p named.localhost ll.com.zone [ named]# vim ll.com.zone [ named]# cp -p ll.com.zone cc.com.zone [ 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.1 www IN A 192.168.68.145 [ 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.1 www IN A 192.168.68.145 [ named]# systemctl start named [ named]# systemctl stop firewalld.service [ named]# setenforce usage: setenforce [ Enforcing | Permissive | 1 | 0 ] [ named]# setenforce 0
2.在win10虚拟机上使用nslookup命令测试是否正常解析
3.创建站点
[ ~]# mkdir -p /var/www/html/ll [ ~]# mkdir -p /var/www/html/cc [ ~]# cd /var/www/html/ [ html]# ls cc ll [ html]# echo "this is ll test web" > ll/index.html [ html]# echo "this is cc test web" > cc/index.html [ html]# ls ll/ index.html [ html]# ls cc/ index.html
4.基于不同域名的服务解析设置
[ html]# cd /usr/local/nginx/conf/ [ conf]# vim /usr/local/nginx/conf/nginx.conf [ 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; } } [ conf]# nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [ conf]# service nginx restart
5.不同域名的测试
Nginx基于不同端口访问
继续根据上面的第四步的配置
[ 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 [ conf]# nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [ conf]# service nginx restart
检测:
Nginx基于不同IP地址访问
添加一块网卡选择nat模式
我的是192.168.68.150
1.修改区域数据配置文件
[ conf]# vim /var/named/cc.com.zone [ 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.1 www IN A 192.168.68.150 [ conf]# systemctl restart named
查看解析是否成功:
2.更改配置文件
[ conf]# vim nginx.conf [ 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; } } [ conf]# nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [ conf]# service nginx restart
检查测试:
总结
本文主要是通过Nginx手工编译安装的基础上对Nginx的虚拟主机的相关配置,分别对应的是基于不同域名、不同端口和不同ip进行的相关配置。重要的是对Nginx的配置文件nginx.conf的配置。这里的域名解析的相关配置需要比较娴熟。
下一篇我们将介绍LNMP架构的搭建过程
相关推荐
zrtlin 2020-11-09
nginxs 2020-11-14
Guanjs0 2020-11-13
小木兮子 2020-11-11
yserver 2020-11-11
ssihc0 2020-11-11
windle 2020-11-10
HanksWang 2020-11-10
liuchen0 2020-11-10
Freshairx 2020-11-10
ccschan 2020-11-10
liwf 2020-11-10
Guanjs0 2020-11-09
AderStep 2020-11-09
mqfcu 2020-11-10
windle 2020-10-29