阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

CentOS 7.3配置Nginx虚拟主机

188次阅读
没有评论

共计 1504 个字符,预计需要花费 4 分钟才能阅读完成。

实验环境

  1. 一台最小化安装的 CentOS 7.3 虚拟机

配置基本环境

1. 安装 nginx

yum install -y epel-*
yum isntall -y nginx vim

2. 建立虚机主机的站点根目录

mkdir /var/wwwroot
mkdir /var/wwwroot/site1
mkdir /var/wwwroot/site2
echo -e "site1" >> /var/wwwroot/site1/index.html
echo -e "site2" >> /var/wwwroot/site2/index.html

CentOS 7.3 配置 Nginx 虚拟主机

3. 关闭 CentOS 的防火墙

setenforce 0
systemctl stop firewalld
systemctl disable firewalld

CentOS 7.3 配置 Nginx 虚拟主机

配置基于端口的虚拟主机

1. 编辑 nginx 配置文件

vim /etc/nginx/conf.d/vhosts.conf

2. 添加以下内容

server {listen 8081;
    root /var/wwwroot/site1;
    index index.html;

    location / {}}
server {listen 8082;
  root /var/wwwroot/site2;
  index index.html;

  location / {}}

CentOS 7.3 配置 Nginx 虚拟主机

3. 启动 nginx 服务

systemctl start nginx

4. 在宿主机访问两个站点

http://192.168.204.135:8081/
http://192.168.204.135:8082/
CentOS 7.3 配置 Nginx 虚拟主机
CentOS 7.3 配置 Nginx 虚拟主机

配置基于域名的虚拟主机

1. 重新编辑 nginx 配置文件

vim /etc/nginx/conf.d/vhosts.conf

2. 删除原内容,重新添加以下内容

server {listen 80;
    server_name site1.test.com;
    root /var/wwwroot/site1;
    index index.html;

    location / {}}
server {listen 80;
  server_name site2.test.com;
  root /var/wwwroot/site2;
  index index.html;

  location / {}}

CentOS 7.3 配置 Nginx 虚拟主机

3. 重启 nginx 服务

systemctl restart nginx

4. 在 Windows 上修改 hosts 文件

编辑 C:\Windows\System32\drivers\etc\hosts 文件,
添加以下内容(根据实际情况自己修改)

192.168.204.135   site1.test.com  
192.168.204.135   site2.test.com

CentOS 7.3 配置 Nginx 虚拟主机

5. 在宿主机访问两个站点

http://site1.test.com/
http://site2.test.com/

CentOS 7.3 配置 Nginx 虚拟主机
CentOS 7.3 配置 Nginx 虚拟主机

配置基于 IP 的虚拟主机

1. 在虚拟机增加两个 IP 地址

ifconfig ens33:1 192.168.204.151
ifconfig ens33:2 192.168.204.152

2. 重新编辑 nginx 配置文件

vim /etc/nginx/conf.d/vhosts.conf

3. 删除原内容,重新添加以下内容

server {listen 192.168.204.151:80;
    root /var/wwwroot/site1;
    index index.html;

    location / {}}
server {listen 192.168.204.152:80;
  root /var/wwwroot/site2;
  index index.html;

  location / {}}

CentOS 7.3 配置 Nginx 虚拟主机

4. 重启 nginx 服务

systemctl restart nginx

5. 在宿主机访问两个站点

http://192.168.204.151/
http://192.168.204.152/
CentOS 7.3 配置 Nginx 虚拟主机
CentOS 7.3 配置 Nginx 虚拟主机

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-21发表,共计1504字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中