一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

云服務器|WEB服務器|FTP服務器|郵件服務器|虛擬主機|服務器安全|DNS服務器|服務器知識|Nginx|IIS|Tomcat|

服務器之家 - 服務器技術 - 服務器知識 - APISIX Ingress 高級使用之 Url Rewrite

APISIX Ingress 高級使用之 Url Rewrite

2022-01-12 23:42k8s技術圈陽明 服務器知識

前面我們了解了 APISIX Ingress 的基本使用,同樣我們來介紹下如何使用 APISIX 來實現 URL Rewrite 操作,還是以前面測試用過的 Nexus 應用為例進行說明,通過 ApisixRoute 對象來配置服務路由.

APISIX Ingress 高級使用之 Url Rewrite

前面我們了解了 APISIX Ingress 的基本使用,同樣我們來介紹下如何使用 APISIX 來實現 URL Rewrite 操作,還是以前面測試用過的 Nexus 應用為例進行說明,通過 ApisixRoute 對象來配置服務路由,對應的資源清單如下所示:

  1. # nexus.yaml 
  2. apiVersion: apps/v1 
  3. kind: Deployment 
  4. metadata: 
  5.   name: nexus 
  6.   labels: 
  7.     app: nexus 
  8. spec: 
  9.   selector: 
  10.     matchLabels: 
  11.       app: nexus 
  12.   template: 
  13.     metadata: 
  14.       labels: 
  15.         app: nexus 
  16.     spec: 
  17.       containers: 
  18.       - image: cnych/nexus:3.20.1 
  19.         imagePullPolicy: IfNotPresent 
  20.         name: nexus 
  21.         ports: 
  22.         - containerPort: 8081 
  23. --- 
  24. apiVersion: v1 
  25. kind: Service 
  26. metadata: 
  27.   labels: 
  28.     app: nexus 
  29.   name: nexus 
  30. spec: 
  31.   ports: 
  32.   - name: nexusport 
  33.     port: 8081 
  34.     targetPort: 8081 
  35.   selector: 
  36.     app: nexus 
  37. --- 
  38. apiVersion: apisix.apache.org/v2beta2 
  39. kind: ApisixRoute 
  40. metadata: 
  41.   name: nexus 
  42.   namespace: default 
  43. spec: 
  44.   http: 
  45.     - name: root 
  46.       match: 
  47.         hosts: 
  48.           - ops.qikqiak.com 
  49.         paths: 
  50.           - "/*" 
  51.       backends: 
  52.       - serviceName: nexus 
  53.         servicePort: 8081 

直接創建上面的資源對象即可:

  1. ? kubectl apply -f nexus.yaml 
  2. ? kubectl get apisixroute 
  3. NAME    HOSTS                   URIS     AGE 
  4. nexus   ["ops.qikqiak.com"]   ["/*"]   39s 
  5. ? kubectl get pods -l app=nexus 
  6. NAME                     READY   STATUS    RESTARTS   AGE 
  7. nexus-6f78b79d4c-b79r4   1/1     Running   0          48s 
  8. ? kubectl get svc -l app=nexus 
  9. NAME    TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE 
  10. nexus   ClusterIP   10.102.53.243   <none>        8081/TCP   58s 

部署完成后,我們根據 ApisixRoute 對象中的配置,只需要將域名 ops.qikqiak.com 解析到 node2 節點(上面通過 port-forward 暴露了 80 端口)即可訪問:

APISIX Ingress 高級使用之 Url Rewrite

url rewrite

