Ingress Nginx-如何为应用程序提供资产 [英] Ingress Nginx - how to serve assets to application

查看:94
本文介绍了Ingress Nginx-如何为应用程序提供资产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我正在[主机名]/product/console上部署应用程序,但是从[主机名]/product/static请求了.css .js文件,因此没有加载它们,我得到了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:无济于事.

我也尝试使用:nginx.ingress.kubernetes.io/location-snippet: | location = /product/console/ { proxy_pass http://[hostname]/product/static/; }

I also tried using: nginx.ingress.kubernetes.io/location-snippet: | location = /product/console/ { proxy_pass http://[hostname]/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控制器容器日志.您可以使用以下命令进行操作:

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控制器
  • 创建podservice
  • 运行示例应用程序
  • 创建Ingress资源
  • 测试
  • 重写示例
  • Deploy nginx-ingress controller
  • Create a pod and a service
  • Run example application
  • Create an Ingress resource
  • Test
  • Rewrite example

您可以按照以下官方文档部署nginx-ingress控制器:

You can deploy your nginx-ingress controller by following official documentation:

Kubernetes.github.io:Ingress-nginx

下面是一个pod的示例定义,以及附加到该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
  • 资产/样式/style.css
  • 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.htmlassets文件夹的位置.
  • $ apt update && apt install -y python3
  • $ python3 -m http.server 8080 where the index.html and assets 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控制器容器的日志,以检查控制器如何处理请求.

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/
  • 控制器得到/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 

吊舱中的日志:

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屋!

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