@@ -892,6 +892,126 @@ location = /robots.txt {
892
892
}
893
893
```
894
894
895
+ ### 获取请求ip
896
+ <!-- rehype:wrap-class=col-span-3-->
897
+
898
+ ``` nginx
899
+ server {
900
+ listen 80;
901
+ server_name xxx.top;
902
+
903
+ location / {
904
+ access_log /data/logs/nginx/json_ip.log json;
905
+ proxy_set_header Host $http_host;
906
+ proxy_set_header X-Real-Ip $remote_addr;
907
+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
908
+ proxy_pass http://127.0.0.1:9999;
909
+ }
910
+ }
911
+
912
+ server {
913
+ listen 9999;
914
+
915
+ location / {
916
+ access_log off;
917
+ default_type application/json;
918
+ return 200 "{\"ip\":\"$http_X_Real_Ip\"}";
919
+ }
920
+ }
921
+ ```
922
+
923
+ ### 判断请求参数
924
+ <!-- rehype:wrap-class=col-span-3-->
925
+
926
+ ``` nginx
927
+ # 判断多个参数示例
928
+ set $flagts 0;
929
+ if ( $arg_aaa ~ "^aaa" ) {
930
+ set $flagts "${flagts}1";
931
+ }
932
+ if ( $arg_bbb ~ "^bbb" ) {
933
+ set $flagts "${flagts}1";
934
+ }
935
+ if ( $flagts = "011" ) {
936
+ return 200;
937
+ }
938
+ ```
939
+
940
+ ### 流量镜像配置
941
+ <!-- rehype:wrap-class=col-span-3-->
942
+
943
+ ``` nginx
944
+ server {
945
+ listen 80;
946
+ server_name 192.168.1.1;
947
+
948
+ location = /mirror1 {
949
+ internal;
950
+ #### address1 ####
951
+ proxy_set_header Host mirror1.com;
952
+ proxy_pass http://127.0.0.1:8008/api/service/list;
953
+ }
954
+
955
+ location = /mirror2 {
956
+ internal;
957
+ #### address2 ####
958
+ proxy_set_header Host mirror2.com;
959
+ proxy_pass http://127.0.0.1:8009/api/service/list;
960
+ }
961
+
962
+ # 只转发这个接口
963
+ location /api/service/list {
964
+ access_log /data/logs/nginx/json_test_to_mirror.log json;
965
+ mirror /mirror1;
966
+ mirror /mirror2;
967
+ proxy_pass http://127.0.0.1:8007;
968
+ }
969
+
970
+ location / {
971
+ access_log /data/logs/nginx/json_test.log json;
972
+ proxy_pass http://192.168.1.1:8007;
973
+ }
974
+
975
+ }
976
+
977
+ ### 流量镜像配置
978
+ <!--rehype:wrap-class=col-span-3-->
979
+
980
+ ```nginx
981
+ server {
982
+ listen 80;
983
+ server_name 192.168.1.1;
984
+
985
+ location = /mirror1 {
986
+ internal;
987
+ #### address1 ####
988
+ proxy_set_header Host mirror1.com;
989
+ proxy_pass http://127.0.0.1:8008/api/service/list;
990
+ }
991
+
992
+ location = /mirror2 {
993
+ internal;
994
+ #### address2 ####
995
+ proxy_set_header Host mirror2.com;
996
+ proxy_pass http://127.0.0.1:8009/api/service/list;
997
+ }
998
+
999
+ # 只转发这个接口
1000
+ location /api/service/list {
1001
+ access_log /data/logs/nginx/json_test_to_mirror.log json;
1002
+ mirror /mirror1;
1003
+ mirror /mirror2;
1004
+ proxy_pass http://127.0.0.1:8007;
1005
+ }
1006
+
1007
+ location / {
1008
+ access_log /data/logs/nginx/json_test.log json;
1009
+ proxy_pass http://192.168.1.1:8007;
1010
+ }
1011
+
1012
+ }
1013
+ ```
1014
+
895
1015
另见
896
1016
---
897
1017
0 commit comments