为什么 nginx 重定向不适用于 Docker 容器?

Why is nginx redirect not working with Docker container?

我想在用户转到 https 到我网站的 http 版本时重定向他们,该网站托管在 Docker 群中。

我正在尝试使用 ngnix 执行此操作,但是我使用的设置不起作用。我已经创建了一个新的 Core 2.0 Web App 来尝试让它在尽可能简单的环境中工作。除了 Web App,我还有我的 Docker 文件:

FROM microsoft/aspnetcore-build:2.0 AS build-env
WORKDIR /app

# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore

# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out

# Build nginx image to redirect http to https
FROM nginx:alpine

EXPOSE 80
COPY nginx.conf /etc/nginx/nginx.conf

# Build runtime image
FROM microsoft/aspnetcore:2.0
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "RedirectService.dll"]

和我的 nginx 文件:

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name _;
    return 301 https://www.google.co.uk;
}

建立我的形象后,我 运行 它与 docker run -p 8006:80 redirectservice。我期望发生的是当我导航到 http://localhost:8006 时它会将我重定向到 Google,但是没有重定向发生。

有人能看出我做错了什么吗?任何帮助将不胜感激。

不是给你重定向,因为nginx进程不是运行ning。 查看 nginx 镜像 Dockerfile (https://github.com/nginxinc/docker-nginx/blob/590f9ba27d6d11da346440682891bee6694245f5/mainline/alpine/Dockerfile) - 最后一行是:

CMD ["nginx", "-g", "daemon off;"]

在您的 Dockerfile 中,您将其替换为:

ENTRYPOINT ["dotnet", "RedirectService.dll"]

因此 nginx 从未启动。

您需要创建一个 sh 脚本,您将在其中 运行 nginx 和 dotnet 并等待它们都结束(即崩溃)。