一、基于域名的 Nginx 虚拟主机
1.为虚拟主机提供域名解析
echo "192.168.126.13 www.xjj.com www.accp.com" >> /etc/hosts
2.为虚拟主机准备网页文档
mkdir -p /var/www/html/xjj
mkdir -p /var/www/html/accp
echo "<h1>www.xjj.com</h1>" > /var/www/html/xjj/index.html
echo "<h1>www.accp.com</h1>" > /var/www/html/accp/index.html
3.修改Nginx的配置文件
vim /usr/local/nginx/conf/nginx.conf
......
http {
......
server {
listen 80;
server_name www.xjj.com;
charset utf-8;
access_log logs/www.xjj.access.log;
location / {
root /var/www/html/xjj;
index index.html index.php;
}
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.php;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
}
4.重启服务,访问测试
systemctl restart nginx
浏览器访问:
http://www.xjj.com
http://www.accp.com
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ewlkj4T7-1633612858832)(C:\Users\姜姜爱吃香菜\AppData\Roaming\Typora\typora-user-images\image-20210930200704497.png)]
二、基于IP的虚拟主机
ifconfig ens33:0 192.168.126.31 netmask 255.255.255.0
vim /usr/local/nginx/conf/nginx.conf
......
http {
......
server {
listen 192.168.126.14:80;
server_name www.xjj.com;
charset utf-8;
access_log logs/www.xjj.access.log;
location / {
root /var/www/html/xjj;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
server {
listen 192.168.126.18: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;
}
}
}
nginx -t
systemctl restart nginx.service
浏览器访问 http://192.168.126.14 http://192.168.126.18
三、基于端口的虚拟主机
vim /usr/local/nginx/conf/nginx.conf
......
http {
......
server {
listen 192.168.126.14:8080;
server_name www.xjj.com;
charset utf-8;
access_log logs/www.xjj.access.log;
location / {
root /var/www/html/xjj;
index index.html index.html;
}
error_page 500 502. 503 504 /50x.html;
location = 50x.html{
root html;
}
}
server {
listen 192.168.126.14:8888;
server_name www.xcf.com;
charset utf-8;
access_log logs/www.xcf.access.log;
location / {
root /var/www/html/benet;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
}
nginx -t
systemctl restart nginx.service
浏览器访问
http://192.168.126.14:8080
http://192.128.126.14:8888
systemctl restart nginx
浏览器访问 http://192.168.126.14:8080 http://192.168.126.14:8888
总结
|