使用nginx:1.24.0-bullseye部署Nginx环境和域名、端口、子目录的虚拟主机配置方法

发表于 2023-04-18 10:57:23
阅读 199

介绍

介绍

前面福哥带着大家学习了包括PHP、MySQl、Redis、Elasticsearch几个基础服务的搭建方法,还学习了使用docker-compose工具编排这些服务的方法,通过这些基础服务完全可以用来支持基于PHP语言的开发的网站环境了。

这里面有个问题,一台服务器只有一个80端口,如果我们有很多网站都需要通过这个80端口发布怎么办?这里面就会涉及到web服务器的一个技术——虚拟主机技术。虚拟主机就是通过不同的主机名(域名)将多个网站集中部署在一起通过一个80端口发布出去的一种技术,虚拟主机还可以通过不同的端口将多个网站集中部署在一起发布出去,虚拟主机还可以通过不同的子目录将多个网站集中部署在一起发布出去。

能实现这个目的的软件很多,今天福哥就给大家讲讲如何通过Nginx来实现虚拟主机的部署!

环境

镜像版本
nginx:1.24.0-bullseye
操作系统
CentOS 7 x86_64 2009
服务器TFCentOS7x64
IP192.168.168.68
端口80

安装

Dockerfile

镜像

福哥选择的是nginx:1.24.0-bullseye这个基础镜像。

https://hub.docker.com/_/nginx/tags?page=1&name=1.24.0-bullseye&ordering=-last_updated

home/topic/2023/0425/09/5dd4f3ea4dc6b902981076f291770403.png

现在hub.docker.com依然打不开,大家就选这个版本吧!

拉取镜像nginx:1.24.0-bullseye,添加到registry.tongfu.net:5000私有仓库里面。

docker pull nginx:1.24.0-bullseye
docker tag nginx:1.24.0-bullseye registry.tongfu.net:5000/nginx:1.24.0-bullseye
docker rmi nginx:1.24.0-bullseye
docker images | grep nginx

home/topic/2023/0817/22/c332c3984838dac37d909748143eea10.png

创建镜像目录

创建镜像目录tfnginx并切换到tfnginx目录下面。

mkdir /tongfu.net/data/dockerfile/tfnginx
cd /tongfu.net/data/dockerfile/tfnginx

home/topic/2023/0817/22/5a85e06d54f4a758104189b34c8489b7.png

依赖镜像

第一行写上依赖镜像nginx:1.24.0-bullseye。

FROM registry.tongfu.net:5000/nginx:1.24.0-bullseye

维护者信息

这是福哥写的维护者信息。

# for MAINTAINER
MAINTAINER Author: Andy Bogate
MAINTAINER Email: tongfu@tongfu.net
MAINTAINER Home page: https://tongfu.net
MAINTAINER Datetime: 2023/04/26
MAINTAINER Version: v1.0

配置文件

复制配置文件

启动一个临时容器ttt,从临时容器里面把默认的nginx.conf和default.conf配置文件复制出来。

docker run -tid --name ttt -h ttt registry.tongfu.net:5000/nginx:1.24.0-bullseye
docker cp ttt:/etc/nginx/nginx.conf .
docker cp ttt:/etc/nginx/conf.d/default.conf .
docker rm -f ttt

home/topic/2023/0817/22/6967805a1cd0d24bd3344f067e3b43f6.png

nginx.conf

这个是nginx的主配置文件,存储在/etc/nginx/目录下面,原始内容如下:

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

这个文件基本不用修改,我们保持原样就行~

default.conf

这个是默认虚拟主机的配置文件,存储在/etc/nginx/conf.d/目录下面,原始内容如下:

server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

这个是默认的虚拟主机配置文件,后面福哥会用自己设计的虚拟主机配置文件,所以这个就删除掉吧!

RUN rm -f /etc/nginx/conf.d/default.conf

域名方式

by-host.conf是福哥建立的虚拟主机配置文件,这个配置文件是通过主机名(域名)实现在一个端口下部署多个网站的,访问每个网站需要使用特定的主机名(域名)。

第一个网站使用域名tfphp-by-host.tongfu.net,第二个网站使用域名tfphp-by-host2.tongfu.net,两个网站都指向之前我们搞的tfphp这个服务,通过upstream绑定两个网站。

upstream tfphp-by-host {
   server tfphp:80;
}

