Nginx虚拟主机
1.基于域名
1.添加域名解析
[root@localhost ~]# vim /etc/hosts
192.168.9.135 www.qwe.com www.accp.com
2.准备虚拟站点网页文档
[root@localhost ~]# mkdir -p /var/www/html/qwe
[root@localhost ~]# mkdir -p /var/www/html/accp
[root@localhost ~]# echo "<h1>www.qwe.com</h1>" > /var/www/html/qwe/index.html
[root@localhost ~]# echo "<h1>www.accp.com</h1>" > /var/www/html/accp/index.html
3.修改Nginx的配置文件
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
#gzip on;
server {
listen 80;
server_name www.qwe.com; #设置域名www.qwe.com
charset utf-8; #指定字符集
access_log logs/www.qwe.access.log; #开启访问日志,指定目录
location / {
root /var/www/html/qwe;
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.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;
}
}
4.重启服务,进行访问
[root@localhost ~]# systemctl restart nginx.service
2.基于IP
1.添加域名
[root@localhost ~]# vim /etc/hosts
192.168.9.135 www.qaz.com
192.168.9.140 www.accp.com
2.设置虚拟网卡
[root@localhost ~]# ifconfig ens33:0 192.168.9.140/24
3.修改Nginx的配置文件
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
#gzip on;
#}
server {
listen 192.168.9.135:80;
server_name www.qwe.com;
charset utf-8;
access_log logs/www.qwe.access.log;
location / {
root /var/www/html/qwe;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 192.168.9.140:80;
server_name www.accp.com;
charset utf-8;
access_log logs/www.accp.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;
}
}
4.重启服务,进行访问
[root@localhost ~]# systemctl restart nginx.service
3.基于端口
1.创建8080端口的网页文件
[root@localhost ~]# mkdir -p /var/www/html/accp8080
[root@localhost ~]# echo "<h1>www.accp8080.com</h1>" > /var/www/html/accp8080/index.html
2.修改Nginx的配置文件
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
#gzip on;
#}
server {
listen 192.168.9.135:80;
server_name www.qwe.com;
charset utf-8;
access_log logs/www.qwe.access.log;
location / {
root /var/www/html/qwe;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 192.168.9.140:8080;
server_name www.accp.com;
charset utf-8;
access_log logs/www.accp.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;
}
}
3.重启服务,进行访问
[root@localhost ~]# systemctl restart nginx.service
|