我有一个 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 |
+---------------------------+--------------------------+------+
错误:
有问题,例如this和 this但它们仅指 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
最佳答案
关于docker - 使用 docker 在 Windows 上进行反向代理 - nginx 不会将 https 转发到 IIS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54461204/