You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular与.NET Core容器HTTPS部署故障排查求助

问题:.NET Core后端容器启动失败+Nginx前端HTTPS配置无效

我有两个容器:一个是.NET Core开发的后端,另一个是Angular开发、通过Nginx转发的前端。原本二者可正常通过HTTP访问,现将配置修改为HTTPS后遇到以下问题:

  1. 后端容器启动失败
  2. 更新Nginx配置与证书后,仍无法通过HTTPS访问前端页面

相关配置文件

.NET Core Dockerfile

# Get Base .NET Core SDK Image from Microsoft
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build-env
WORKDIR /source

# Copy the CSPROJ file and restore and dependencies
COPY *.sln .
COPY IQC_Database_Management_API/*.csproj ./IQC_Database_Management_API/
RUN dotnet restore

# Copy the project files and build release
COPY IQC_Database_Management_API/. ./IQC_Database_Management_API/
RUN dotnet publish -c Release -o out

# Generate runtime image
FROM mcr.microsoft.com/dotnet/sdk:7.0
WORKDIR /source
EXPOSE 5001
ENV ASPNETCORE_URLS=https://+:5001
ENV ASPNETCORE_ENVIRONMENT=Docker
COPY --from=build-env /source/out .
ENTRYPOINT [ "dotnet","IQC_Database_Management_API.dll" ]

前端Dockerfile

# Stage 1: Build the Angular application
FROM node:latest AS build
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm install
COPY . .
RUN npm run build --prod

# Stage 2: Serve the application with Nginx
FROM nginx:latest
COPY --from=build /app/dist/ESI_IQCDatabase /usr/share/nginx/html
COPY nginx.conf /etc/nginx/nginx.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

初始nginx.conf

worker_processes 1;

events {
    worker_connections 1024;
}

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

    sendfile        on;
    keepalive_timeout  65;

    server {
        listen 80;
        server_name localhost;

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

        location /api/ {
            proxy_pass   https://iqc-api-container2:5001/api/;
        }

        error_page 404 /404.html;
        location = /404.html {
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }
}

.NET Core launchSettings.json

{
  "profiles": {
    "http": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "swagger",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "dotnetRunMessages": true,
      "applicationUrl": "http://localhost:5069"
    },
    "https": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "swagger",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "dotnetRunMessages": true,
      "applicationUrl": "https://localhost:7213;http://localhost:5069"
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "swagger",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "Docker": {
      "commandName": "Docker",
      "launchBrowser": true,
      "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Docker"
      },
      "publishAllPorts": true,
      "useSSL": true,
      "httpPort": 5000,
      "sslPort": 5001
    }
  },
  "$schema": "https://json.schemastore.org/launchsettings.json",
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:56246",
      "sslPort": 44369
    }
  }
}

问题详情

1. 后端容器启动失败

在远程服务器执行以下命令启动后端容器时,容器启动失败:

sudo docker run -d -v /home/qq/IQC_Database.db:/source/IQC_Database.db -p 7213:5001 --name iqc-api-container2 iqc-api2

2. HTTPS访问前端失败(更新后)

修改nginx.conf为以下内容:

worker_processes 1;

events {
    worker_connections 1024;
}

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

    sendfile        on;
    keepalive_timeout  65;

    server {
        listen 80;
        server_name azeu1-eng-leon1.company.com;

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

        listen 443 ssl;
        ssl_certificate /etc/nginx/certs/server.crt;
        ssl_certificate_key /etc/nginx/certs/server.key;

        error_page 404 /404.html;
        location = /404.html {
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }
}

执行以下命令生成证书:

openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout certs/server.key -out certs/server.crt -subj "/CN=azeu1-eng-leon1.company.com"

将server.crt和server.key移动到容器的/etc/nginx/certs目录,重启容器后仍无法通过HTTPS访问页面。


内容的提问来源于stack exchange,提问作者AoLiGei

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 02:27:06