{"id":441607,"date":"2025-05-11T09:57:31","date_gmt":"2025-05-11T01:57:31","guid":{"rendered":"https:\/\/www.idc.net\/help\/?p=441607"},"modified":"2025-05-11T09:57:31","modified_gmt":"2025-05-11T01:57:31","slug":"%e4%bd%bf%e7%94%a8-nginx-%e9%9b%86%e6%88%90-modsecurity-%e5%ae%9e%e7%8e%b0%e9%ab%98%e5%ae%89%e5%85%a8%e9%98%b2%e6%8a%a4","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/441607\/","title":{"rendered":"\u4f7f\u7528 Nginx \u96c6\u6210 ModSecurity \u5b9e\u73b0\u9ad8\u5b89\u5168\u9632\u62a4"},"content":{"rendered":"<h2>\u5f15\u8a00<\/h2>\n<p>\u5728\u5f53\u4eca\u7f51\u7edc\u73af\u5883\u4e2d\uff0cWeb \u670d\u52a1\u9762\u4e34\u7740\u8d8a\u6765\u8d8a\u591a\u7684\u5b89\u5168\u6311\u6218\u3002Nginx \u4f5c\u4e3a\u4e00\u6b3e\u9ad8\u6027\u80fd\u7684Web\u670d\u52a1\u5668\uff0c\u82e5\u80fd\u642d\u914d ModSecurity \u6a21\u5757\u4f7f\u7528\uff0c\u5c06\u5927\u5927\u63d0\u5347\u9632\u62a4\u80fd\u529b\u3002\u672c\u6587\u5c06\u4ee5\u5b9e\u7528\u6280\u672f\u4e3a\u5bfc\u5411\uff0c\u4ecb\u7ecd\u5982\u4f55\u5728 Linux \u7cfb\u7edf\u4e2d\u4e3a Nginx \u96c6\u6210 ModSecurity v3\uff0c\u5b9e\u73b0\u5b89\u5168\u8bf7\u6c42\u8fc7\u6ee4\u4e0e\u89c4\u5219\u63a7\u5236\uff0c\u540c\u65f6\u4e5f\u4e3a\u670d\u52a1\u5668\u8fd0\u7ef4\u8005\u63d0\u4f9b\u90e8\u7f72\u5efa\u8bae\u548c\u73af\u5883\u4f18\u5316\u63d0\u793a\u3002<\/p>\n<p>\u5bf9\u4e8e\u8fd0\u884c\u9ad8\u5e76\u53d1\u4e1a\u52a1\u6216\u90e8\u7f72\u654f\u611f\u6570\u636e\u7684\u4f01\u4e1a\u7528\u6237\u6765\u8bf4\uff0c\u9009\u7528<strong>\u9999\u6e2f\u4e91\u670d\u52a1\u5668<\/strong>\u4e0d\u4ec5\u80fd\u6709\u6548\u7f29\u77ed\u8bbf\u95ee\u5ef6\u8fdf\uff0c\u8fd8\u80fd\u5b9e\u73b0\u6570\u636e\u8de8\u5883\u4f20\u8f93\u7684\u7075\u6d3b\u5408\u89c4\u3002\u4ee5\u4e0b\u5185\u5bb9\u5c06\u5e2e\u52a9\u60a8\u5728\u6784\u5efa\u5b89\u5168\u7cfb\u7edf\u7684\u540c\u65f6\uff0c\u4e86\u89e3\u5408\u9002\u7684\u670d\u52a1\u5668\u8d44\u6e90\u9009\u62e9\u3002<\/p>\n<hr \/>\n<h2>\u4e00\u3001\u786e\u8ba4 Nginx \u7248\u672c\u4fe1\u606f<\/h2>\n<p>\u786e\u4fdd\u670d\u52a1\u5668\u4e2d\u5b89\u88c5\u7684 Nginx \u4e0e\u8ba1\u5212\u7f16\u8bd1\u7684\u7248\u672c\u4e00\u81f4\u662f\u6574\u4e2a\u6d41\u7a0b\u7684\u524d\u63d0\uff1a<\/p>\n<pre><code class=\"language-bash\">nginx -v\r\n# \u8f93\u51fa\u793a\u4f8b\uff1a\r\nnginx version: nginx\/1.26.2\r\n<\/code><\/pre>\n<hr \/>\n<h2>\u4e8c\u3001\u5b89\u88c5\u7cfb\u7edf\u4f9d\u8d56\u7ec4\u4ef6<\/h2>\n<p>\u4f7f\u7528 YUM \u5305\u7ba1\u7406\u5de5\u5177\u5b89\u88c5\u4ee5\u4e0b\u5fc5\u8981\u4f9d\u8d56\uff1a<\/p>\n<pre><code class=\"language-bash\">yum install epel-release\r\nyum install -y gcc gcc-c++ flex bison yajl lua curl-devel \\\r\nzlib-devel pcre-devel pcre2-devel libxml2-devel libtool \\\r\nautoconf automake make git wget openssl openssl-devel\r\nyum install lmdb GeoIP-devel ssdeep-devel\r\n<\/code><\/pre>\n<p>\u8fd9\u4e9b\u7ec4\u4ef6\u662f\u7f16\u8bd1 ModSecurity \u548c Nginx \u7684\u57fa\u7840\u4f9d\u8d56\u3002\u5efa\u8bae\u4f7f\u7528\u5168\u65b0\u7cfb\u7edf\u73af\u5883\uff0c\u907f\u514d\u7248\u672c\u51b2\u7a81\u3002<\/p>\n<hr \/>\n<h2>\u4e09\u3001\u7f16\u8bd1\u5b89\u88c5 ModSecurity \u6a21\u5757<\/h2>\n<p>\u8fdb\u5165\u672c\u5730\u6e90\u7801\u76ee\u5f55\u5e76\u83b7\u53d6 ModSecurity\uff1a<\/p>\n<pre><code class=\"language-bash\">cd \/usr\/local\/\r\ngit clone https:\/\/gitee.com\/gongxw\/ModSecurity.git\r\ncd ModSecurity\r\ngit submodule init &amp;&amp; git submodule update\r\nsh build.sh\r\n.\/configure\r\nmake clean  # \u5982\u4ece\u5176\u4ed6\u670d\u52a1\u5668\u590d\u5236\u6e90\u7801\uff0c\u5efa\u8bae\u6e05\u7406\r\nmake\r\nmake install\r\n<\/code><\/pre>\n<p>\u6b64\u6a21\u5757\u5c06\u4f5c\u4e3a Nginx \u7684\u4e00\u4e2a\u6269\u5c55\u6a21\u5757\u52a0\u8f7d\u4f7f\u7528\u3002<\/p>\n<hr \/>\n<h2>\u56db\u3001\u96c6\u6210 ModSecurity \u5230 Nginx<\/h2>\n<h3>\u83b7\u53d6 Nginx \u5bf9\u5e94\u6e90\u7801\u5e76\u914d\u7f6e\u6a21\u5757<\/h3>\n<pre><code class=\"language-bash\">cd \/usr\/local\/\r\ntar -zxf nginx-1.26.2.tar.gz\r\ncd nginx-1.26.2\/\r\n<\/code><\/pre>\n<p>\u786e\u8ba4 Nginx \u7f16\u8bd1\u53c2\u6570\uff1a<\/p>\n<pre><code class=\"language-bash\">nginx -V\r\n<\/code><\/pre>\n<p>\u4f7f\u7528\u4e0b\u5217\u53c2\u6570\u8fdb\u884c\u7f16\u8bd1\uff08\u9700\u5c06 ModSecurity \u6a21\u5757\u8def\u5f84\u6dfb\u52a0\uff09\uff1a<\/p>\n<pre><code class=\"language-bash\">.\/configure --prefix=\/etc\/nginx --sbin-path=\/usr\/sbin\/nginx \\\r\n--modules-path=\/usr\/lib64\/nginx\/modules --conf-path=\/etc\/nginx\/nginx.conf \\\r\n--error-log-path=\/var\/log\/nginx\/error.log --http-log-path=\/var\/log\/nginx\/access.log \\\r\n--pid-path=\/var\/run\/nginx.pid --lock-path=\/var\/run\/nginx.lock \\\r\n--with-http_ssl_module --with-stream_ssl_module \\\r\n--add-module=\/usr\/local\/ModSecurity-nginx\r\nmake\r\nmake install\r\n<\/code><\/pre>\n<hr \/>\n<h2>\u4e94\u3001\u914d\u7f6e ModSecurity \u89c4\u5219\u96c6<\/h2>\n<pre><code class=\"language-bash\">mkdir -p \/usr\/local\/nginx\/conf\/modsecurity\r\ncd \/usr\/local\/\r\ngit clone https:\/\/github.com\/coreruleset\/coreruleset.git\r\ncp ModSecurity\/modsecurity.conf-recommended \/usr\/local\/nginx\/conf\/modsecurity\/modsecurity.conf\r\ncp ModSecurity\/unicode.mapping \/usr\/local\/nginx\/conf\/modsecurity\/\r\ncp -r coreruleset\/rules \/usr\/local\/nginx\/conf\/modsecurity\/\r\ncp coreruleset\/crs-setup.conf.example \/usr\/local\/nginx\/conf\/modsecurity\/crs-setup.conf\r\n<\/code><\/pre>\n<p>\u4fee\u6539\u89c4\u5219\u914d\u7f6e\u6587\u4ef6\uff0c\u542f\u7528\u5b89\u5168\u9632\u62a4\u529f\u80fd\uff1a<\/p>\n<pre><code class=\"language-bash\">vi \/usr\/local\/nginx\/conf\/modsecurity\/modsecurity.conf\r\n# \u6ce8\u91ca\u539f\u6709\u89c4\u5219\u884c\r\n# SecRuleEngine DetectionOnly\r\nSecRuleEngine On\r\nInclude \/usr\/local\/nginx\/conf\/modsecurity\/crs-setup.conf\r\nInclude \/usr\/local\/nginx\/conf\/modsecurity\/rules\/*.conf\r\n<\/code><\/pre>\n<hr \/>\n<h2>\u516d\u3001Nginx \u914d\u7f6e\u6587\u4ef6\u8c03\u6574<\/h2>\n<h3>\u8bbe\u7f6e\u5168\u5c40\u9632\u62a4\u89c4\u5219\uff08\u5168\u5c40\u914d\u7f6e\uff09<\/h3>\n<pre><code class=\"language-nginx\">modsecurity on;\r\nmodsecurity_rules_file \/usr\/local\/nginx\/conf\/modsecurity\/modsecurity.conf;\r\n<\/code><\/pre>\n<h3>\u5c40\u90e8\u542f\u7528\uff08\u5efa\u8bae\u7528\u4e8e\u7279\u5b9a server \u5757\uff09<\/h3>\n<pre><code class=\"language-nginx\">server {\r\n    listen 443 ssl;\r\n    modsecurity on;\r\n    modsecurity_rules_file \/usr\/local\/nginx\/conf\/modsecurity\/modsecurity.conf;\r\n    ...\r\n}\r\n<\/code><\/pre>\n<p>\u68c0\u67e5\u65e5\u5fd7\u8f93\u51fa\u786e\u8ba4\u6a21\u5757\u8fd0\u884c\u60c5\u51b5\uff1a<\/p>\n<pre><code class=\"language-bash\">tail -10f \/var\/log\/modsec_audit.log\r\n<\/code><\/pre>\n<hr \/>\n<h2>\u4e03\u3001\u90e8\u7f72\u5efa\u8bae\u4e0e\u6027\u80fd\u4f18\u5316<\/h2>\n<p>\u90e8\u7f72 ModSecurity \u4f1a\u7565\u5fae\u589e\u52a0\u7cfb\u7edf\u8d44\u6e90\u6d88\u8017\uff0c\u56e0\u6b64\u5efa\u8bae\u7ed3\u5408\u6027\u80fd\u8f83\u4f18\u7684\u670d\u52a1\u5668\u8d44\u6e90\u3002\u5728\u56fd\u5185\u5916\u8bbf\u95ee\u5747\u9700\u9ad8\u7a33\u5b9a\u6027\u7684\u573a\u666f\u4e0b\uff0c\u9009\u62e9<strong>\u9999\u6e2fvps<\/strong>\u6216<strong>\u9999\u6e2f\u72ec\u7acb\u670d\u52a1\u5668<\/strong>\u5c06\u66f4\u9002\u5408\u3002\u9999\u6e2f\u8282\u70b9\u80fd\u63d0\u4f9b\u5feb\u901f\u8bbf\u95ee\u6e2f\u6fb3\u53f0\u53ca\u4e1c\u5357\u4e9a\u5730\u533a\u7684\u4f18\u8d28\u901a\u9053\uff0c\u5bf9\u8de8\u5883\u4e1a\u52a1\u5c24\u4e3a\u53cb\u597d\u3002\u4f01\u4e1a\u53ef\u8003\u8651\u9009\u8d2d\u5177\u5907\u7075\u6d3b\u6269\u5c55\u3001\u9ad8\u9632\u62a4\u5e26\u5bbd\u548cBGP\u591a\u7ebf\u63a5\u5165\u7684\u9999\u6e2f\u670d\u52a1\u5668\u8d44\u6e90\u3002<\/p>\n<hr \/>\n<h2>\u7ed3\u8bed<\/h2>\n<p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u60a8\u5df2\u6210\u529f\u5b8c\u6210\u4e86\u57fa\u4e8e Nginx \u7684 ModSecurity \u96c6\u6210\u3002\u8be5\u65b9\u6848\u4e0d\u4ec5\u589e\u5f3a\u4e86 Web \u670d\u52a1\u7684\u5b89\u5168\u80fd\u529b\uff0c\u8fd8\u4e3a\u62b5\u5fa1\u5e38\u89c1\u653b\u51fb\u5982 SQL \u6ce8\u5165\u3001XSS \u63d0\u4f9b\u4e86\u5f3a\u5927\u4fdd\u969c\u3002<\/p>\n<p>\u4e3a\u786e\u4fdd\u8fd9\u4e9b\u529f\u80fd\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e2d\u7a33\u5b9a\u8fd0\u884c\uff0c\u5efa\u8bae\u914d\u5408\u9ad8\u6027\u80fd\u7684\u670d\u52a1\u5668\u67b6\u6784\u4f7f\u7528\u3002\u5c24\u5176\u662f<strong>\u9999\u6e2f\u4e91\u670d\u52a1\u5668<\/strong>\u3001<strong>\u9999\u6e2fvps<\/strong>\u7b49\u5177\u5907\u9ad8\u901f\u7f51\u7edc\u548c\u7a33\u5b9a\u7535\u529b\u4fdd\u969c\u7684\u4ea7\u54c1\uff0c\u53ef\u4e3a\u4f01\u4e1a\u7cfb\u7edf\u90e8\u7f72\u6253\u4e0b\u575a\u5b9e\u57fa\u7840\u3002\u7acb\u5373\u4e86\u89e3\uff1a<a href=\"https:\/\/idc.net\/\">\u9999\u6e2f\u72ec\u7acb\u670d\u52a1\u5668\u00a0<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f15\u8a00 \u5728\u5f53\u4eca\u7f51\u7edc\u73af\u5883\u4e2d\uff0cWeb \u670d\u52a1\u9762\u4e34\u7740\u8d8a\u6765\u8d8a\u591a\u7684\u5b89\u5168\u6311\u6218\u3002Nginx \u4f5c\u4e3a\u4e00\u6b3e\u9ad8\u6027\u80fd\u7684Web\u670d\u52a1\u5668\uff0c\u82e5\u80fd\u642d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42],"tags":[],"class_list":["post-441607","post","type-post","status-publish","format-standard","hentry","category-nginx"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/441607","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=441607"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/441607\/revisions"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=441607"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=441607"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=441607"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}