server {
   listen 80;

   server_name tfphp-by-host.tongfu.net;

   location / {
        proxy_pass       http://tfphp-by-host;
        proxy_redirect   off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-Ip $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;      
   }
}

server {
   listen 80;

   server_name tfphp-by-host2.tongfu.net;

   location / {
        proxy_pass       http://tfphp-by-host;
        proxy_redirect   off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-Ip $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;      
   }
}

这种方式配置每个网站会有完整的HTTP信息,强烈推荐!

在创建新的镜像的时候把by-host.conf配置文件拷贝进去。

COPY by-host.conf /etc/nginx/conf.d/

端口方式

by-port.conf是福哥建立了又一个虚拟主机配置文件,这个配置文件是通过端口实现多个网站的部署的,这种方式需要多个空闲的服务端口,算是比较省事的做法。

第一个网站使用的是81端口,第二个网站使用的是82端口,,两个网站都指向之前我们搞的tfphp这个服务,通过upstream绑定两个网站。

upstream tfphp-by-port {
   server tfphp:80;
}

server {
   listen 81;

   server_name tfphp-by-port.tongfu.net;

   location / {
        proxy_pass       http://tfphp-by-port;
        proxy_redirect   off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-Ip $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;      
   }
}

server {
   listen 82;

   server_name tfphp-by-port.tongfu.net;

   location / {
        proxy_pass       http://tfphp-by-port;
        proxy_redirect   off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-Ip $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;      
   }
}

这种配置方式每个网站会有完整的HTTP信息,但是tfphp并不知道非80端口的存在,一般推荐!

在创建新的镜像的时候把by-port.conf配置文件拷贝进去。

COPY by-port.conf /etc/nginx/conf.d/

子目录方式

by-dir.conf是福哥建立了又一个虚拟主机配置文件,这个配置文件是通过子目录实现多个网站的部署的,这种方式不需要额外的资源,只需要设置不同的子目录就行,是资源比较紧张的情况下的选择。

第一个网站使用的是/dir/子目录,第二个网站使用的是/dir2/子目录,,两个网站都指向之前我们搞的tfphp这个服务,通过upstream绑定两个网站。

upstream tfphp-by-dir {
   server tfphp:80;
}

server {
   listen 80;

   server_name tfphp-by-dir.tongfu.net;

   location ~ ^\/dir\/(.*)$ {
        proxy_pass       http://tfphp-by-dir/$1;
        proxy_redirect   off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-Ip $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;      
   }

   location ~ ^\/dir2\/(.*)$ {
        proxy_pass       http://tfphp-by-dir/$1;
        proxy_redirect   off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-Ip $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;      
   }
}

这种配置方式网站得到的路径是以子目录为根目录计算的,一般推荐!

在创建新的镜像的时候把by-dir.conf配置文件拷贝进去。

COPY by-dir.conf /etc/nginx/conf.d/

创建镜像

使用下面的命令创建tfnginx:1.24.0-1.0.0镜像。

docker build -f Dockerfile \
-t registry.tongfu.net:5000/tfnginx:1.24.0-1.0.0 ./

home/topic/2023/0817/22/120aa0e5f58f72f8e37e6c2e22a1e483.png

查看镜像

看看新的镜像。

docker images | grep tfnginx

home/topic/2023/0817/22/ded565772343fc5222c17b2352491d64.png

测试

宿主机程序文件

在tfphp服务的数据目录下面建立一个php程序文件tfnginx.php,写入下面的代码。

<?php

var_dump($_SERVER['HTTP_HOST']);

启动容器

上一课我们把tfphp服务的docker-compose配置给删除了,可以参考上一课的实例里面的完整版本的docker-compose.yml配置文件内容恢复回来。

因为tfphp服务占用了80端口,而tfnginx服务也需要占用80端口,为了避免冲突需要把tfphp的端口映射关掉。就是要把tfphp服务的ports配置删除掉。

  tfphp:
    build:
      dockerfile: Dockerfile
      context: ./tfphp7.4nginx
    image: registry.tongfu.net:5000/tfphp:7.4-nginx-1.0.0
    container_name: tfphp
    volumes:
      - /tongfu.net/data/docker/data/tfphp/html:/var/www/html

然后我们通过docker-compose工具把整个服务组的服务都启动起来!

docker-compose -f /tongfu.net/data/dockerfile/docker-compose.yml up -d

home/topic/2023/0817/22/bcc9ca506d2f79c83fdc5dd7cb1668f5.png

