我正在嘗試在 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
碼頭工人-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
uj5u.com熱心網友回復:
在 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;
}
}
uj5u.com熱心網友回復:
我修改了你的檔案,你需要為你的域指定一個 $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;
}
}
}
uj5u.com熱心網友回復:
我設法找到了解決方案。我猜是瀏覽器快取了我對 https://2f81239fe704/ (這是 docker 容器主機名)的第一個不正確的重定向回應,并且我的任何 nginx.conf 更改都沒有真正產生任何影響。清理 cookie 和站點資料后,對 nginx.conf 的更改實際上會生效。最后,我結束了:
server_name localhost;
if ($scheme = http) {
return 301 https://localhost:8021$request_uri;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/495824.html
