Ingress Nginx - 如何为应用程序提供资产 [英] Ingress Nginx - how to serve assets to application
问题描述
我有一个问题,我正在 [主机名]/产品/控制台上部署一个应用程序,但是 .css .js 文件是从 [主机名]/product/static 请求的,因此它们没有被加载,我得到404.
I have an issue, I am deploying an application on [hostname]/product/console, but the .css .js files are being requested from [hostname]/product/static, hence they are not being loaded and I get 404.
我尝试过 nginx.ingress.kubernetes.io/rewrite-target:
无济于事.
I have tried nginx.ingress.kubernetes.io/rewrite-target:
to no avail.
我也试过使用:nginx.ingress.kubernetes.io/location-snippet: |位置 =/产品/控制台/{proxy_pass http://[主机名]/product/static/;}
但后者似乎根本没有被nginx控制器接收到.这是我的 ingress.yaml
But the latter does not seem to be picked up by the nginx controller at all. This is my ingress.yaml
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: ingress-resource
annotations:
kubernetes.io/ingress.class: "nginx"
nginx.ingress.kubernetes.io/enable-rewrite-log: "true"
# nginx.ingress.kubernetes.io/rewrite-target: /$1
nginx.ingress.kubernetes.io/location-snippet: |
location = /product/console/ {
proxy_pass http://[hostname]/product/static/;
}
spec:
rules:
- host: {{.Values.HOSTNAME}}
http:
paths:
- path: /product/console
backend:
serviceName: product-svc
servicePort: prod ##25022
- path: /product/
backend:
serviceName: product-svc
servicePort: prod #25022
--我可以要求一些指点吗?我一直在尝试谷歌搜索并尝试了一些不同的变体,但我似乎做错了什么.谢谢!
-- Can I ask for some pointers? I have been trying to google this out and tried some different variations, but I seem to be doing something wrong. Thanks!
推荐答案
TL;DR
要诊断错误 404 的原因,您可以检查 nginx-ingress
控制器 pod 日志.您可以使用以下命令执行此操作:
To diagnose the reason why you get error 404 you can check in nginx-ingress
controller pod logs. You can do it with below command:
kubectl logs -n ingress-nginx INGRESS_NGINX_CONTROLLER_POD_NAME
您应该得到与此类似的输出(取决于您的用例):
You should get output similar to this (depending on your use case):
CLIENT_IP - - [12/May/2020:11:06:56 +0000] "GET / HTTP/1.1" 200 238 "-" "REDACTED" 430 0.003 [default-ubuntu-service-ubuntu-port] [] 10.48.0.13:8080 276 0.003 200
CLIENT_IP - - [12/May/2020:11:06:56 +0000] "GET /assets/styles/style.css HTTP/1.1" 200 22 "http://SERVER_IP/" "REDACTED" 348 0.002 [default-ubuntu-service-ubuntu-port] [] 10.48.0.13:8080 22 0.002 200
通过上面的日志,您可以检查请求是否由 nginx-ingress
控制器正确处理以及它们发送到哪里.
With above logs you can check if the requests are handled properly by nginx-ingress
controller and where they are sent.
你也可以查看Kubernetes.github.io:ingress-nginx:入口路径匹配.这是一个描述 Ingress
如何用正则表达式匹配路径的文档.
Also you can check the Kubernetes.github.io: ingress-nginx: Ingress-path-matching. It's a document describing how Ingress
matches paths with regular expressions.
您可以使用 Ingress
进行实验,如下例所示:
You can experiment with Ingress
, by following below example:
- 部署
nginx-ingress
控制器 - 创建一个
pod
和一个service
- 运行示例应用程序
- 创建一个
Ingress
资源 - 测试
- 重写示例
您可以通过以下官方文档部署您的 nginx-ingress
控制器:
You can deploy your nginx-ingress
controller by following official documentation:
Kubernetes.github.io: Ingress-nginx
以下是用于测试目的的 pod 和附加服务的示例定义:
Below is an example definition of a pod and a service attached to it which will be used for testing purposes:
apiVersion: apps/v1
kind: Deployment
metadata:
name: ubuntu-deployment
spec:
selector:
matchLabels:
app: ubuntu
replicas: 1
template:
metadata:
labels:
app: ubuntu
spec:
containers:
- name: ubuntu
image: ubuntu
command:
- sleep
- "infinity"
---
apiVersion: v1
kind: Service
metadata:
name: ubuntu-service
spec:
selector:
app: ubuntu
ports:
- name: ubuntu-port
port: 8080
targetPort: 8080
nodePort: 30080
type: NodePort
示例页面
我用一个 css
创建了一个基本的 index.html
来模拟请求过程.您需要在 pod 内创建此文件(手动或将它们复制到 pod).
Example page
I created a basic index.html
with one css
to simulate the request process. You need to create this files inside of a pod (manually or copy them to pod).
文件树如下所示:
- index.html
- assets/styles/style.css
index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="assets/styles/style.css">
<title>Document</title>
</head>
<body>
<h1>Hi</h1>
</body>
请具体看一行:
<link rel="stylesheet" href="assets/styles/style.css">
style.css:
h1 {
color: red;
}
您可以使用 python
在页面上方运行:
You can run above page with python
:
$ apt update &&apt install -y python3
$ python3 -m http.server 8080
存储index.html
和assets
文件夹的位置.
$ apt update && apt install -y python3
$ python3 -m http.server 8080
where theindex.html
andassets
folder is stored.
以下是配置为使用 nginx-ingress
控制器的 Ingress
资源示例:
Below is an example Ingress
resource configured to use nginx-ingress
controller:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: nginx-ingress-example
annotations:
kubernetes.io/ingress.class: nginx
spec:
rules:
- host:
http:
paths:
- path: /
backend:
serviceName: ubuntu-service
servicePort: ubuntu-port
应用上述资源后,您就可以开始测试了.
After applying above resource you can start to test.
您可以转到浏览器并输入与您的 Ingress
资源关联的外部 IP 地址.
You can go to your browser and enter the external IP address associated with your Ingress
resource.
正如我上面所说的,您可以检查 nginx-ingress
控制器 pod 的日志以检查您的控制器如何处理请求.
As I said above you can check the logs of nginx-ingress
controller pod to check how your controller is handling request.
如果你运行前面提到的命令 python3 -m http.server 8080
你也会得到日志:
If you run command mentioned earlier python3 -m http.server 8080
you will get logs too:
$ python3 -m http.server 8080
Serving HTTP on 0.0.0.0 port 8080 (http://0.0.0.0:8080/) ...
10.48.0.16 - - [12/May/2020 11:06:56] "GET / HTTP/1.1" 200 -
10.48.0.16 - - [12/May/2020 11:06:56] "GET /assets/styles/style.css HTTP/1.1" 200 -
重写示例
我编辑了 Ingress
资源以向您展示路径重写的示例:
Rewrite example
I've edited the Ingress
resource to show you an example of a path rewrite:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: nginx-ingress-example
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
rules:
- host:
http:
paths:
- path: /product/(.*)
backend:
serviceName: ubuntu-service
servicePort: ubuntu-port
对行进行了更改:
nginx.ingress.kubernetes.io/rewrite-target: /$1
和:
- path: /product/(.*)
步骤:
- 浏览器发送:
/product/
- Controller 得到
/product/
并将其重写为/
- Pod 从控制器获得
/
.
- The browser sent:
/product/
- Controller got
/product/
and had it rewritten to/
- Pod got
/
from a controller.
来自nginx-ingress
控制器的日志:
CLIENT_IP - - [12/May/2020:11:33:23 +0000] "GET /product/ HTTP/1.1" 200 228 "-" "REDACTED" 438 0.002 [default-ubuntu-service-ubuntu-port] [] 10.48.0.13:8080 276 0.001 200 fb0d95e7253335fc82cc84f70348683a
CLIENT_IP - - [12/May/2020:11:33:23 +0000] "GET /product/assets/styles/style.css HTTP/1.1" 200 22 "http://SERVER_IP/product/" "REDACTED" 364 0.002 [default-ubuntu-service-ubuntu-port] [] 10.48.0.13:8080 22 0.002 200
来自 Pod 的日志:
Logs from the pod:
10.48.0.16 - - [12/May/2020 11:33:23] "GET / HTTP/1.1" 200 -
10.48.0.16 - - [12/May/2020 11:33:23] "GET /assets/styles/style.css HTTP/1.1" 200 -
如果您对此有任何疑问,请告诉我.
Please let me know if you have any questions in that.
这篇关于Ingress Nginx - 如何为应用程序提供资产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!