Nginx虚拟主机类型概述
Nginx服务器提供了三种构建虚拟主机的策略供管理员选择。
第一种方案是利用域名进行区分,即基于域名的虚拟主机。
第二种方案是通过IP地址进行隔离,即基于IP的虚拟主机。
第三种方案则是利用不同的网络端口来区分服务,即基于端口的虚拟主机。
基于域名的虚拟主机配置
首先介绍如何构建基于域名的虚拟主机环境。
第一步是完成Nginx服务的编译与安装工作。
第二步需要配置DNS域名解析服务,确保域名能够正确解析到服务器IP。
第三步是对Nginx虚拟主机进行具体配置。
a、创建用于测试的网页文件。
[root@localhost named]# cd
[root@localhost ~]# mkdir -p /var/www/html/kgc
[root@localhost ~]# mkdir -p /var/www/html/accp
[root@localhost ~]# ls /var/www/html/accp kgc
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# echo "this kgc web" > kgc/index.html
[root@localhost html]# echo "this accp web" > accp/index.html
在/var/www/html目录下分别创建kgc和accp两个目录,并在各自的index.html文件中写入对应的测试内容,例如"this kgc web"和"this accp web"。
vim /usr/local/nginx/conf/nginx.conf
include conf.d/*.conf;
server {
listen 80;
server_name www.kgc.com;
charset utf-8;
access_log logs/www.kgc.com.access.log ;
location / {
root /var/www/html/kgc;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name www.accp.com;
charset utf-8;
access_log logs/www.accp.com.access.log ;
location / {
root /var/www/html/accp;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
b、编辑/usr/local/nginx/conf/nginx.conf配置文件。
systemctl restart nginx
netstat -ntap | grep 80
在配置文件中添加两个server块,分别监听80端口,并将server_name设置为www.kgc.com和www.accp.com,同时指定各自的root目录为/var/www/html/kgc和/var/www/html/accp。
c、重载Nginx服务以应用新配置。
基于端口的虚拟主机配置
执行systemctl restart nginx命令重启服务,并使用netstat -ntap | grep 80检查端口监听状态。
d、进行访问测试。
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# echo "this is kgc 8080 web" > kgc/index.html
通过浏览器或curl工具访问www.kgc.com和www.accp.com,验证是否分别返回对应的网页内容。
server {
listen 192.168.109.137:80;
server_name www.accp.com;
charset utf-8;
access_log logs/www.accp.com.access.log ;
location / {
root /var/www/html/accp;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 192.168.109.137:8080;
server_name www.accp.com;
charset utf-8;
access_log logs/www.accp8080.com.access.log ;
location / {
root /var/www/html/accp8080;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
接下来演示基于端口的虚拟主机配置方法。
systemctl restart nginx
netstat -ntap | grep 80
a、创建另一个端口的测试网页。
在/var/www/html/accp目录下更新index.html内容,或者创建新的目录如accp8080并写入测试内容"this is kgc 8080 web"。
基于IP的虚拟主机配置
b、编辑nginx.conf配置文件,仅修改监听地址部分。
保留一个server块监听192.168.109.137:80对应www.accp.com,新增一个server块监听192.168.109.137:8080,同样指向www.accp.com但根目录可能不同或内容不同。
vim /var/named/kgc.com.zone
systemctl restart named
c、重载Nginx服务。
server {
listen 192.168.109.137:80;
server_name www.kgc.com;
charset utf-8;
access_log logs/www.kgc.com.access.log ;
location / {
root /var/www/html/kgc;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 192.168.109.134:80;
server_name www.accp.com;
charset utf-8;
access_log logs/www.accp.com.access.log ;
location / {
root /var/www/html/accp;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
再次执行systemctl restart nginx并检查端口监听情况。
systemctl restart nginx
netstat -ntap | grep 80
d、测试网页访问。
192.168.109.137
192.168.109.134
分别访问www.accp.com(默认80端口)和www.accp.com:8080,观察返回结果是否符合预期。
以上所述是小编给大家介绍的基于域名、端口和IP搭建nginx虚拟主机,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对本站网站的支持! 如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!