• Nginx的应用之虚拟主机


    开始前请确保selinux关闭,否则当配置完虚拟主机后,尽管权限或者网站目录都正确,访问的结果也是403

    nginx的虚拟主机有三种方式:

    一、基于域名的虚拟主机

    (1)创建对应的web站点目录以及程序代码

    [root@web01 ~]# mkdir /data/www/{game,video}
    [root@web01 ~]# echo "game" > /data/www/game/index.html
    [root@web01 ~]# echo "video" > /data/www/video/index.html

    (2)配置不同域名的虚拟主机

    [root@web01 ~]# cat /etc/nginx/conf.d/game.conf
    server {
        listen       80;
        server_name  game.com;
        root /data/www/game;
        index index.html;
        ...
    }
    [root@web01 ~]# cat /etc/nginx/conf.d/video.conf
    server {
        ...
        listen       80;
        server_name  video.com;
        root /data/www/video;
        index index.html;
    }

    配置完虚拟主机后最好重启或重载nginx服务

    (3)修改hosts文件进行访问测试

    vim /etc/hosts
    127.0.0.1 game.com video.com

    curl game.com
    game

    curl video.com
    video

    二、基于IP的虚拟主机

      1、基于多网卡多IP的方式

    server {
        ...
        listen 10.0.0.10:80;
        ...
    }
    
    server {
        ...
        listen 10.0.0.11:80;
        ...
    }

      2、基于单网卡多IP的方式

    #添加一个IP
    [root@web01 ~]# ip addr add 10.0.0.11/24 dev eth0
    
    # 虚拟机配置方案
    [root@web01 ~]# cat /etc/nginx/conf.d/addr1.conf
    server {
        ...
        listen 10.0.0.10:80;
        ...
    }
    
    [root@web01 ~]# cat /etc/nginx/conf.d/addr2.conf
    server {
        ...
        listen 10.0.0.11:80;
        ...
    }

    三、基于端口的虚拟主机

    #仅修改listen监听端口即可, 但不能和系统端口出现冲突
    
    [root@web01 ~]# cat /etc/nginx/conf.d/port1.conf
    server {
        ...
        listen 80;
        ...
    }
    
    [root@web01 ~]# cat /etc/nginx/conf.d/port2.conf
    server {
        ...
        listen 81;
        ...
    }
    
    [root@web01 ~]# cat /etc/nginx/conf.d/port3.conf
    server {
        ...
        listen 82;
        ...
    }
  • 相关阅读:
    Anders Hejlsberg 和 Erich Gamma
    node-webkit
    用户密码加密存储十问十答,一文说透密码安全存储
    Spring Security-- 验证码功能的实现
    BigDecimal 专题
    基于微信开发 专题
    unwrapThrowable
    Android PullToRefreshExpandableListView的点击事件
    Android The content of the adapter has changed but ListView did not receive a notification
    Android入门:广播发送者与广播接收者
  • 原文地址:https://www.cnblogs.com/Smbands/p/11409663.html
Copyright © 2020-2023  润新知