update apisix_conf

This commit is contained in:
Chu Văn Hải committed 2026-06-18 13:11:32 +07:00
1 parent a0315401e8
commit 97530237c6
3 files changed
+65 -1

No files matched your search

+46
View File
@@ -125,6 +125,41 @@ Phương án này cho phép **Client** chỉ cần truyền API Key đơn giản
nginx_config:
http_configuration_snippet: |
lua_shared_dict oauth_cache 1m; # Cấp 1MB bộ nhớ để cache access token
map $http_cf_connecting_ip $client_ip_from_cf_or_xff {
default $http_cf_connecting_ip;
"" $http_x_forwarded_for;
}
map $client_ip_from_cf_or_xff $client_real_ip {
default $client_ip_from_cf_or_xff;
"" $remote_addr;
}
```
Nếu APISIX chạy sau Cloudflare + Nginx/aaPanel cùng VPS, biến `$client_real_ip` giúp access log lấy IP thật theo thứ tự ưu tiên: `CF-Connecting-IP` -> `X-Forwarded-For` -> `$remote_addr`. Không khai báo `real_ip_header` trong `http_configuration_snippet` vì APISIX đã sinh directive này sẵn và Nginx sẽ báo duplicate. Sau khi đổi `config.yaml`, cần recreate container APISIX:
```bash
docker compose up -d --force-recreate apisix
```
```bash
curl -i http://127.0.0.1:9180/apisix/admin/global_rules/real-ip \
-H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' \
-H 'Content-Type: application/json' \
-X PUT \
-d '{
"plugins": {
"real-ip": {
"source": "client_real_ip",
"trusted_addresses": [
"127.0.0.1",
"10.0.0.0/8",
"172.16.0.0/12",
"192.168.0.0/16"
],
"recursive": true
}
}
}'
```
#### Bước 2: Thiết lập Route với plugin `key-auth` và `serverless-pre-function`
@@ -140,6 +175,16 @@ curl -i http://localhost:9180/apisix/admin/routes/ssm-vn-dev-route \
"name": "ssm-vn-dev-route",
"plugins": {
"key-auth": {},
"real-ip": {
"source": "client_real_ip",
"trusted_addresses": [
"127.0.0.1",
"10.0.0.0/8",
"172.16.0.0/12",
"192.168.0.0/16"
],
"recursive": true
},
"proxy-rewrite": {
"regex_uri": [
"^/ssm-vn-dev(/.*)$",
@@ -172,6 +217,7 @@ curl -i http://localhost:9180/apisix/admin/routes/ssm-vn-dev-route \
> [!IMPORTANT]
> **Giải thích tham số cấu hình Upstream:**
> * `pass_host: "node"`: Bắt buộc cấu hình trường này để APISIX ghi đè giá trị header `Host` gửi tới Dynamics 365 trùng khớp với node URL (`<YOUR_ORG>.crm.dynamics.com`). Nếu thiếu, Dynamics 365 sẽ từ chối xử lý request vì host header không khớp.
> * `real-ip`: Ghi lại `remote_addr` trong APISIX từ biến `$client_real_ip`, ưu tiên `CF-Connecting-IP`, fallback sang `X-Forwarded-For`, rồi `$remote_addr`, giúp `loki-logger` và access log thấy IP thật thay vì IP Docker gateway.
> * `proxy-rewrite.regex_uri`: Bắt buộc để bỏ prefix route nội bộ `/ssm-vn-dev` trước khi gọi Dataverse. Request vào `/ssm-vn-dev/api/data/v9.0/...` sẽ được gửi lên upstream thành `/api/data/v9.0/...`.
> * Các tham số `<YOUR_TENANT_ID>`, `<YOUR_CLIENT_ID>`, `<YOUR_CLIENT_SECRET>`, `<YOUR_ORG>` trong đoạn mã Lua cần được thay thế bằng thông tin môi trường Azure AD và Dynamics 365 thực tế của bạn.