基于(域名、IP、端口)三种形式 访问Nginx 虚拟主机
基于(域名、IP、端口)三种形式 访问Nginx 虚拟主机
一、基于域名的Nginx虚拟主机
echo "192.168.30.16 www.haha.com www.hehe.com" >> /etc/hosts
例:

mkdir -p /var/www/html/haha mkdir -p /var/www/html/hehe echo"<h1>zhouxingchi</h1>" > /var/www/html/haha/index.html echo "<h1>wumengda</h1>" > /var/www/html/hehe/index.html
例:

vim /usr/local/nginx/conf/nginx.conf
....
http {
.....
server {
listen 80;
server_name WWW.haha.com; #设置域名www.haha.com
charset utf-8;
access_1og logs/www.haha.access.log; #设置日志名
location / {
root /var/www/html/haha; #设置www.haha.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.hehe.com; #设置域名www.hehe.com
charset utf-8;
access_log logs/www.hehe.access.1og;
location / {
root /var/www/html/hehe;
index index.html index.php;
}
error_page 500 502 503 504 /50x. html;
location = 50x.html{
root html;
}
}
}
例:

systemctl restart nginx 浏跑器访问 http://www.haha.com http://www.hehe.com
例:



二、基于IP的Nginx虚拟主机
ifconfig ens33:0 192.168.30.20 netmask 255.255.255.0
例:

vim /usr/local/nginx/conf/nginx.conf
....
http {
.....
server {
listen 192.168.30.16:80; #设置监听地址192.168.30.16
server_name WWW.haha.com; .
charset utf-8;
access_log logs/www.haha.access.log;
location / {
root /var/www/html/haha;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html ;
location = 50x.html {
root html ;
}
}
server
listen 192.168.30.20:80;
server_name www.hehe.com; #设置域名www.hehe.com
charset utf-8;
access_log logs/www.hehe.access.1og;
location / {
root /var/www/html/hehe;
index index.html index.php;
}
error_page 500 502 503 504 /50x. html;
location = 50x.html{
root html;
}
}
}
例:

systemctl restart nginx 浏览器访问 http://192.168.30.16 http://192.168.30.20
例:



三、基于端口的Nginx虚拟主机
vim /usr/local/nginx/conf/nginx.conf
....
http {
.....
server {
listen 192.168.30.16:8081; #设置监听8081端口.
server_name www.haha.com;
charset utf-8;
access_log logs/www.haha.acess.1og;
location / {
root /var/www/html/haha;
index index.html index.php;
}
error_page 500 502 503 504 /50x. html;
location = 50x. html{
root html;
}
}
server
listen 192.168.30.20:8080;
server_name www.hehe.com; #设置域名www.hehe.com
charset utf-8;
access_log logs/www.hehe.access.1og;
location / {
root /var/www/html/hehe;
index index.html index.php;
}
error_page 500 502 503 504 /50x. html;
location = 50x.html{
root html;
}
}
}
例:
systemctl restart nginx 浏览器访问 http://192.168.30.16:8080 http://192.168.30.20:8080
例:




浙公网安备 33010602011771号