使用下面的命令基于tfnginx:1.24.0-1.0.0镜像启动一个容器,将80端口和443端口映射到宿主机上面。

docker run -tid \
--name tfnginx \
-h tfnginx \
--net tfnet \
-p 80:80 \
-p 443:443 \
-p 81:81 \
-p 82:82 \
registry.tongfu.net:5000/tfnginx:1.24.0-1.0.0

home/topic/2023/0817/22/6e66abf5a409a96ec704b24f20eb9797.png

设置hosts解析

在Windows上以管理员权限运行cmd命令行窗口,右键左下角的windows图标选择搜索,在弹出的输入框里面输入“cmd”,最后点击右边的“以管理员身份运行”。

home/topic/2023/0817/22/c89677dd3dfc418389b5292c14cb69ab.png

在打开的cmd窗口里面输入如下命令,以管理员身份打开hosts文件。

notepad drivers\etc\hosts

home/topic/2023/0817/22/6139b85de36648d48600a1caf3bfc5fc.png

在打开的记事本里添加四个刚刚在nginx里面配置的域名。

192.168.168.68      tfphp-by-host.tongfu.net
192.168.168.68      tfphp-by-host2.tongfu.net
192.168.168.68      tfphp-by-port.tongfu.net
192.168.168.68      tfphp-by-dir.tongfu.net

home/topic/2023/0817/22/4006d6bc29400c461eb0deebf29251f4.png保存后退出记事本。

测试Nginx

域名方式

使用tfphp-by-host.tongfu.net域名测试。

http://tfphp-by-host.tongfu.net/tfnginx.php

home/topic/2023/0817/22/88c7c541849af9f26bfc6017b144777f.png

使用tfphp-by-host2.tongfu.net域名测试。

http://tfphp-by-host2.tongfu.net/tfnginx.php

home/topic/2023/0817/22/5072341bcd3966eb87f57f7b31f7cc85.png

端口方式

使用81端口测试。

http://tfphp-by-port.tongfu.net:81/tfnginx.php

home/topic/2023/0817/22/a4e69ff034540a5c72ece240c5efb574.png

使用82端口测试。

http://tfphp-by-port.tongfu.net:82/tfnginx.php

home/topic/2023/0817/22/36ed5507c31a88aa2b52e9bb493d1818.png

子目录方式

使用/dir/子目录测试。

http://tfphp-by-dir.tongfu.net/dir/tfnginx.php

home/topic/2023/0817/22/2a42a3015ae4395f8fecb56b2183d79b.png

使用/dir2/子目录测试。

http://tfphp-by-dir.tongfu.net/dir2/tfnginx.php

home/topic/2023/0817/23/2a5b9507a70b4bde6f0cada8b34377c5.png

docker-compose管理

镜像测试完成了,接下来我们要把tfnginx交给docker-compose来管理了。

配置文件

配置文件增加tfnginx的配置信息。

这里面通过depends_on选项设置了tfnginx是依赖tfphp服务的,也就是说启动tfnginx服务的时候如果tfphp没有启动的话会先启动tfphp服务。

  tfnginx:
    build:
      dockerfile: Dockerfile
      context: ./tfnginx
    image: registry.tongfu.net:5000/tfnginx:1.24.0-1.0.0
    container_name: tfnginx
    ports:
      - 80:80
      - 443:443
      - 81:81
      - 82:82
    depends_on:
      - tfphp

启动

因为刚刚我们手动启动的容器tfnginx占用了端口,所以先要删除tfnginx容器。

docker rm -f tfnginx

home/topic/2023/0817/23/7912181d0467809d39128d0fd5f1899d.png

启动docker-compose就可以自动创建nginx镜像自动启动tfnginx容器了。

docker-compose -f /tongfu.net/data/dockerfile/docker-compose.yml up -d

home/topic/2023/0817/23/3d6c3c36461ba5d9a8dd7c342faa52bc.png

总结

今天福哥带着大家一起学习了通过nginx:1.24.0-bullseye基础镜像搭建Nginx运行环境的方法。Nginx是最流行的web服务器之一,大部分网站都是使用Nginx作为网站的web服务器软件的,我们要把Nginx玩熟了,玩透了,这样在后面的学习当中才会顺畅!

后面福哥会带着大家学习使用rancher来搭建kubernetes(k8s)服务器集群环境,以及使用rancher环境来部署一系列的服务,相比较docker-compose来说kubernetes可以管理更多的服务器,kubernetes是大型web平台的选择!