【问题标题】:Reverse proxy on windows using docker - nginx is not forwarding https to IIS使用 docker 在 Windows 上进行反向代理 - nginx 未将 https 转发到 IIS
【发布时间】:2019-06-24 22:49:08
【问题描述】:

我有一个 win10 机器,我在其中运行 docker 和两个装有 Windows 的容器,如下图所示,其中一个运行 Nginx 并充当另一个运行 IIS 的容器的反向代理。 它适用于 http,但从 nginx 到 IIS 的重定向对于 https 失败。

各个容器自己接受 https,所以我知道证书安装正确。我使用自签名证书。 我在想nginx.conf 中可能存在一个我不知道的设置。

我可以的

+---------------------------+--------------------------+------+
| https://localhost         | points to nginx          | OK   | 
+---------------------------|--------------------------|------|
| https://localhost:5003    | points to iis            | OK   |
+---------------------------|--------------------------|------|
| https://localhost/mysite  | points to iis via nginx  | FAIL |
+---------------------------+--------------------------+------+

还有错误:

有问题,例如thisthis 但它们仅指 http。 DigitalOcean 上有一个 tutorial 描述了如何使用 https 设置 nginx,我基本上遵循了这一点,但它仍然不起作用。

Nginx - access.log:

"GET /mysite HTTP/1.1" 504 585 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"

Nginx - 错误日志:

*5 upstream timed out (10060: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond) while connecting to upstream, client: 172.18.0.1, server: localhost, request: "GET /mysite HTTP/1.1", upstream: "https://172.18.0.2:5003/", host: "localhost"

IIS 日志:

C:\inetpub\logs 为空

问题

如何让nginx转发https到IIS容器?


设置

设置 docker 网络: docker network create -d nat --subnet=172.18.0.0/16 nginx-proxy-network

构建命令:

cd nginx-proxy
docker build -t nginx-proxy .
Cd ..\iis
Docker build -t iis .

启动 nginx 容器: docker run -d -p 80:80 -p 443:443 --network nginx-proxy-network --ip 172.18.0.3 nginx-proxy

启动 iis 容器: docker run -d -p 5002:80 -p 5003:443 --network nginx-proxy-network --ip 172.18.0.2 iis

Nginx

为 nginx 生成证书1 C:\openssl\openssl.exe genrsa -des3 -out localhost.key 2048

C:\openssl\openssl.exe req -new -key localhost.key -out localhostcsr -config C:\openssl\openssl.conf

C:\openssl\openssl.exe x509 -req -days 365 -in localhost.csr -signkey localhost.key -out localhost.crt

它要求输入密码,然后我将其存储在 txt 文件中。

Nginx.conf:

worker_processes 1;
events {
    worker_connections 1024;
}

http {
    include mime.types;
    default_type application/octet-stream;
    sendfile on;
    keepalive_timeout 65;
    server {
        listen 80;
        server_name localhost ; 

        location /mysite {
            proxy_pass http://172.18.0.2/; 
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
        }
        location / {
            root html;
            index index.html index.htm;
        }
# redirect server error pages to the static page /50x.html
#
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            root html;
        }
}
# HTTPS server
    server {
        listen *:443 ssl;
        server_name localhost ;
        ssl on;
        ssl_password_file C:\cert\pwdcert.txt; 
        ssl_certificate C:\cert\localhost.crt; 
        ssl_certificate_key C:\cert\localhost.key;
        ssl_session_cache shared:SSL:1m;
        ssl_session_timeout 5m;
        ssl_ciphers HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers on;
        add_header Strict-Transport-Security "max-age=63072000;        includeSubdomains; preload";
        add_header X-Frame-Options DENY;
        add_header X-Content-Type-Options nosniff;
        add_header X-Frame-Options "SAMEORIGIN";

        location /mysite {
            proxy_pass https://172.18.0.2:5003/;
            # proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
        }

        location / {
            root html;
            index index.html index.htm;
        }
    }
}

Nginx - Docker 文件:

FROM microsoft/windowsservercore
COPY nginx/ /nginx
RUN mkdir "C:\\cert" 
COPY *.crt /cert
COPY *.key /cert
COPY pwdcert.txt /cert
WORKDIR /nginx
CMD ["nginx"]

IIS

IIS Docker 文件:

FROM microsoft/aspnet
COPY iisscripts.ps1 /
RUN powershell -noexit "C:\iisscripts.ps1"
COPY mysite/ /inetpub/wwwroot/

iisscripts.ps1:

$cert = New-SelfSignedCertificate -DnsName "localhost" -    CertStoreLocation cert:\LocalMachine\My
New-WebBinding -Name "Default Web Site" -IP "*" -Port 443 -Protocol https
new-item -path IIS:\SslBindings\0.0.0.0!443 -Value $cert

【问题讨论】:

    标签: docker ssl nginx iis nginx-reverse-proxy


    【解决方案1】:

    你能从运行 nginx 的容器中 curl IIS URL 吗?

    然后使用 ssh 进入 nginx 容器:-

    https://[my域或IP地址]

    【讨论】:

    • 在 nginx 容器内运行 curl https://localhost/mysite 会得到响应 curl : The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel. 这是什么意思?
    【解决方案2】:

    和我想的一样。由于运行 NGINX 的容器不信任您在 IIS 容器中安装的证书,因此它不会代理连接。您需要通过将以下内容添加到您的 NGINX 配置来告诉 NGINX 不验证 ssl。

    ssl_verify_client 关闭

    或者使用受信任的证书。

    【讨论】:

    • 我将ssl_verify_client off; 添加到服务器块,即server { listen *:443 ssl ; server_name localhost ; ssl_verify_client off; [...] },但它仍然报告Could not establish trust relationship for the SSL/TLS secure channel。我需要使用其他设置来禁用信任关系吗?
    • 如果您现在尝试通过浏览器点击它会发生什么?
    • 我首先得到一个“此站点不安全”,这可能意味着它正在使用 https 访问 nginx。然后我接受该站点不安全,然后我得到一个 nginx 504 Gateway Time-out nginx/1.15.0。不知道该怎么办。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多