重定向到docker容器内Nginx上的HTTPS

发布于 2025-02-10 20:58:51 字数 1876 浏览 4 评论 0原文

我正在尝试在https上的docker容器内的nginx上运行Blazor Wasm应用。当我在浏览器中键入https:// localhost:8021(8021由端口443映射)时,该站点会正确加载。我还想从http:// localhost:8020(8020映射为80)重定向到以前的https地址。但是,无论我如何配置我的nginx.conf,重定向都将变为完全奇怪的url https:// 2f81239fe704/。谁能看到我在这里做错了什么?

nginx.conf

events { }

http {
    include mime.types;

    server {
        listen 80;
        listen 443 ssl;
        
        server_name _;
        
        if ($scheme = http) {
            return 301 https://$host$request_uri;
            # except $host I also tried localhost:8021/443, 127.0.0.1:8021:443,
            # host.docker.internal:8021/443, nothing seems to work

        }
        
        ssl_certificate /https/localhost.crt;
        ssl_certificate_key /https/localhost.rsa;
        
        location / {
            root /usr/share/nginx/html;
            try_files $uri $uri/ /index.html =404;
        }
    }
}

dockerfile:

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src

# COPY FILES OMITTED

RUN dotnet restore "Solution.sln"

COPY . .

WORKDIR "src/Admin/"
RUN dotnet build "Admin.csproj" -c Debug -o /app/build

FROM build AS publish
RUN dotnet publish Admin.csproj -c Debug -o /app/publish

FROM nginx:alpine AS final
EXPOSE 80
EXPOSE 443
WORKDIR /usr/share/nginx/html
COPY --from=publish /app/publish/wwwroot .
COPY src/Admin/nginx.conf /etc/nginx/nginx.conf

docker-compose.yml


  admin:
    image: admin
    build:
      context: .
      dockerfile: src/Admin/Dockerfile
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - ASPNETCORE_URLS=https://+;http://+
    ports:
      - "8020:80"
      - "8021:443"
    volumes:
      - ${APPDATA}/Microsoft/UserSecrets:/root/.microsoft/usersecrets:ro
      - ${APPDATA}/ASP.NET/Https:/root/.aspnet/https:ro
      - ~/.aspnet/https:/https:ro

I'm trying to run Blazor WASM app on NGINX inside Docker Container on https. When I type https://localhost:8021 in browser (8021 is mapped from port 443), the site correctly loads. I also want to redirect from http://localhost:8020 (8020 is mapped from port 80) to previous https address. But no matter how I configure my nginx.conf, the redirect goes to completely weird URL https://2f81239fe704/. Can anyone see what am I doing wrong here?

nginx.conf

events { }

http {
    include mime.types;

    server {
        listen 80;
        listen 443 ssl;
        
        server_name _;
        
        if ($scheme = http) {
            return 301 https://$host$request_uri;
            # except $host I also tried localhost:8021/443, 127.0.0.1:8021:443,
            # host.docker.internal:8021/443, nothing seems to work

        }
        
        ssl_certificate /https/localhost.crt;
        ssl_certificate_key /https/localhost.rsa;
        
        location / {
            root /usr/share/nginx/html;
            try_files $uri $uri/ /index.html =404;
        }
    }
}

Dockerfile:

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src

# COPY FILES OMITTED

RUN dotnet restore "Solution.sln"

COPY . .

WORKDIR "src/Admin/"
RUN dotnet build "Admin.csproj" -c Debug -o /app/build

FROM build AS publish
RUN dotnet publish Admin.csproj -c Debug -o /app/publish

FROM nginx:alpine AS final
EXPOSE 80
EXPOSE 443
WORKDIR /usr/share/nginx/html
COPY --from=publish /app/publish/wwwroot .
COPY src/Admin/nginx.conf /etc/nginx/nginx.conf

docker-compose.yml


  admin:
    image: admin
    build:
      context: .
      dockerfile: src/Admin/Dockerfile
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - ASPNETCORE_URLS=https://+;http://+
    ports:
      - "8020:80"
      - "8021:443"
    volumes:
      - ${APPDATA}/Microsoft/UserSecrets:/root/.microsoft/usersecrets:ro
      - ${APPDATA}/ASP.NET/Https:/root/.aspnet/https:ro
      - ~/.aspnet/https:/https:ro

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

独行侠 2025-02-17 20:58:52

而不是一个,在nginx.conf中创建两个diff对象,

一个用于端口80,另一个用于443。

示例:

server {
    listen       80;

    server_name  _;

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

server {
    listen       443 ssl;

    server_name  _;

    ssl_certificate /https/localhost.crt;
    ssl_certificate_key /https/localhost.rsa;

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

Instead of one, create two diff object in nginx.conf

One for port 80 and other for 443.

Example:

server {
    listen       80;

    server_name  _;

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

server {
    listen       443 ssl;

    server_name  _;

    ssl_certificate /https/localhost.crt;
    ssl_certificate_key /https/localhost.rsa;

    location / {
        root /usr/share/nginx/html;
        try_files $uri $uri/ /index.html =404;
    }
}
终陌 2025-02-17 20:58:52

我修改了您的文件,您需要指定您的域$ server_name变量

events { }

http {
    include mime.types;

    server {
        listen 80;
        listen 443 ssl;
        
        server_name www.foo.bar;
        
        if ($scheme = http) {
            return 301 https://$server_name$request_uri;
            # except $host I also tried localhost:8021/443, 127.0.0.1:8021:443,
            # host.docker.internal:8021/443, nothing seems to work

        }
        
        ssl_certificate /https/localhost.crt;
        ssl_certificate_key /https/localhost.rsa;
        
        location / {
            root /usr/share/nginx/html;
            try_files $uri $uri/ /index.html =404;
        }
    }
}

I modified your file you need to specify your domain a $server_name variable

events { }

http {
    include mime.types;

    server {
        listen 80;
        listen 443 ssl;
        
        server_name www.foo.bar;
        
        if ($scheme = http) {
            return 301 https://$server_name$request_uri;
            # except $host I also tried localhost:8021/443, 127.0.0.1:8021:443,
            # host.docker.internal:8021/443, nothing seems to work

        }
        
        ssl_certificate /https/localhost.crt;
        ssl_certificate_key /https/localhost.rsa;
        
        location / {
            root /usr/share/nginx/html;
            try_files $uri $uri/ /index.html =404;
        }
    }
}
云裳 2025-02-17 20:58:52

我设法找到了解决方案。这是浏览器,我猜是对https:// 2f81239fe704/(是docker容器hostname)和我的任何nginx.conf更改的第一个不正确的重定向响应。清洁cookie和网站数据后,更改为nginx.conf实际上会产生效果。最后,我最终得到了:

server_name localhost;
        
if ($scheme = http) {
    return 301 https://localhost:8021$request_uri;
}

I managed to find the solution. It was the browser, which I guess, cached my first, incorrect redirection response to https://2f81239fe704/ (which was docker container hostname) and any of my nginx.conf change didn't really had any effect. After cleaning cookies and site data, changes to nginx.conf actually make effects. Finally, I ended up with:

server_name localhost;
        
if ($scheme = http) {
    return 301 https://localhost:8021$request_uri;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文