![](/img/trans.png)
[英]kubernetes nginx ingress fails to redirect HTTP to HTTPS
[英]How to debug Kubernetes nginx Ingress redirection from HTTP to HTTPS
快速問題:我如何調試入口和 Nginx 以了解 HTTP->HTTPS 重定向的確切位置?
更多細節:
我們有:我們有戰爭文件 + Tomcat,用 Docker 構建它。 在 AWS 中使用 Kubernetes 運行它。
我們需要的是:應用程序應該可以通過 HTTP 和 HTTPS 訪問。 HTTP不應重定向到 HTTPS。
問題:HTTP 總是重定向到 HTTPS。
我們嘗試什么:我們有 Ingress
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: ${some name
namespace: ${some namespace}
labels:
app: ${some app}
env: ${some env}
annotations:
nginx.ingress.kubernetes.io/ssl-redirect: "false" #we added this for turn off https redirection
nginx.ingress.kubernetes.io/force-ssl-redirect: "false" #we added this for turn off https redirection
nginx.ingress.kubernetes.io/affinity: "cookie" # We use it for sticky sessions
nginx.ingress.kubernetes.io/affinity-mode: "persistent"
nginx.ingress.kubernetes.io/session-cookie-name: "some cookie name"
nginx.ingress.kubernetes.io/session-cookie-expires: "172800"
nginx.ingress.kubernetes.io/session-cookie-max-age: "172800"
nginx.ingress.kubernetes.io/rewrite-target: /
nginx.ingress.kubernetes.io/whitelist-source-range: ${whitelist of ip adresses}
spec:
tls:
- hosts:
- ${some host}
- ${some another host}
secretName: my-ingress-ssl
rules:
- host: ${some host}
http:
paths:
- path: /
backend:
serviceName: ${some another service name}
servicePort: 8080
- host: ${some another host}
http:
paths:
- path: /
backend:
serviceName: ${some another service name}
servicePort: 8080
和配置圖
kind: ConfigMap
apiVersion: v1
metadata:
labels:
app: ${some app}
env: ${some env}
namespace: ${some namespace}
name: nginx-config
data:
hsts: "false" #we added this for turn off https redirection
hsts-max-age: "0" #we added this for turn off https redirection
ssl-redirect: "false" #we added this for turn off https redirection
hsts-include-subdomains: "false" #we added this for turn off https redirection
在 Tomcat server.xml 我們有:
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
...
<!-- Define an AJP 1.3 Connector on port 8009 -->
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
...
我們評論了這個連接器(它現在不應該工作):
<!--
<Connector port="8443" protocol="org.apache.coyote.http11.Http11AprProtocol"
maxThreads="150" SSLEnabled="true" >
<UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" />
<SSLHostConfig>
<Certificate certificateKeyFile="conf/key.pem"
certificateFile="conf/cert.pem"
certificateChainFile="conf/chain.pem"
type="RSA" />
</SSLHostConfig>
</Connector>
-->
我嘗試了所有可能的帶有入口注釋的變體,但沒有成功的結果。
我想知道的是:如何使用 Nginx調試入口以了解 HTTP->HTTPS 重定向的確切位置?
UPD
事實證明,安裝在服務器上的不是 Nginx controller,而是 Traefik。 由於安全限制,我無法看到帶有 controller 的 pod。 所以沒有 Nginx 注釋起作用。 盡管如此,下面對我的問題的回答將幫助有類似問題的人。
在您描述的設置中,處理傳入的 http 請求可能涉及至少三個進程
互聯網 -> AWS ELB -> Nginx 入口 Controller -> Tomcat
任何進程都可以發出 301 重定向以指示 http 流量使用 https 重試。 tomcat 進程中的重定向可以由 tomcat 配置或應用程序 tomcat 主機指定。
我會嘗試通過嘗試繞過更高的進程來隔離執行重定向的進程:
如果由於 ssh 限制、curl 可用性等原因,這一切都不可能實現……另一種方法是為所涉及的每個進程打開請求日志記錄——盡管這不適用於生產工作負載,因為它會導致機密信息是 output 到不適當的上下文,例如 web 層日志文件或 cloudwatch 日志。
您可以查看當前 nginx-ingress 清單的 nginx 配置並檢查 http 到https的任何規則,如下所述:
kubectl exec -it -n <namespace-of-ingress-controller> <nginx-ingress-controller-pod-name> -- cat /etc/nginx/nginx.conf
使用命令curl -LIk http://<domain>
並檢查server:
header 進行重定向。 在 curl 中:
-L
將遵循重定向-I
只顯示標題-k
將允許不安全的 SSL 請求
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.