Nginx网络服务——虚拟主机设置
阿里云国内75折 回扣 微信号:monov8 |
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6 |
相比较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.重启服务浏览器访问测试
二、基于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.重启服务浏览器访问测试