美文网首页nginx开发技巧
Nginx三种虚拟主机配置方法

Nginx三种虚拟主机配置方法

作者: 果然朝辉 | 来源:发表于2019-02-25 23:30 被阅读28次

    虚拟主机技术能够让同一台服务器、同一组Nginx进程上运行多个网站,降低了资金和服务器资源的损耗。Nginx可以配置三种类型的虚拟主机,本文就是主要介绍这三种虚拟主机配置方式。


    image.png
    • 配置基于IP的虚拟主机
      基于IP的虚拟主机就是直接把server_name设置为不同IP即可完成配置
    http
    {
      server 
      {
         listen 80;
         server_name 192.168.6.53;
         access_log logs/server1.access.log commbined;
         
         location / 
         {
             index index.html;
             root/data1/wwwroot/server1
         }
      }
      server 
      {
         listen 80;
         server_name 192.168.6.54;
         access_log logs/server2.access.log commbined;
         
         location / 
         {
             index index.html;
             root/data1/wwwroot/server2
         }
      }
      server 
      {
         listen 80;
         server_name 192.168.6.55;
         access_log logs/server3.access.log commbined;
         
         location / 
         {
             index index.html;
             root/data1/wwwroot/server3
         }
      }
    }
    
    • 基于域名的虚拟主机
      基于域名的虚拟主机就是直接把server_name设置为不同域名即可完成配置
    http
    {
      server 
      {
         listen 80;
         server_name a.domain.com;
         access_log logs/a.domain.com.access.log commbined;
         
         location / 
         {
             index index.html;
             root/data1/wwwroot/server1
         }
      }
      server 
      {
         listen 80;
         server_name b.domain.com;
         access_log logs/b.domain.com.access.log commbined;
         
         location / 
         {
             index index.html;
             root/data1/wwwroot/server2
         }
      }
      server 
      {
         listen 80;
         server_name www.domain domain.com *.domain.com;
         access_log logs/server3.access.log commbined;
         
         location / 
         {
             index index.html;
             root/data1/wwwroot/server3
         }
      }
    }
    
    • 基于端口的虚拟主机
      基于端口的虚拟主机就是直接把listen设置成不同的端口即可完成配置
    http
    {
      server 
      {
         listen 80;
         server_name 192.168.6.53
         access_log logs/server1.access.log commbined;
         
         location / 
         {
             index index.html;
             root/data1/wwwroot/server1
         }
      }
      server 
      {
         listen 8080;
         server_name 192.168.6.53
         access_log logs/server2.access.log commbined;
         
         location / 
         {
             index index.html;
             root/data1/wwwroot/server2
         }
      }
      server 
      {
         listen 10080;
         server_name 192.168.6.53
         access_log logs/server3.access.log commbined;
         
         location / 
         {
             index index.html;
             root/data1/wwwroot/server3
         }
      }
    }
    

    相关文章

      网友评论

        本文标题:Nginx三种虚拟主机配置方法

        本文链接:https://www.haomeiwen.com/subject/notoyqtx.html