相比较Apache的虚拟主机设置,Nginx的设置是十分简便的只需要修改主配置中的相关配置就能实现虚拟主机的效果
一、基于域名的 Nginx 虚拟主机
1. 为虚拟主机提供域名解析
echo "192.168.190.40 www.gundam.com www.noelle.com" >> /etc/hosts
2.为虚拟主机准备网页文档
mkdir -p /var/www/html/ztm
mkdir -p /var/www/html/hss
echo "<h1>www.gundam.com</h1>" > /var/www/html/gundam/index.html
echo "<h1>www.noelle.com</h1>" > /var/www/html/noelle/index.html
3.修改Nginx的配置文件
vim /usr/local/nginx/conf/nginx.conf
......
http {
......
server {
listen 80;
server_name www.gundam.com; #设置域名www.gundam.com
charset utf-8;
access_log logs/www.gundam.access.log; #设置日志名
location / {
root /var/www/html/gundam; #设置www.gundam.com 的工作目录
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
server {
listen 80;
server_name www.noelle.com; #设置域名www.noelle.com
charset utf-8;
access_log logs/www.noelle.access.log;
location / {
root /var/www/html/noelle;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
}
4.重启服务,浏览器访问测试
文章来源地址https://www.toymoban.com/news/detail-472699.html
二、基于IP 的 Nginx 虚拟主机
1.设置虚拟主机IP
ifconfig ens33:0 192.168.190.42 netmask 255.255.255.0
2.修改主配置文件
vim /usr/local/nginx/conf/nginx.conf
......
http {
......
server {
listen 192.168.190.40:80; #设置监听地址192.168.190.40
server_name www.gundam.com;
charset utf-8;
access_log logs/www.gundam.access.log;
location / {
root /var/www/html/gundam;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
server {
listen 192.168.190.42:80; #设置监听地址192.168.190.42
server_name www.noelle.com;
charset utf-8;
access_log logs/www.noelle.access.log;
location / {
root /var/www/html/noelle;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
}
3.重启服务,浏览器访问测试
重启服务
systemctl restart nginx
浏览器访问
http://192.168.190.40
http://192.168.190.42
三、基于端口的Nginx虚拟机
1.修改主配置文件
vim /usr/local/nginx/conf/nginx.conf
......
http {
......
server {
listen 192.168.190.40:8080; #设置监听 8080 端口
server_name www.ztm.com;
charset utf-8;
access_log logs/www.gundam.access.log;
location / {
root /var/www/html/gundam;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
server {
listen 192.168.190.40:8888; #设置监听 8888 端口
server_name www.noelle.com;
charset utf-8;
access_log logs/www.noelle.access.log;
location / {
root /var/www/html/noelle;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
}
2.重启服务,浏览器访问测试
文章来源:https://www.toymoban.com/news/detail-472699.html
到了这里,关于Nginx网络服务——虚拟主机设置的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!