基于(域名、IP、端口)三种形式 访问Nginx 虚拟主机

 

 

 

 

                    一、基于域名的Nginx虚拟主机

                      1.为虚拟主机提供域名解析

                      2.为虚拟主机准备网页文档

                      3.修改Nginx的配置文件

                      4.重启服务,访问测试

                    二、基于IP的Nginx虚拟主机

                      1.添加一张网卡

                      2.添加虚拟主机配置

                      3.验证配置并重启服务

                    三、基于端口的Nginx虚拟主机

                      1.添加虚拟主机配置

                      2.验证配置并重启服务

 

 

 

 

 

一、基于域名的Nginx虚拟主机

1.为虚拟主机提供域名解析

echo "192.168.30.16 www.haha.com www.hehe.com" >> /etc/hosts

例:

基于(域名、IP、端口)三种形式 访问Nginx 虚拟主机_html

2.为虚拟主机准备网页文档

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

例:

基于(域名、IP、端口)三种形式 访问Nginx 虚拟主机_php_02

3.修改Nginx的配置文件

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;
   }
 }
}

例:

基于(域名、IP、端口)三种形式 访问Nginx 虚拟主机_重启_03

4.重启服务,访问测试

systemctl restart nginx

浏跑器访问
http://www.haha.com
http://www.hehe.com

例:

基于(域名、IP、端口)三种形式 访问Nginx 虚拟主机_html_04

 

基于(域名、IP、端口)三种形式 访问Nginx 虚拟主机_虚拟主机_05

 

基于(域名、IP、端口)三种形式 访问Nginx 虚拟主机_php_06

二、基于IP的Nginx虚拟主机

1.添加一张网卡

ifconfig ens33:0 192.168.30.20 netmask 255.255.255.0

例:

基于(域名、IP、端口)三种形式 访问Nginx 虚拟主机_重启_07

2.添加虚拟主机配置

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;
   }
 }
}

例:

基于(域名、IP、端口)三种形式 访问Nginx 虚拟主机_虚拟主机_08

3.验证配置并重启服务

systemctl restart nginx

浏览器访问
http://192.168.30.16
http://192.168.30.20

例:

基于(域名、IP、端口)三种形式 访问Nginx 虚拟主机_html_04

 

基于(域名、IP、端口)三种形式 访问Nginx 虚拟主机_重启_10

 

基于(域名、IP、端口)三种形式 访问Nginx 虚拟主机_html_11

三、基于端口的Nginx虚拟主机

1.添加虚拟主机配置

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;
   }
 }
}

例:
基于(域名、IP、端口)三种形式 访问Nginx 虚拟主机_虚拟主机_12

2.验证配置并重启服务

systemctl restart nginx

浏览器访问
http://192.168.30.16:8080
http://192.168.30.20:8080

例:

基于(域名、IP、端口)三种形式 访问Nginx 虚拟主机_html_04

 

基于(域名、IP、端口)三种形式 访问Nginx 虚拟主机_php_14

 

基于(域名、IP、端口)三种形式 访问Nginx 虚拟主机_php_15