zoukankan      html  css  js  c++  java
  • 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;
        ...
    }
  • 相关阅读:
    读库存扣减系列文章有感
    为什么不要将spring-boot相关依赖打入二方包
    volatile的特性代码验证
    谈谈JVM(基础模型)
    谈String,StringBuilder,StringBuffer随笔
    maven 安装
    Mysql中常用的函数
    web网页打印的方法(浏览器通用)
    web网页打印的方法
    代理服务器的用途
  • 原文地址:https://www.cnblogs.com/Smbands/p/11409663.html
Copyright © 2011-2022 走看看