Nginx多站 多IP 多端口配置 详解 一步一步得来 你也搞得定

Nginx多站点配置

  1. 一个nginx上可以运行多个网站。有多种方式:

    1. http:// + ip/域名 + 端口 + URI

    2. 其中,ip/域名变了,那么网站入口就变了,端口变了,网站入口也变了,而每个网站都需要有自己的入口

[root@web01 nginx]# cat nginx.conf
worker_processes 2;
events {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;
charset utf-8;

# 8yy复制8行,小p黏贴
server {
listen 80;
server_name localhost;
location / {
root /html/one;
index index.html index.htm;
}
}

server {
listen 81;
server_name localhost;
location / {
root /html/two;
index index.html index.htm;
}
}

server {
listen 82;
server_name localhost;
location / {
root /html/three;
index index.html index.htm;
}
}
}

[root@web01 nginx]# mkdir -p /html/{one,two,three}
[root@web01 nginx]# echo 'one' >/html/one/index.html
[root@web01 nginx]# echo 'two' >/html/two/index.html

Nginx 多IP配置文件

  • 每个操作系统其实都可以配置多个ip地址,多个ip地址一定要手动配置昂.

    • Linux 配置多个IP

  • [root@web01 three]# cd /etc/sysconfig/network-scripts/
    [root@web01 network-scripts]# ls
    [root@web01 network-scripts]# vim ifcfg-ens33
    TYPE="Ethernet"
    BOOTPROTO="static"
    NAME="ens33"
    DEVICE="ens33"
    ONBOOT="yes"
    IPADDR1=192.168.61.139
    IPADDR2=192.168.61.140
    IPADDR3=192.168.61.141
    NETMASK=255.255.255.0
    GATEWAY=192.168.20.2
    DNS1=223.5.5.5
    # 保存退出
    # 重启网卡服务:systemctl restart network,查看ip地址:
    ------------------------------------------------------------------------
    [root@web01 network-scripts]# ip addr
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group
    default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
    valid_lft forever preferred_lft forever
    2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP
    group default qlen 1000
    link/ether 00:0c:29:37:68:fd brd ff:ff:ff:ff:ff:ff
    inet 192.168.61.139/24 brd 192.168.61.255 scope global noprefixroute ens33
    valid_lft forever preferred_lft forever
    inet 192.168.61.140/24 brd 192.168.61.255 scope global secondary
    noprefixroute ens33
    valid_lft forever preferred_lft forever
    inet 192.168.61.141/24 brd 192.168.61.255 scope global secondary
    noprefixroute ens33
    valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe37:68fd/64 scope link
    valid_lft forever preferred_lft forever

    Nginx配置文件

  • 多个ip地址都配置好了。那么之前的三个网站,我们调整一下配置即可,每个网站就可以都用80端口了

  • [root@web01 nginx]# vim nginx.conf
    worker_processes 1;
    events {
    worker_connections 1024;
    }
    ---------------------------------------------------------
    http {
    include mime.types;
    default_type application/octet-stream;
    charset utf-8;
    ----------------------------------------------------------
    server {
    listen 80;
    server_name localhost;
    location / {
    default_type application/octet-stream;
    charset utf-8;
    server {
    listen 192.168.61.139:80; # 直接写ip地址和端口,大家都是80端口
    server_name localhost;
    location / {
    root /web/one;
    index index.html index.htm;
    }
    
    }
    -----------------------------------------------------------
    server {
    listen 192.168.61.140:80;
    server_name localhost;
    location / {
    root /web/two;
    index index.html index.htm;
    }
    }
    -------------------------------------------------------------
    server {
    listen 192.168.61.141:80;
    server_name localhost;
    location / {
    root /web/three;
    index index.html index.htm;
    }
    }
    }
    
    --------------------------------------------------------------------
    # 保存退出
    
    # 检查一下语法
    
    [root@web01 nginx]# nginx -t
    
    # 重启nginx
    
    systemctl restart nginx  

    Nginx多域名配置

  • 多ip的方式其实也不太好,因为如果我们的多网站想在互联网上被公网访问

    • 那么就需要多个ip,而公网ip是收费的

    • 那么有一种省钱的方式,就是多域名方式

  • server {
    listen 80;
    server_name a.jaden.com;
    location / {
    root /html/one;
    index index.html index.htm;
    }
    }
    ------------------------------------------------
    server {
    listen 80;
    server_name b.jaden.com;
    location / {
    root /html/two;
    index index.html index.htm;
    }
    }
    ------------------------------------------------
    server {
    listen 80;
    server_name c.jaden.com;
    location / {
    root /html/three;
    index index.html index.htm;
    }
    }
    #修改完配置文件之后,重启nginx
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浩策

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值