概述
系统环境:
[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
一、配置基于IP的虚拟主机
1、配置网卡IP,先查看本地虚拟机ip地址,物理机IP是:192.168.80.1
[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-eno16777736
TYPE="Ethernet"
BOOTPROTO="static"
NAME="eno16777736"
DEVICE="eno16777736"
ONBOOT="yes"
IPADDR=192.168.80.10
NETMASK=255.255.255.0
GATEWAY=192.168.80.2
DNS1=192.168.1.1
2、在我的虚机网卡eno16777736(centos6的网卡名字是eth0)设备上添加另外两个IP别名,192.168.80.11和192.168.80.12
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eno16777736:0
TYPE=Ethernet
BOOTPROTO=static
NAME=eno16777736:0
DEVICE=eno16777736:0
ONBOOT=yes
IPADDR=192.168.80.11
NETMASK=255.255.255.0
GATEWAY=192.168.80.2
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eno16777736:1
TYPE=Ethernet
BOOTPROTO=static
NAME=eno16777736:1
DEVICE=eno16777736:1
ONBOOT=yes
IPADDR=192.168.80.12
NETMASK=255.255.255.0
GATEWAY=192.168.80.2
修改完,重启网络服务
[root@localhost ~]# systemctl restart network
3、查看确认:
[root@localhost ~]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
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: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:38:cf:ef brd ff:ff:ff:ff:ff:ff
inet 192.168.80.10/24 brd 192.168.80.255 scope global eno16777736
valid_lft forever preferred_lft forever
inet 192.168.80.11/24 brd 192.168.80.255 scope global secondary eno16777736:0
valid_lft forever preferred_lft forever
inet 192.168.80.12/24 brd 192.168.80.255 scope global secondary eno16777736:1
valid_lft forever preferred_lft forever
4、ping测试正常
[root@localhost ~]# ping 192.168.80.11
PING 192.168.80.11 (192.168.80.11) 56(84) bytes of data.
64 bytes from 192.168.80.11: icmp_seq=1 ttl=64 time=0.032 ms
64 bytes from 192.168.80.11: icmp_seq=2 ttl=64 time=0.042 ms
^C
--- 192.168.80.11 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1014ms
rtt min/avg/max/mdev = 0.032/0.037/0.042/0.005 ms
[root@localhost ~]# ping 192.168.80.12
PING 192.168.80.12 (192.168.80.12) 56(84) bytes of data.
64 bytes from 192.168.80.12: icmp_seq=1 ttl=64 time=0.058 ms
64 bytes from 192.168.80.12: icmp_seq=2 ttl=64 time=0.046 ms
^C
--- 192.168.80.12 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms
二、yum 安装nginx
1、如果默认yum install -y nginx安装不成功就先配置yum源,否则跳过。
[root@localhost ~]# vim /etc/yum.repos.d/epel.repo
[epel]
name=aliyun epel
baseurl=http://mirrors.aliyun.com/epel/7Server/x86_64/
gpgcheck=0
2、修改nginx配置文件,默认路径是 /etc/nginx/nginx.conf(主要是黑色粗体的配置,其他不写都可以)
[root@localhost ~]# vim /etc/nginx/nginx.conf
默认IP 192.168.80.10的nginx配置
server {
listen 80;
server_name _;
root /usr/share/nginx/html; #网页文件存放的目录
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
添加server1 192.168.80.11的nginx配置
server {
listen 80;
server_name 192.168.80.11;
root /usr/share/nginx/server1; #网页文件存放的目录
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
添加server2 192.168.80.12的nginx配置
server {
listen 80;
server_name 192.168.80.12;
root /usr/share/nginx/server2; #网页文件存放的目录
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
启动服务
[root@localhost ~]# systemctl start nginx
3、添加另外两个新增IP的nginx页面。新建server*目录,然后拷贝默认目录html里面的文件到新目录并修改网页头部内容以便区分。
server1
[root@localhost nginx]# mkdir server1
[root@localhost nginx]# cp -R html/* server1/
[root@localhost server1]# vim index.html
修改各自目录下的index.html
77 <h1>Welcome to <strong>server1</strong> on Fedora!</h1>
server2
[root@localhost nginx]# mkdir server2
[root@localhost nginx]# cp -R html/* server2/
[root@localhost server2]# vim index.html
修改各自目录下的index.html
77 <h1>Welcome to <strong>server2</strong> on Fedora!</h1>
三、重启服务并查看
[root@localhost ~]# systemctl restart network
[root@localhost ~]# systemctl restart nginx
网页访问成功!
最后
以上就是害羞百合为你收集整理的Centos7 nginx配置基于多个IP的虚拟主机的全部内容,希望文章能够帮你解决Centos7 nginx配置基于多个IP的虚拟主机所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复