基于域名、端口和IP搭建nginx虚拟主机
时间:2019-12-17 14:21 来源:linux.it.net.cn 作者:IT
nginx支持的虚拟主机有三种
1、基于域名的虚拟主机
2、基于IP的虚拟主机
3、基于端口的虚拟主机
一、基于域名构建
1、编译安装nginx服务
2、配置DNS域名解析服务
3、配置虚拟主机
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
b、编辑nginx.conf配置文件
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.it.net.cn;
charset utf-8;
access_log logs/www.it.net.cn.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;
}
}
c、重载服务
systemctl restart nginx
netstat -ntap | grep 80
d、访问测试
www.kgc.com
www.it.net.cn
二、基于端口
a、创建另一个端口的测试网页
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# echo "this is kgc 8080 web" > kgc/index.html
b、编辑nginx.conf配置文件,仅修改监听地址
server {
listen 192.168.109.137:80;
server_name www.it.net.cn;
charset utf-8;
access_log logs/www.it.net.cn.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.it.net.cn;
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;
}
}
c、重载nginx服务
systemctl restart nginx
netstat -ntap | grep 80
d、测试网页
www.it.net.cn
www.it.net.cn8080
三、基于IP
1、修改网页配置文件中的区域数据配置文件
vim /var/named/kgc.com.zone
systemctl restart named
2、编辑nginx.conf中的配置,修改ip地址
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.it.net.cn;
charset utf-8;
access_log logs/www.it.net.cn.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;
}
}
c、重载nginx服务
systemctl restart nginx
netstat -ntap | grep 80
d、测试网页
192.168.109.137
192.168.109.134 (责任编辑:IT)
nginx支持的虚拟主机有三种 1、基于域名的虚拟主机 2、基于IP的虚拟主机 3、基于端口的虚拟主机 一、基于域名构建 1、编译安装nginx服务 2、配置DNS域名解析服务 3、配置虚拟主机 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 b、编辑nginx.conf配置文件 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.it.net.cn; charset utf-8; access_log logs/www.it.net.cn.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; } } c、重载服务 systemctl restart nginx netstat -ntap | grep 80 d、访问测试 www.kgc.com www.it.net.cn 二、基于端口 a、创建另一个端口的测试网页 [root@localhost ~]# cd /var/www/html/ [root@localhost html]# echo "this is kgc 8080 web" > kgc/index.html b、编辑nginx.conf配置文件,仅修改监听地址 server { listen 192.168.109.137:80; server_name www.it.net.cn; charset utf-8; access_log logs/www.it.net.cn.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.it.net.cn; 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; } } c、重载nginx服务 systemctl restart nginx netstat -ntap | grep 80 d、测试网页 www.it.net.cn www.it.net.cn8080 三、基于IP 1、修改网页配置文件中的区域数据配置文件 vim /var/named/kgc.com.zone systemctl restart named 2、编辑nginx.conf中的配置,修改ip地址 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.it.net.cn; charset utf-8; access_log logs/www.it.net.cn.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; } } c、重载nginx服务 systemctl restart nginx netstat -ntap | grep 80 d、测试网页 192.168.109.137 192.168.109.134 (责任编辑:IT) |