zoukankan      html  css  js  c++  java
  • Nginx 从入门到放弃(二)

    学习完了nginx的基本知识后,我们来了解下nginx的虚拟主机。

    说到虚拟主机,那就得说一说虚拟主机的三种方式了

    • 基于端口的虚拟主机
    • 基于域名的虚拟主机
    • 基于ip的虚拟主机

    基于端口实现虚拟主机

    只需要修改配置文件nginx.conf,增加一个server即可

    server {
            listen 2022;
            server_name z.com;
    
            location / {
                root /usr/local/nginx/z.com;
                index 2022.html;
            }
        }
    server {
            listen 2024;
            server_name z.com;
    
            location / {
                root /usr/local/nginx/z.com;
                index 2022.html;
            }
        }
    server {
            listen 2023;
            server_name z.com;
    
            location / {
                root /usr/local/nginx/z.com;
                index 2022.html;
            }
        }

    效果如下

    基于域名的虚拟主机

    server {
            listen 80;
            server_name z.com;
    
            location / {
                root z.com;
                index index.html;
            }
        }

    要想要测试成功,需要修改hosts文件,使得该域名指向本机

    基于ip的虚拟主机

    server {
            listen 80;
            server_name 192.168.10.11;
    
            location / {
                root /usr/local/nginx/ip;
                index index.html;
            }
        }

    当本地有多个ip地址时可以使用这种方式运行多个web服务。

    总结

    我一直在想虚拟主机有什么用呢?就只是多几种方式访问本机服务器而已,是否这样子就可以本地运行多个web网站了呢?

    比如我有两个网站,一个是电商的,一个是博客,那么我就可以给电商一个域名虚拟主机,博客一个域名虚拟主机就好了。

  • 相关阅读:
    暑假训练第三周总结
    UVA 1212 Duopoly
    UVA 12125 March of the Penguins
    UVA 1345 Jamie's Contact Groups
    UVA 10806 Dijkstra, Dijkstra.
    暑假训练第一周总结
    HDU 5792 World is Exploding
    HDU 5791 Two
    HDU 5787 K-wolf Number
    Sql With as 用法
  • 原文地址:https://www.cnblogs.com/welisit/p/11012940.html
Copyright © 2011-2022 走看看