Home > Software design >  Redirect to https on nginx inside Docker container
Redirect to https on nginx inside Docker container

Time:06-25

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

CodePudding user response:

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;
    }
}

CodePudding user response:

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;
        }
    }
}

CodePudding user response:

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;
}
  • Related