天天看點

使用nginx:1.24.0-bullseye部署Nginx環境

作者:同福程式設計

1. 介紹

1.1 介紹

前面福哥帶着大家學習了包括PHP、MySQl、Redis、Elasticsearch幾個基礎服務的搭建方法,還學習了使用docker-compose工具編排這些服務的方法,通過這些基礎服務完全可以用來支援基于PHP語言的開發的網站環境了。

這裡面有個問題,一台伺服器隻有一個80端口,如果我們有很多網站都需要通過這個80端口釋出怎麼辦?這裡面就會涉及到web伺服器的一個技術——虛拟主機技術。虛拟主機就是通過不同的主機名(域名)将多個網站集中部署在一起通過一個80端口釋出出去的一種技術,虛拟主機還可以通過不同的端口将多個網站集中部署在一起釋出出去,虛拟主機還可以通過不同的子目錄将多個網站集中部署在一起釋出出去。

能實作這個目的的軟體很多,今天福哥就給大家講講如何通過Nginx來實作虛拟主機的部署!

1.2 環境

鏡像版本 nginx:1.24.0-bullseye
作業系統 CentOS 7 x86_64 2003
伺服器 TFCentOS7x64
IP 192.168.168.68
端口 80

2. 安裝

2.1 Dockerfile

2.1.1 鏡像

福哥選擇的是nginx:1.24.0-bullseye這個基礎鏡像。

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

使用nginx:1.24.0-bullseye部署Nginx環境

2.1.2 維護者資訊

這是福哥寫的維護者資訊。

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

2.2 配置檔案

2.2.1 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;
}           

這個檔案基本不用修改,我們保持原樣就行~

