Nginx Ingress rewrite-target返回308,服务正常工作。

huangapple go评论69阅读模式
英文:

Nginx Ingress rewrite-target gives 308, service is working

问题

以下是您要翻译的内容:

I have an app set up which can be contacted via the service-IP, but not using the Ingress Rule.

Consider the following Ingress Manifest:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: myapp
  namespace: default
  annotations:
    kubernetes.io/ingress.class: public
    cert-manager.io/cluster-issuer: letsencrypt-prod
    nginx.ingress.kubernetes.io/rewrite-target: /$0
    nginx.ingress.kubernetes.io/force-ssl-redirect: "true"
spec:
  tls:
  - secretName: letsencrypt-prod
    hosts:
    - my.host.net
  rules:
  - host: my.host.net
    http:
      paths:
      - path: /myapp(/|$)(.*)
        pathType: Prefix
        backend:
          service:
            name: myapp
            port:
              number: 1234

The relevant service is up and running. Doing a curl 10.152.183.91/myapp/path/ with 10.152.183.91 being the service IP gives the desired result.

When I go through curl my.host.net/myapp/path/ however, I get a 308 Permanent Redirect. Other apps on the same cluster are running as expected, so the cluster itself as well as nginx-ingress and CoreDNS are doing their job.

Where did I go wrong? Is the nginx.ingress.kubernetes.io/rewrite-target: /$0 wrong?

英文:

I have an app set up which can be contacted via the service-IP, but not using the Ingress Rule.

Consider the following Ingress Manifest:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: myapp
  namespace: default
  annotations:
    kubernetes.io/ingress.class: public
    cert-manager.io/cluster-issuer: letsencrypt-prod
    nginx.ingress.kubernetes.io/rewrite-target: /$0
    nginx.ingress.kubernetes.io/force-ssl-redirect: "true"
spec:
  tls:
  - secretName: letsencrypt-prod
    hosts:
    - my.host.net
  rules:
  - host: my.host.net
    http:
      paths:
      - path: /myapp(/|$)(.*)
        pathType: Prefix
        backend:
          service:
            name: myapp
            port:
              number: 1234

The relevant service is up and running. Doing a curl 10.152.183.91/myapp/path/ with 10.152.183.91 being the service IP gives the desired result.

When I go through curl my.host.net/myapp/path/ however, I get a 308 Permanent Redirect. Other apps on the same cluster are running as expected, so the cluster itself as well as nginx-ingress and CoreDNS are doing their job.

Where did I go wrong? Is the nginx.ingress.kubernetes.io/rewrite-target: /$0 wrong?

答案1

得分: 1

首要的是,您需要更改以下部分:

  • 从:nginx.ingress.kubernetes.io/rewrite-target: /$0
  • 到:nginx.ingress.kubernetes.io/rewrite-target: /$2

解释:

> ## 重写目标
>
> ### 注意
>
> 捕获组 以编号占位符的形式按顺序保存,形式为 $1、$2 ... $n。这些占位符可以用作 rewrite-target 注释中的参数。
>
> 创建带有重写注释的 Ingress 规则:
>
> yaml > apiVersion: networking.k8s.io/v1 > kind: Ingress > metadata: > annotations: > nginx.ingress.kubernetes.io/use-regex: "true" > nginx.ingress.kubernetes.io/rewrite-target: /$2 > name: rewrite > namespace: default > spec: > ingressClassName: nginx > rules: > - host: rewrite.bar.com > http: > paths: > - path: /something(/|$)(.*) > pathType: Prefix > backend: > service: > name: http-svc > port: > number: 80 >
>
> 在此 Ingress 定义中,由 (.*) 捕获的任何字符都将分配给占位符 $2,然后将其用作 rewrite-target 注释中的参数。
>
> 例如,上面的 Ingress 定义将导致以下重写:
>
> * rewrite.bar.com/something 重写为 rewrite.bar.com/
>
> * rewrite.bar.com/something/ 重写为 rewrite.bar.com/
>
> * rewrite.bar.com/something/new 重写为 rewrite.bar.com/new
>
> -- Kubernetes.github.io: Ingress-nginx: Examples: Rewrite


至于 curl 部分。通过运行与您所做的完全相同:

  • curl my.host.net/myapp/path/

您将收到一个 308 永久重定向

默认情况下,curl 不会跟随重定向。尝试使用 curl -L

示例,类似于您的设置:

❯ curl kruklabs.internal/myapp/
<html>
<head><title>308 永久重定向</title></head>
<body>
<center><h1>308 永久重定向</h1></center>
<hr><center>nginx</center>
</body>
</html>

❯ curl kruklabs.internal/myapp/ -L -k
这是 nginx 容器!

> 顺便说一下!
>
> 您还可以尝试直接使用 HTTPSIngress 进行联系。

英文:

First and foremost, you will need to change the:

  • from: nginx.ingress.kubernetes.io/rewrite-target: /$0
  • to: nginx.ingress.kubernetes.io/rewrite-target: /$2

Explanation:

> ## Rewrite Target
>
> ### Note
>
> Captured groups are saved in numbered placeholders, chronologically, in the form $1, $2 ... $n. These placeholders can be used as parameters in the rewrite-target annotation.
>
> Create an Ingress rule with a rewrite annotation:
>
> yaml
> apiVersion: networking.k8s.io/v1
> kind: Ingress
> metadata:
> annotations:
> nginx.ingress.kubernetes.io/use-regex: "true"
> nginx.ingress.kubernetes.io/rewrite-target: /$2
> name: rewrite
> namespace: default
> spec:
> ingressClassName: nginx
> rules:
> - host: rewrite.bar.com
> http:
> paths:
> - path: /something(/|$)(.*)
> pathType: Prefix
> backend:
> service:
> name: http-svc
> port:
> number: 80
>

>
> In this ingress definition, any characters captured by (.*) will be assigned to the placeholder $2, which is then used as a parameter in the rewrite-target annotation.
>
> For example, the ingress definition above will result in the following rewrites:
>
> * rewrite.bar.com/something rewrites to rewrite.bar.com/
> * rewrite.bar.com/something/ rewrites to rewrite.bar.com/
> * rewrite.bar.com/something/new rewrites to rewrite.bar.com/new
>
> -- Kubernetes.github.io: Ingress-nginx: Examples: Rewrite


As for the curl part. By running exactly the same as you did:

  • curl my.host.net/myapp/path/

You will receive a 308 Permanent Redirect.

curl by default is not following the redirection. Try with curl -L.

Example with setup similar to yours:

❯ curl kruklabs.internal/myapp/      
<html>
<head><title>308 Permanent Redirect</title></head>
<body>
<center><h1>308 Permanent Redirect</h1></center>
<hr><center>nginx</center>
</body>
</html>

❯ curl kruklabs.internal/myapp/ -L -k  
Here is the nginx container!

> A side note!
>
> You could also try to contact Ingress directly with HTTPS.

答案2

得分: 0

问题是我根本不需要进行任何重定向,因为我的应用程序已经在端点 /myapp/ 上进行监听。因此,任何请求到达我的服务器的 /myapp/path 都已经具有正确的URL。

当我使用 curl -L 时发现了一个重定向循环(正如 @dawik-kruk 指出的那样)。

英文:

The problem was that I didn't even need to do any redirection, since my app was listening to the endpoint /myapp/ anyways. So any request hitting my server at the /myapp/path already had the correct URL.

I figured this out when curl -L showed a redirect loop (as pointed out by @dawik-kruk)

huangapple
  • 本文由 发表于 2023年2月26日 21:17:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75572241.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定