【问题标题】:Why is nginx redirect not working with Docker container?为什么 nginx 重定向不适用于 Docker 容器?
【发布时间】:2018-10-10 10:24:44
【问题描述】:

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

我正在尝试使用 ngnix 执行此操作,但是我使用的设置不起作用。我创建了一个新的 Core 2.0 Web App 来尝试让它在最简单的环境中运行。除了 Web App,我还有我的 Dockerfile:

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,但是不会发生重定向。

谁能看到我做错了什么?任何帮助将不胜感激。

【问题讨论】:

  • 尝试:docker run -it - -net=host redirectservice /bin/bash 然后,尝试使用 curl 访问 google.com。或者,尝试定位 nginx 日志,看看是什么错误。

标签: docker nginx http-redirect


【解决方案1】:

它没有重定向你,因为 nginx 进程没有运行。 看一下 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 并等待它们结束(即崩溃)。

【讨论】:

  • 谢谢你,让我很清楚我哪里出错了。最后,我最终在我的 swarm 中为 nginx 提供了一个单独的服务。
  • 正确,如果您使用 swarm 单独服务的方法是正确的方法:)
猜你喜欢
  • 2021-12-27
  • 2018-07-05
  • 1970-01-01
  • 2019-03-11
  • 2017-05-07
  • 1970-01-01
  • 2019-07-30
  • 1970-01-01
  • 2020-12-10
相关资源
最近更新 更多