Kubernetes入口重写 [英] Kubernetes ingress rewrite

查看:109
本文介绍了Kubernetes入口重写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在将PHP整体移植到Kubernetes中,并且在nginx-ingress中重写URL时遇到了一些麻烦.

I'm currently porting a PHP monolith into Kubernetes, and I'm having some trouble with the URL rewriting in nginx-ingress.

系统使用 phroute ,并且一切正常.问题是要设置nginx入口以使其运行良好.

The system uses phroute and that's working all fine. The problem is setting up the nginx ingress to play nice.

旧系统具有以下重写规则:

The old system had the following rewrite rules:

try_files $uri $uri/ @rewrite;

location @rewrite {
  rewrite ^/(.*)$ /index.php?_url=/$1;
}

location / {
  try_files $uri $uri/ =404;
}

location ~ \.php$ {
  try_files $uri =404;
  fastcgi_split_path_info ^(.+\.php)(/.+)$;
  fastcgi_pass 1.2.3.4:9000;
  fastcgi_index index.php;
  include fastcgi_params;
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  fastcgi_param PATH_INFO $fastcgi_path_info;
}

location ~* \.(js|css|png|jpg|jpeg|gif|svg)$ {
  expires max;
  log_not_found off;
  rewrite "/[a-z0-9]{40}/([^.]+)\.(js|css|png|jpg|jpeg|gif|svg)$" /assets/$1.$2;
}

我为nginx入口尝试了以下注释:

I've tried the following annotations for my nginx ingress:

ingress:
  enabled: true
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /index.php?_url=/$1
  hosts:
    - site.kube
  paths: [
    /(.*),
    /[a-z0-9]{40}/([^.]+)\.(js|css|png|jpg|jpeg|gif|svg)$
  ]

以下是我的问题:

  • phroute不使用$_GET['_url']参数,而是使用$_SERVER['REQUEST_URI'].如何设置?如果将rewrite-target设置为/$1,则路由有效,但是:

  • phroute doesn't use the $_GET['_url'] parameter, but rather $_SERVER['REQUEST_URI']. How do I set it? If I set the rewrite-target to /$1, the routes work, but:

如何实现资产路径?

请告诉我是否可以提供更多信息.谢谢.

Please tell me if I can provide any more information. Thanks.

修改

为回应评论中的问题,以下是一些版本输出.

In response to questions in comments, here's some version output.

Kubernetes(在撰写本文时,最新的Minikube):

Kubernetes (latest Minikube, at the time of writing):

$ kubectl version
Client Version: version.Info{Major:"1", Minor:"16", GitVersion:"v1.16.3", GitCommit:"b3cbbae08ec52a7fc73d334838e18d17e8512749", GitTreeState:"clean", BuildDate:"2019-11-14T04:24:29Z", GoVersion:"go1.12.13", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"16", GitVersion:"v1.16.2", GitCommit:"c97fe5036ef3df2967d086711e6c0c405941e14b", GitTreeState:"clean", BuildDate:"2019-10-15T19:09:08Z", GoVersion:"go1.12.10", Compiler:"gc", Platform:"linux/amd64"}

nginx-ingress(内置到minikube中):

$ kubectl describe pod nginx-ingress-controller-6fc5bcc8c9-qnmz2 -n kube-system
{
    [ ... ]
    Image:         quay.io/kubernetes-ingress-controller/nginx-ingress-controller:0.26.1,
    [ ... ]
}

推荐答案

我找到了解决方案!

原因是未处理根server { ... }块的try_files.这与以下行有关,以便将请求从nginx ingress正确传播到我的部署中的nginx pod中,该行作为入口注释是必需的:

The reason was that the root server { ... }-block's try_files isn't handled. This has to do with the following line being required to properly propagate the request from the nginx ingress to the nginx pod in my deployment, this line is required as an ingress annotation:

nginx.ingress.kubernetes.io/rewrite-target: /$1

要使其正常工作,我要做的只是以下操作:

To get it to work, all I did was the following:

  1. 在我的Nginx配置的配置映射中,我从server { ... } -block中删除了try_files.

  1. In my config map for the nginx config, I removed the try_files from the server { ... }-block.

location / { ... }块中,我将try_files更改为:

try_files $uri $uri/ =404;

收件人:

try_files $uri $uri/ @rewrite;

这解决了问题,因为rewrite-target使得所有传入nginx的请求最终都在location / { ... } -block中,从而绕过了服务器的try_files.

This solved the problem, because the rewrite-target made all requests incoming to nginx end up in the location / { ... }-block and thus bypass the server's try_files.

这篇关于Kubernetes入口重写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