{"id":1180,"date":"2022-08-19T15:47:27","date_gmt":"2022-08-19T07:47:27","guid":{"rendered":"https:\/\/www.linuxdevops.cn\/?p=1180"},"modified":"2023-04-07T09:53:16","modified_gmt":"2023-04-07T01:53:16","slug":"nginx-ingress-shields-an-interface-and-restricts-ip","status":"publish","type":"post","link":"https:\/\/www.linuxdevops.cn\/2022\/08\/nginx-ingress-shields-an-interface-and-restricts-ip\/","title":{"rendered":"NGINX ingress \u5c4f\u853d\u67d0\u4e2a\u63a5\u53e3\uff0c\u9488\u5bf9\u67d0\u4e2a\u63a5\u53e3\u8fdb\u884cIP\u767d\u540d\u5355\u8bbf\u95ee\u9650\u5236"},"content":{"rendered":"
\u4e00\u3001if\u8bed\u53e5\u4e2d\u7684\u5224\u65ad\u6761\u4ef6(nginx)<\/p>\n
1\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\uff1a<\/p>\n
==:\u7b49\u503c\u6bd4\u8f83;\n~\uff1a\u4e0e\u6307\u5b9a\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u5339\u914d\u65f6\u8fd4\u56de\u201c\u771f\u201d\uff0c\u5224\u65ad\u5339\u914d\u4e0e\u5426\u65f6\u533a\u5206\u5b57\u7b26\u5927\u5c0f\u5199\uff1b\n~*\uff1a\u4e0e\u6307\u5b9a\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u5339\u914d\u65f6\u8fd4\u56de\u201c\u771f\u201d\uff0c\u5224\u65ad\u5339\u914d\u4e0e\u5426\u65f6\u4e0d\u533a\u5206\u5b57\u7b26\u5927\u5c0f\u5199\uff1b\n!~\uff1a\u4e0e\u6307\u5b9a\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u4e0d\u5339\u914d\u65f6\u8fd4\u56de\u201c\u771f\u201d\uff0c\u5224\u65ad\u5339\u914d\u4e0e\u5426\u65f6\u533a\u5206\u5b57\u7b26\u5927\u5c0f\u5199\uff1b\n!~*\uff1a\u4e0e\u6307\u5b9a\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u4e0d\u5339\u914d\u65f6\u8fd4\u56de\u201c\u771f\u201d\uff0c\u5224\u65ad\u5339\u914d\u4e0e\u5426\u65f6\u4e0d\u533a\u5206\u5b57\u7b26\u5927\u5c0f\u5199\uff1b<\/code><\/pre>\n2\u3001\u6587\u4ef6\u53ca\u76ee\u5f55\u5339\u914d\u5224\u65ad\uff1a<\/p>\n
-f, !-f\uff1a\u5224\u65ad\u6307\u5b9a\u7684\u8def\u5f84\u662f\u5426\u4e3a\u5b58\u5728\u4e14\u4e3a\u6587\u4ef6\uff1b\n-d, !-d\uff1a\u5224\u65ad\u6307\u5b9a\u7684\u8def\u5f84\u662f\u5426\u4e3a\u5b58\u5728\u4e14\u4e3a\u76ee\u5f55\uff1b\n-e, !-e\uff1a\u5224\u65ad\u6307\u5b9a\u7684\u8def\u5f84\u662f\u5426\u5b58\u5728\uff0c\u6587\u4ef6\u6216\u76ee\u5f55\u5747\u53ef\uff1b\n-x, !-x\uff1a\u5224\u65ad\u6307\u5b9a\u8def\u5f84\u7684\u6587\u4ef6\u662f\u5426\u5b58\u5728\u4e14\u53ef\u6267\u884c\uff1b<\/code><\/pre>\n\u4e8c\u3001\u573a\u666f\u793a\u4f8b<\/p>\n
\u516c\u53f8API\u6587\u6863\u5305\u542b\u5728\u67d0\u4e2aJava\u5e94\u7528\u6216\u8005Golang\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ea\u9488\u5bf9API\u6587\u6863\u63a5\u53e3\u8fdb\u884c\u5c4f\u853d\u6216\u8005\u9650\u5236IP\u8bbf\u95ee\uff0c\u5176\u4f59\u63a5\u53e3\u5219\u662f\u6b63\u5e38\u8bbf\u95ee\u3002<\/p>\n
\u3010\u793a\u4f8b1\u3011\u76f4\u63a5\u5c4f\u853d\u6389\u63a5\u53e3\u6587\u6863<\/p>\n
#\u6dfb\u52a0nginx ingress\u6ce8\u89e3\uff0c\u5982\u679c\u8bf7\u6c42url\u4e2d\u5305\u542b"\/doc.html|\/swagger" \u5219\u8fd4\u56de403\n nginx.ingress.kubernetes.io\/server-snippet: |\n if ( $request_uri ~* (\/doc.html|\/swagger)) {\n return 403;\n }<\/code><\/pre>\n\u3010\u793a\u4f8b2\u3011\u63a5\u53e3\u53ea\u5f00\u653e\u67d0\u4e2aIP\u767d\u540d\u5355\u8fdb\u884c\u8bbf\u95ee<\/p>\n
\nnginx\u7684\u914d\u7f6e\u4e2d\u4e0d\u652f\u6301if\u6761\u4ef6\u7684\u903b\u8f91\u4e0e\uff0f\u903b\u8f91\u6216\u8fd0\u7b97 \uff0c\u5e76\u4e14\u4e0d\u652f\u6301if\u7684\u5d4c\u5957\u8bed\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u7528\u53d8\u91cf\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\uff1a<\/p>\n
#\u9996\u5148\u662f\u4f2a\u4ee3\u7801\uff08\u5373\u4e0d\u88abnginx\u652f\u6301\uff09\uff0c\u5199\u5728\u8fd9\u91cc\u53ea\u662f\u4e3a\u4e86\u65b9\u4fbf\u7406\u89e3\uff1a\n #\u89e3\u6790\uff1a\u5982\u679c\u8bbf\u95ee\u8bf7\u6c42url\u4e2d\u5305\u542b\"\/doc.html|\/swagger\"\u5e76\u4e14\u8bbf\u95eeIP\u4e0d\u662f\u516c\u53f8IP 222.222.222.222 \u5c31\u8fd4\u56de403\uff0c\u53cd\u4e4b\u5982\u679c\u662f\u516c\u53f8IP\u7684\u8bdd\u5c31\u6b63\u5e38\u8bbf\u95ee\u3002\n if ( $request_uri ~* (\/doc.html|\/swagger) && $remote_addr !~* ^222.222.222.222 ) {\n return 403;\n }<\/code><\/pre>\n<\/blockquote>\n\u4e0a\u9762\u8bed\u6cd5\u4e0d\u88abNGINX\u652f\u6301\uff0c\u4e0b\u9762\u662fNGINX\u6216\u8005NGINX ingress \u652f\u6301\u7684\u6b63\u786e\u8bed\u6cd5\uff0c\u7528\u53d8\u91cf\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\u3002<\/p>\n
#NGINX ingress \u914d\u7f6e\u793a\u4f8b \nnginx.ingress.kubernetes.io\/server-snippet: |\n set $test '';\n if ($request_uri ~* (\/doc.html|\/swagger)){ \n set $test 1;\n }\n if ( $remote_addr !~* 222.222.222.222 ) {\n set $test "${test}2";\n }\n if ( $test = 12 ) {\n return 403;\n }<\/code><\/pre>\n