同樣如果現在需要通過一個子路徑來訪問 Nexus 應用的話又應該怎么來實現呢?比如通過 http://ops.qikqiak.com/nexus 來訪問我們的應用,首先我們肯定需要修改 ApisixRoute 對象中匹配的 paths 路徑,將其修改為 /nexus:

  1. apiVersion: apisix.apache.org/v2beta2 
  2. kind: ApisixRoute 
  3. metadata: 
  4.   name: nexus 
  5.   namespace: default 
  6. spec: 
  7.   http: 
  8.     - name: root 
  9.       match: 
  10.         hosts: 
  11.           - ops.qikqiak.com 
  12.         paths: 
  13.           - "/nexus*" 
  14.       backends: 
  15.       - serviceName: nexus 
  16.         servicePort: 8081 

更新后我們可以通過 http://ops.qikqiak.com/nexus 訪問應用:

APISIX Ingress 高級使用之 Url Rewrite

仔細分析發現很多靜態資源404了,這是因為現在我們只匹配了 /nexus 的請求,而我們的靜態資源是 /static 路徑開頭的,當然就匹配不到了,所以就出現了404,所以我們只需要加上這個 /static 路徑的匹配就可以了,同樣更新 ApisixRoute 對象,新增 /static/* 路徑支持:

  1. apiVersion: apisix.apache.org/v2beta2 
  2. kind: ApisixRoute 
  3. metadata: 
  4.   name: nexus 
  5.   namespace: default 
  6. spec: 
  7.   http: 
  8.     - name: root 
  9.       match: 
  10.         hosts: 
  11.           - ops.qikqiak.com 
  12.         paths: 
  13.           - "/nexus*" 
  14.           - "/static/*" 
  15.       backends: 
  16.       - serviceName: nexus 
  17.         servicePort: 8081 

更新后發現雖然靜態資源可以正常訪問了,但是當我們訪問 http://ops.qikqiak.com/nexus 的時候依然會出現 404 錯誤。

APISIX Ingress 高級使用之 Url Rewrite

這是因為我們這里是將 /nexus 路徑的請求直接路由到后端服務去了,而后端服務沒有對該路徑做任何處理,所以也就是404的響應了,在之前 ingress-nginx 或者 traefik 中我們是通過 url 重寫來實現的,而在 APISIX 中同樣可以實現這個處理,相當于在請求在真正到達上游服務之前將請求的 url 重寫到根目錄就可以了,這里我們需要用到 proxy-rewrite 這個插件(需要確保在安裝的時候已經包含了該插件),proxy-rewrite 是上游代理信息重寫插件,支持對 scheme、uri、host 等信息的重寫,該插件可配置的屬性如下表所示:

APISIX Ingress 高級使用之 Url Rewrite

我們現在的需求是希望將所有 /nexus 下面的請求都重寫到根路徑 / 下面去,所以我們應該使用 regex_uri 屬性,轉發到上游的新 uri 地址, 使用正則表達式匹配來自客戶端的 uri,當匹配成功后使用模板替換轉發到上游的 uri, 未匹配成功時將客戶端請求的uri 轉發至上游,重新修改后的 ApisixRoute 對象如下所示,新增 plugins 屬性來配置插件:

  1. apiVersion: apisix.apache.org/v2beta2 
  2. kind: ApisixRoute 
  3. metadata: 
  4.   name: nexus 
  5.   namespace: default 
  6. spec: 
  7.   http: 
  8.     - name: root 
  9.       match: 
  10.         hosts: 
  11.           - ops.qikqiak.com 
  12.         paths: 
  13.           - "/nexus*" 
  14.           - "/static/*" 
  15.       plugins: 
  16.       - name: proxy-rewrite 
  17.         enable: true 
  18.         config: 
  19.           regex_uri: ["^/nexus(/|$)(.*)""/$2"
  20.       backends: 
  21.       - serviceName: nexus 
  22.         servicePort: 8081 

這里我們啟用一個 proxy-rewrite 插件,并且將所有 /nexus 路徑的請求都重寫到了 / 跟路徑下,重新更新后再次訪問 http://ops.qikqiak.com/nexus 應該就可以正常訪問了:

APISIX Ingress 高級使用之 Url Rewrite

只有最后一個小問題了,從瀏覽器網絡請求中可以看出我們沒有去匹配 /service 這個路徑的請求,只需要配置上該路徑即可,如下所示:

  1. apiVersion: apisix.apache.org/v2beta2 
  2. kind: ApisixRoute 
  3. metadata: 
  4.   name: nexus 
  5.   namespace: default 
  6. spec: 
  7.   http: 
  8.     - name: root 
  9.       match: 
  10.         hosts: 
  11.           - ops.qikqiak.com 
  12.         paths: 
  13.           - "/nexus*" 
  14.           - "/static/*" 
  15.           - "/service/*" 
  16.       plugins: 
  17.       - name: proxy-rewrite 
  18.         enable: true 
  19.         config: 
  20.           regex_uri: ["^/nexus(/|$)(.*)""/$2"
  21.       backends: 
  22.       - serviceName: nexus 
  23.         servicePort: 8081 

現在重新訪問子路徑就完成正常了:

APISIX Ingress 高級使用之 Url Rewrite

redirect

現在當我們訪問 http://ops.qikqiak.com/nexus 或者 http://ops.qikqiak.com/nexus/ 的時候都可以得到正常的結果,一般來說我們可能希望能夠統一訪問路徑,比如訪問 /nexus 子路徑的時候可以自動跳轉到 /nexus/ 以 Splash 結尾的路徑上去。同樣要實現該需求我們只需要使用一個名為 redirect 的插件即可,該插件是 URI 重定向插件,可配置的屬性如下所示:

APISIX Ingress 高級使用之 Url Rewrite

要實現我們的需求直接使用 regex_uri 這個屬性即可,只需要去匹配 /nexus 的請求,然后進行跳轉即可,更新 ApisixRoute 對象:

  1. apiVersion: apisix.apache.org/v2beta2 
  2. kind: ApisixRoute 
  3. metadata: 
  4.   name: nexus 
  5.   namespace: default 
  6. spec: 
  7.   http: 
  8.     - name: root 
  9.       match: 
  10.         hosts: 
  11.           - ops.qikqiak.com 
  12.         paths: 
  13.           - "/nexus*" 
  14.           - "/static/*" 
  15.           - "/service/*" 
  16.       plugins: 
  17.       - name: proxy-rewrite 
  18.         enable: true 
  19.         config: 
  20.           regex_uri: ["^/nexus(/|$)(.*)""/$2"
  21.       - name: redirect 
  22.         enable: true 
  23.         config: 
  24.           regex_uri: ["^(/nexus)$""$1/"
  25.       backends: 
  26.       - serviceName: nexus 
  27.         servicePort: 8081 

我們新啟用了一個 redirect 插件,并配置 regex_uri: ["^(/nexus)$", "$1/"],這樣當訪問 /nexus 的時候會自動跳轉到 /nexus/ 路徑下面去。

同樣如果我們想要重定向到 https,只需要在該插件下面設置 config.http_to_https=true 即可:

  1. # ... 其他部分省略 
  2. name: redirect 
  3.   enable: true 
  4.   config: 
  5.     http_to_https: true 

tls

通過使用上面的 redirect 插件配置 http_to_https 可以將請求重定向到 https 上去,但是我們現在并沒有對我們的 ops.qikqiak.com 配置 https 證書,這里我們就需要使用 ApisixTls 對象來進行證書管理。

我們先使用 openssl 創建一個自簽名的證書,當然你有正規 CA 機構購買的證書的話直接將證書下載下來使用即可:

  1. ? openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout tls.key -out tls.crt -subj "/CN=ops.qikqiak.com" 

然后通過 Secret 對象來引用上面創建的證書文件:

  1. # 要注意證書文件名稱必須是 tls.crt 和 tls.key 
  2. ? kubectl create secret tls ops-tls --cert=tls.crt --key=tls.key 

然后就可以創建一個 ApisixTls 資源對象,引用上面的 Secret 即可:

  1. apiVersion: apisix.apache.org/v1 
  2. kind: ApisixTls 
  3. metadata: 
  4.   name: ops-tls 
  5. spec: 
  6.   hosts: 
  7.     - ops.qikqiak.com 
  8.   secret: 
  9.     name: ops-tls 
  10.     namespace: default 

同時 APISIX TLS 還可以配置 spec.client,用于進行 mTLS 雙向認證的配置。上面的資源對象創建完成后,即可訪問 https 服務了(chrome 瀏覽器默認會限制不安全的證書,只需要在頁面上輸入 thisisunsafe 即可訪問了):

APISIX Ingress 高級使用之 Url Rewrite

而且當訪問 http 的時候也會自動跳轉到 https 上面去,此外我們還可以結合 cert-manager 來實現自動化的 https。

完整的資源對象如下所示:

  1. apiVersion: apisix.apache.org/v2beta2 
  2. kind: ApisixRoute 
  3. metadata: 
  4.   name: nexus 
  5.   namespace: default 
  6. spec: 
  7.   http: 
  8.     - name: root 
  9.       match: 
  10.         hosts: 
  11.           - ops.qikqiak.com 
  12.         paths: 
  13.           - "/nexus*" 
  14.           - "/static/*" 
  15.           - "/service/*" 
  16.       plugins: 
  17.       - name: proxy-rewrite 
  18.         enable: true 
  19.         config: 
  20.           regex_uri: ["^/nexus(/|$)(.*)""/$2"
  21.       - name: redirect 
  22.         enable: true 
  23.         config: 
  24.           regex_uri: ["^(/nexus)$""$1/"
  25.       - name: redirect 
  26.         enable: true 
  27.         config: 
  28.           http_to_https: true 
  29.       backends: 
  30.       - serviceName: nexus 
  31.         servicePort: 8081 
  32. --- 
  33. apiVersion: apisix.apache.org/v1 
  34. kind: ApisixTls 
  35. metadata: 
  36.   name: ops-tls 
  37. spec: 
  38.   hosts: 
  39.     - ops.qikqiak.com 
  40.   secret: 
  41.     name: ops-tls 
  42.     namespace: default 

關于 APISIX Ingress 更多高級用法請繼續關注后續文章。

原文鏈接:https://mp.weixin.qq.com/s/P6I_27d3rjmN0Ls_aZMHUA

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美不卡一区二区三区免 | 精品福利视频一区二区三区 | 亚洲精品久久麻豆蜜桃 | 2019aw网站| 91好色| 男人桶女下面60分钟视频 | 91免费在线播放 | 95视频在线观看在线分类h片 | 成人精品区| 2019午夜福合集高清完整版 | 久久成人精品免费播放 | 亚洲成年| 校花被强迫np肉高h 校服下的白嫩小乳尖h1v1 | 日韩精品成人a在线观看 | 午夜在线观看免费完整直播网 | 高清男的插曲女的 欢迎你老狼 | 成年人在线免费看 | 亚洲精品动漫免费二区 | 国产一区二区三区久久小说 | 久久人妻熟女中文字幕AV蜜芽 | 欧美日韩国产在线人成dvd | 国产理论片在线观看 | 春意影院午夜爽爽爽免费 | 92国产福利久久青青草原 | 国产成人综合一区精品 | 欧美洲大黑香蕉在线视频 | 欧美视频在线播放观看免费福利资源 | 97精品国产自在现线免费 | 国产欧美日韩综合二区三区 | girlfriend动漫在线播放 | 国产欧美日韩一区二区三区在线 | 国产成人a v在线影院 | 操碰91| 98成人| 1024国产看片在线观看 | 亚洲 欧美 中文字幕 在线 | 催眠 迷j系列小说 | 亚洲男女在线 | 天堂樱桃bt在线www | 三级理论在线播放大全 | 青青草国产免费久久久91 |