• Nginx实现虚拟主机


    一、阿里云域名解析

    打开已经购买的域名,选择解析功能 添加记录

    二、虚拟主机配置

    1、不用nginx如何让一台服务器绑定多个ip地址()

    方法一:

    使用标准的网络配置工具(比如ifconfig和route命令)添加lP别名:

    当前ip配置情况:

    在eth0网卡再绑定一个ip:192.168.101.103

    /sbin/ifconfig eth0:1 192.168.101.103 broadcast 192.168.101.255 netmask 255.255.255.0 up

    /sbin/route add -host 192.168.101.103 dev eth0:1

    方法二:

    将/etc/sysconfig/network-scripts/ifcfg-eth0文件复制一份,命名为ifcfg-eth0:1

    修改其中内容:

    DEVICE=eth0:1

    IPADDR=192.168.25.103

    其他项不用修改,重启系统

    如何给Ubuntu网站绑定多个IP,怎么给Ubuntu服务器设置多IP? Ubuntu其实只需要设置一个文件 /etc/network/interfaces 即可。

    iface lo inet loopback

    auto ens33

    iface ens33 inet static

    address 10.0.143.116

    netmask 255.255.255.0

    gateway 10.0.143.1

    auto ens33:0

    iface ens33:0 inet static

    address 10.0.143.202

    netmask 255.255.255.0

    多个不同IP段的 /etc/network/interfaces 配置文件的范例如下:

    # The loopback network interface

    auto lo

    iface lo inet loopback

    # The primary network interface

    auto eth0

    iface eth0 inet static

    address 8.8.8.2

    netmask 255.255.255.248

    gateway 8.8.8.1 要注意这里,多个不同IP段,只要1个gateway配置即可,其他IP不需要配置gateway

    auto eth0:0

    iface eth0:0 inet static

    address 8.8.8.3

    netmask 255.255.255.248

    auto eth0:1

    iface eth0:1 inet static

    address 8.8.8.4

    netmask 255.255.255.248

    配置文件完成后,重启

    第二种方式是动态修改.

    这里直接使用 ifconfig 命令

    sudo ifconfig eth0:0 110.25.*.* broadcast 110.25.*.255 netmask 255.255.255.0

    注:

    第一种方式在你修改重启之后不会失效.这种方式稍微操作不当,则会造成远程无法连接。

    第二种方式虽然只对当前生效,重启之后就会失效.但是不会因为操作不当给自己造成的不便。

    修改成功之后用ifconfig命令看一下

    eth0

    eth0:0 (多出一个eth0:0,查看与eth0的网关是否一样)

    2、nginx基于ip的虚拟主机配置

    server {
        listen       80;
        server_name  192.168.25.141;
        
        #charset koi8-r;
        
        #access_log  logs/host.access.log  main;
        
        location / {
            root   /www/blog;
            index  index.html index.htm;
        }
        
        
    }
    
    server {
        listen       80;
        server_name  192.168.25.100;
        
        #charset koi8-r;
        
        #access_log  logs/host.access.log  main;
        
        location / {
            root   /www/org;
            index  index.html index.htm;
        }
        
        
        }

    3、nginx基于端口的虚拟主机

    server {
            listen       81;
            server_name  192.168.25.141;
     
            #charset koi8-r;
     
            #access_log  logs/host.access.log  main;
     
            location / {
                root   /www/blog;
                index  index.html index.htm;
            }
     
            
        }
        server {
            listen       82;
            server_name  192.168.25.141;
     
            #charset koi8-r;
     
            #access_log  logs/host.access.log  main;
     
            location / {
                root   /www/org;
                index  index.html index.htm;
            }
     
            
        }

    4、nginx基于域名的虚拟主机

    server {
            listen       80;
            server_name  www.demo.com;
     
            #charset koi8-r;
     
            #access_log  logs/host.access.log  main;
     
            location / {
                root   /www/blog;
                index  index.html index.htm;
            }
     
            
        }
     
        server {
            listen       80;
            server_name  ok.demo.com;
     
            #charset koi8-r;
     
            #access_log  logs/host.access.log  main;
     
            location / {
                root   /www/org;
                index  index.html index.htm;
            }
     
            
        }

    三、ServerName配置规则

    1、上到下 匹配就返回 没有匹配返回第一个配置

    2、一个server_name 后面可以空格分割 配置多个域名或主机名

    server_name ok.demo.com www.demo.com;

    3、可以配置通配符,如下除了www.demo.com 其他都访问 /www/org下的文件

    server {
            listen       80;
            server_name  www.demo.com;
     
            #charset koi8-r;
     
            #access_log  logs/host.access.log  main;
     
            location / {
                root   /www/blog;
                index  index.html index.htm;
            }
     
            
        }
     
        server {
            listen       80;
            server_name  *.demo.com;
     
            #charset koi8-r;
     
            #access_log  logs/host.access.log  main;
     
            location / {
                root   /www/org;
                index  index.html index.htm;
            }
     
            
        }

    4、server_name 后面可以跟一个正则表达式匹配

    server_name ~^[0-9]+\.demo.com$

    四、互联网域名使用场景

    1、多用户域名系统

    2、二级域名系统

    3、短网址生成

  • 相关阅读:
    沙盒软件-软件多开-命令行-运行参数-进程通信-运行和评估
    含文档+PPT+源码等]精品微信小程序校园论坛+后台管理系统|前后分离VUE[包运行成功]
    security跨域配置
    kr第三阶段(二)32 位汇编
    mac m1 m2 深度学习环境(pytorch)配置
    卷积神经网络(CNN)识别验证码
    【MTK】 配置GPIO控制前置闪光灯
    关于分离式编译(.h和.cpp,编译哪些东西)
    SpringBoot+Vue项目流浪狗领养管理系统的设计与实现
    Redis分布式锁及其常见问题解决方案
  • 原文地址:https://blog.csdn.net/qq_34491508/article/details/125909164