2.2.2 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 ~ \.phpnbsp;{
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.phpnbsp;{
    #    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           

2.2.3 域名方式

by-host.conf是福哥建立的虛拟主機配置檔案,這個配置檔案是通過主機名(域名)實作在一個端口下部署多個網站的,通路每個網站需要使用特定的主機名(域名)。

第一個網站使用域名tfphp-by-host.tongfu.net,第二個網站使用域名tfphp-by-host2.tongfu.net,兩個網站都指向之前我們搞的php-tfphp這個服務,通過upstream綁定兩個網站。

upstream tfphp-by-host {
   server php-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/           

2.2.4 端口方式

by-port.conf是福哥建立了又一個虛拟主機配置檔案,這個配置檔案是通過端口實作多個網站的部署的,這種方式需要多個空閑的服務端口,算是比較省事的做法。

第一個網站使用的是81端口,第二個網站使用的是82端口,,兩個網站都指向之前我們搞的php-tfphp這個服務,通過upstream綁定兩個網站。

upstream tfphp-by-port {
   server php-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資訊,但是php-tfphp并不知道非80端口的存在,一般推薦!

在建立新的鏡像的時候把by-port.conf配置檔案拷貝進去。

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

2.2.5 子目錄方式

by-dir.conf是福哥建立了又一個虛拟主機配置檔案,這個配置檔案是通過子目錄實作多個網站的部署的,這種方式不需要額外的資源,隻需要設定不同的子目錄就行,是資源比較緊張的情況下的選擇。

第一個網站使用的是/dir/子目錄,第二個網站使用的是/dir2/子目錄,,兩個網站都指向之前我們搞的php-tfphp這個服務,通過upstream綁定兩個網站。

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

server {
   listen 80;

   server_name tfphp-by-dir.tongfu.net;

   location ~ ^\/dir\/(.*)nbsp;{
        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\/(.*)nbsp;{
        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/           

2.3 建立鏡像

使用下面的指令建立tfnginx:1.24.0-1.0.0鏡像。

docker build -f Dockerfile \
-t registry.tongfu.net:5000/tfnginx:1.24.0-1.0.0 ./           
使用nginx:1.24.0-bullseye部署Nginx環境

2.4 檢視鏡像

看看新的鏡像。

docker images | grep tfnginx           
使用nginx:1.24.0-bullseye部署Nginx環境

3. 測試

3.1 主控端程式檔案

在tfphp服務的資料目錄下面建立一個php程式檔案tfnginx.php,寫入下面的代碼。

<?php

var_dump($_SERVER['HTTP_HOST']);           

3.2 啟動容器

如果你的TFCentOS7x64伺服器的php-tfphp服務沒有啟動的話,請先通過docker-compose把它啟動起來,否則後面的測試會有問題!

docker-compose -f /tongfu.net/data/dockerfile/docker-compose.yml up -d           
使用nginx:1.24.0-bullseye部署Nginx環境

使用下面的指令基于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           
使用nginx:1.24.0-bullseye部署Nginx環境

3.3 設定hosts解析

在Windows上以管理者權限運作cmd指令行視窗,右鍵左下角的windows圖示選擇搜尋,在彈出的輸入框裡面輸入“cmd”,最後點選右邊的“以管理者身份運作”。

使用nginx:1.24.0-bullseye部署Nginx環境

在打開的cmd視窗裡面輸入如下指令,以管理者身份打開hosts檔案。

notepad drivers\etc\hosts           
使用nginx:1.24.0-bullseye部署Nginx環境

在打開的記事本裡添加四個剛剛在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           
使用nginx:1.24.0-bullseye部署Nginx環境

儲存後退出記事本。

3.4 測試Nginx

3.4.1 域名方式

使用tfphp-by-host.tongfu.net域名測試。

使用nginx:1.24.0-bullseye部署Nginx環境

使用tfphp-by-host2.tongfu.net域名測試。

使用nginx:1.24.0-bullseye部署Nginx環境

3.4.2 端口方式

使用81端口測試。

使用nginx:1.24.0-bullseye部署Nginx環境

使用82端口測試。

使用nginx:1.24.0-bullseye部署Nginx環境

3.4.3 子目錄方式

使用/dir/子目錄測試。

使用nginx:1.24.0-bullseye部署Nginx環境

使用/dir2/子目錄測試。

使用nginx:1.24.0-bullseye部署Nginx環境

4. docker-compose管理

鏡像測試完成了,接下來我們要把tfnginx交給docker-compose來管理了。

4.1 配置檔案

配置檔案增加tfnginx的配置資訊。

這裡面通過depends_on選項設定了tfnginx是依賴php-tfphp服務的,也就是說啟動tfnginx服務的時候如果php-tfphp沒有啟動的話會先啟動php-tfphp服務。

  tfnginx:
    build:
      dockerfile: Dockerfile
      context: ./nginx1.24.0
    image: registry.tongfu.net:5000/nginx:1.24.0-2.0.0
    container_name: tfnginx
    ports:
      - 80:80
      - 443:443
      - 81:81
      - 82:82
    depends_on:
      - php-tfphp           

4.2 啟動

因為剛剛我們手動啟動的容器tfnginx占用了端口,是以先要删除tfnginx容器。

docker rm -f tfnginx           
使用nginx:1.24.0-bullseye部署Nginx環境

啟動docker-compose就可以自動建立nginx鏡像自動啟動tfnginx容器了。

docker-compose -f /tongfu.net/data/dockerfile/docker-compose.yml up -d           
使用nginx:1.24.0-bullseye部署Nginx環境

5. 總結

今天福哥帶着大家一起學習了通過nginx:1.24.0-bullseye基礎鏡像搭建Nginx運作環境的方法。Nginx是最流行的web伺服器之一,大部分網站都是使用Nginx作為網站的web伺服器軟體的,我們要把Nginx玩熟了,玩透了,這樣在後面的學習當中才會順暢!

後面福哥會帶着大家學習使用rancher來搭建kubernetes(k8s)伺服器叢集環境,以及使用rancher環境來部署一系列的服務,相比較docker-compose來說kubernetes可以管理更多的伺服器,kubernetes是大型web平台的選擇!

https://m.tongfu.net/home/35/blog/514001.html

繼續閱讀