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