重定向到docker容器内Nginx上的HTTPS
我正在尝试在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
而不是一个,在nginx.conf中创建两个diff对象,
一个用于端口80,另一个用于443。
示例:
Instead of one, create two diff object in nginx.conf
One for port 80 and other for 443.
Example:
我修改了您的文件,您需要指定您的域$ server_name变量
I modified your file you need to specify your domain a $server_name variable
我设法找到了解决方案。这是浏览器,我猜是对https:// 2f81239fe704/(是docker容器hostname)和我的任何nginx.conf更改的第一个不正确的重定向响应。清洁cookie和网站数据后,更改为nginx.conf实际上会产生效果。最后,我最终得到了:
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: