docker - 使用 docker 在 Windows 上进行反向代理 - nginx 不会将 https 转发到 IIS

标签 docker ssl nginx iis nginx-reverse-proxy

我有一个 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 |
+---------------------------+--------------------------+------+

enter image description here

错误:

enter image description here

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

Nginx - 访问日志:

"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 didn't properly response after a period time, or established connection failed because connected host has failed respond) while connecting to upstream, 客户端:172.18.0.1,服务器:localhost,请求:“GET/mysite HTTP/1.1”,上游:“https://172.18.0.2:5003/”,主机:“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

最佳答案

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

然后使用 ssh 执行到 nginx 容器中:-

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

关于docker - 使用 docker 在 Windows 上进行反向代理 - nginx 不会将 https 转发到 IIS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54461204/

相关文章:

web-services - 在 Web 应用程序中使用受 SSL 保护的 Web 服务

amazon-web-services - Api Gateway 与 Elastic Beanstalk 的连接(客户端 SSL 证书)

nginx - K8S群集中的RemoteIpAddress始终是入口IP

Nginx:无法访问该站点;权限配置?

java - 如何将 Docker 添加到我的模块化 Web 应用程序

docker - 如何配置github操作以将Docker容器部署到AWS ElasticBeanstalk多容器环境

python - 如何使用 Python 通过 SSL 使用 SMTP 连接到邮件服务器?

使用共享 SSL 的 Facebook 页面选项卡

java - 如何在测试容器上使用docker-compose.yml时解决安装问题

docker - Docker neo4j容器刚刚挂起