nginx 基于IP的多虚拟主机配置

nginx 基于IP的多虚拟主机配置

1.基于IP的多虚拟主机配置

1.1 网络配置

linux操作系统支持IP别名的添加。

nginx 服务器提供的每台虚拟主机对应配置一个不同的IP,因此需要将网卡设置为同时能够监听多个IP地址。

先查看当前的网络配置:ifconfig

再为eth1(对应自己的网卡名称)添加IP别名:192.168.1.66 和 192.168.1.99

c 复制代码
ifconfig eth1:0 192.168.1.66 netmask 255.255.255.0 up 
ifconfig eth1:1 192.168.1.99 netmask 255.255.255.0 up

接着查看配置结果:ifconfig

比之前多了两个网络配置:eth1:0 和 eth1:1

1.2 配置nginx

来到nginx的安装目录,并复制html-66和html-99:

c 复制代码
cd /usr/local/nginx 
cp -r html html-66 
cp -r html html-99

编辑html-66和html-99的首页:

c 复制代码
vim ./html-66/index.html 
vim ./html-99/index.html

改为可以区分的显示页面即可,后面用来测试的显示页面,随意发挥。

接着配置nginx.conf

c 复制代码
vim ./conf/nginx.conf

加入以下内容

c 复制代码
    server {
        listen 80;
        server_name 192.168.1.66;
        location / {
            root html-66;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html-66;
        }
    }

    server {
        listen 80;
        server_name 192.168.1.99;
        location /{
            root html-99;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html-99;
        }
    }

配置完成保存退出::wq

说明:

  • listen 80; 监听80端口

  • server_name 虚拟主机的指令,这里基于IP配置

  • root html的根目录

  • index root根目录下的首页

先检查nginx.conf 配置语法是否正确:

cd sbin nginx -t

启动nginx

nginx

nginx 关停和重启命令

nginx -s stop nginx -s reload

到目前为止全部配置完成,接下来测试

curl http://192.168.1.66 curl http://192.168.1.99

能正常的打印出 html-66/html-99 目录下的index.html 页面说明成功了

相关推荐
全栈工程师修炼指南4 小时前
Nginx | stream content 阶段:UDP 协议四层反向代理浅析与实践
运维·网络·网络协议·nginx·udp
鹏北海5 小时前
micro-app 微前端项目部署指南
前端·nginx·微服务
全栈工程师修炼指南6 小时前
Nginx | stream content 阶段:TCP 协议四层反向代理浅析与实践
运维·网络·网络协议·tcp/ip·nginx
森焱森7 小时前
详解 Spring Boot、Flask、Nginx、Redis、MySQL 的关系与协作
spring boot·redis·python·nginx·flask
考琪8 小时前
Nginx打印变量到log方法
java·运维·nginx
消失的旧时光-19439 小时前
Nginx 是什么?为什么它不写在代码里?——从 0 认识 Nginx
运维·服务器·nginx
不像程序员的程序媛19 小时前
Nginx日志切分
服务器·前端·nginx
JoySSLLian1 天前
手把手教你安装免费SSL证书(附宝塔/Nginx/Apache配置教程)
网络·人工智能·网络协议·tcp/ip·nginx·apache·ssl
一分半心动1 天前
宝塔面板lnmp架构,tp6框架网站伪静态
nginx·php
全栈工程师修炼指南1 天前
Nginx | stream 四层反向代理:SSL、PREREAD 阶段模块指令浅析与实践
运维·网络·网络协议·nginx·ssl