From 97530237c63509afc481a972e522739f0c60a0fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chu=20V=C4=83n=20H=E1=BA=A3i?= Date: Thu, 18 Jun 2026 13:11:32 +0700 Subject: [PATCH] update apisix_conf --- apisix-stack/README.MD | 46 ++++++++++++++++++++++++ apisix-stack/apisix_conf/config.yaml | 10 +++++- apisix-stack/apisix_conf/route_create.sh | 10 ++++++ 3 files changed, 65 insertions(+), 1 deletion(-) diff --git a/apisix-stack/README.MD b/apisix-stack/README.MD index 4124ea5..5d036cd 100644 --- a/apisix-stack/README.MD +++ b/apisix-stack/README.MD @@ -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 (`.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ố ``, ``, ``, `` 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. diff --git a/apisix-stack/apisix_conf/config.yaml b/apisix-stack/apisix_conf/config.yaml index 244d309..6931c85 100644 --- a/apisix-stack/apisix_conf/config.yaml +++ b/apisix-stack/apisix_conf/config.yaml @@ -20,9 +20,17 @@ deployment: nginx_config: http_configuration_snippet: | lua_shared_dict oauth_cache 1m; + 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; + } http: access_log: "/dev/stdout" - access_log_format: '{"time_local":"$time_local","client_ip":"$remote_addr","request":"$request","status":$status,"body_bytes_sent":$body_bytes_sent,"request_time":$request_time,"upstream_response_time":"$upstream_response_time"}' + access_log_format: '{"time_local":"$time_local","client_ip":"$client_real_ip","request":"$request","status":$status,"body_bytes_sent":$body_bytes_sent,"request_time":$request_time,"upstream_response_time":"$upstream_response_time"}' access_log_format_escape: json error_log: "/dev/stderr" error_log_level: "warn" diff --git a/apisix-stack/apisix_conf/route_create.sh b/apisix-stack/apisix_conf/route_create.sh index 35d0f6e..3379492 100644 --- a/apisix-stack/apisix_conf/route_create.sh +++ b/apisix-stack/apisix_conf/route_create.sh @@ -102,6 +102,16 @@ cat > "$PAYLOAD_FILE" <