【Azure 应用服务】App Service For Container 配置Nginx,设置/home/site/wwwroot/目录为启动目录,并配置反向代理

问题描述

通过Docker Desktop for Linux,配置Nginx镜像后,自定义nginx.conf文件,修改启动目录和对 /out 路径的反向代理到博客园的博文地址 (https://www.cnblogs.com/lulight/p/15180884.html), 然后部署到Azure App Service中的整体实现方案。

 

操作步骤(共5步)

第 0 步:启动本地 Docker Desktop,并拉取Nginx 镜像

# 1. pull nginx image ... need docker for linux
docker pull nignx

注意:必须切换为 Linux Container,避免在拉去 Nginx 镜像时候出现如下错误:

C:Usersu>docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
no matching manifest for windows/amd64 10.0.19043 in the manifest list entries

第一步:创建Dockerfile 文件

FROM nginx 
COPY appnginx.html  /home/site/wwwroot/index.html
COPY . /home/site/wwwroot
COPY nginx.conf /etc/nginx/nginx.conf
RUN  .

注意:

  • 这里Dockerfile的名字必须为Dockerfile
  • 第一行 FROM nginx 表示这次构建的image是以nginx的镜像为基础
  • 第二行 表示把本地目录中的一个appnginx.html静态文件复制到 /home/site/wwwroot/下的index.html文件中
  • 第三行 表示把本地当前与Dockerfile同级目录中的所有内容都复制到 /home/site/wwwroot 中
  • 第四行 表示把自定义的nginx.conf文件复制到linux下的nginx的安装目录中 /etc/nginx/nginx.conf,代替默认的nginx.conf

 

第二步:定义nginx.conf文件,其中包含修改启动路径,配置方向代理路径

worker_processes 1;
events{
    worker_connections 1024;
}
http{
    include mime.types;
    default_type application/cotet-stream;
    sendfile on;
    keepalive_timeout 65;
    server {
        listen   80 default;
        server_name  localhost;
        access_log  /var/log/nginx/localhost.access.log;
        location / {
                root   /home/site/wwwroot/;
                index  index.html index.htm;
        }
        location /out {
            proxy_pass https://www.cnblogs.com/lulight/p/15180884.html;
        }
    } 
    
}

注意:

  • 在Server节点中,配置了两种路径处理,当访问的是 / 根目录时,路径修改为  /home/site/wwwroot/, 默认的启动页面时 index.html 或者时 index.htm
  • 当请求路径时 localhost:80/out 时,反向代理请求发送到博客园博文地址  https://www.cnblogs.com/lulight/p/15180884.html

第三步:创建镜像后,Push到ACR中

使用az指令来创建ACR并通过docker登录到ACR中,然后push mynginx镜像到ACR中,为第四步准备。全文参考文档:https://docs.microsoft.com/zh-cn/azure/app-service/tutorial-custom-container?pivots=container-linux#create-a-resource-group

# 1. pull nginx image ... need docker for linux
docker pull nignx

# 2. build image 
docker build -t mynginx:latest .

# 3. run images 
docker run --name mynginxtest3 -p 8081:80 mynginx:v4


# 4. Push to ACR 
az cloud set --name AzureChinaCloud

az login
# az group create 命令创建资源组
az group create --name appacr-rg --location chinanorth2
# az acr create 命令创建 Azure 容器注册表
az acr create --name lbacr01 --resource-group appacr-rg --sku Basic --admin-enabled true
# az acr show 命令以检索注册表的凭据
az acr credential show --resource-group appacr-rg --name lbacr01

# docker login 命令登录到容器注册表
docker login lbacr01.azurecr.cn --username lbacr01

# 为ACR 标记本地 Docker 映像
docker tag mynginx lbacr01.azurecr.cn/mynginx:latest

# docker push 命令将映像推送到为ACR
docker push lbacr01.azurecr.cn/mynginx:latest

#az acr repository list 命令验证推送是否成功
az acr repository list -n lbacr01

# 5. 创建 app service  门户或者是CLI指令

注意:

  • 以上指令中 lbacr01 为测试demo中的ACR名称,实际需要根据情况修改

 上传成功后结果为:

第四步:Azure门户中创建App Service,选择Docker并从ACR中获取镜像

 

注意:整个操作根据Azure门户提示一步一步进行。 https://portal.azure.cn/#create/Microsoft.WebSite, 如要使用AZ命令,则同样参考第三步链接(将应用服务配置为从注册表部署映像:https://docs.microsoft.com/zh-cn/azure/app-service/tutorial-custom-container?pivots=container-linux#configure-app-service-to-deploy-the-image-from-the-registry)

第五步:验证App Service的访问及反向代理结果

 

附录:方案中的静态页面内容

appnginx.html

<html>
    <body>
        <h1>Hello docker + nginx from china azure app service /home/site/wwwroot/ !</h1>
        <h2>Hello docker + nginx  from china azure app service /home/site/wwwroot/ !</h2>
        <h3>Hello docker + nginx   from china azure app service /home/site/wwwroot/ !</h3>
        <h4>Hello docker + nginx    from china azure app service /home/site/wwwroot/ !</h4>
        <h5>Hello docker + nginx     from china azure app service /home/site/wwwroot/ !</h5>
        <h6>Hello docker  + nginx     from china azure app service /home/site/wwwroot/ !</h6> 
    </body>
</html>

update.html

<html>
    <body>
        <h1>update page ....... !</h1>
         
        <h5>China azure app service /home/site/wwwroot/ !</h5> 
    </body>
</html>

参考资料

nginx反向代理配置两个不同服务器https://www.cnblogs.com/momjs/p/10615088.html

使用自定义容器将自定义软件迁移到 Azure 应用服务: https://docs.microsoft.com/zh-cn/azure/app-service/tutorial-custom-container?pivots=container-linux 

当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!

原文地址:https://www.cnblogs.com/lulight/p/15191666.html