{"id":50529,"date":"2024-10-08T11:51:14","date_gmt":"2024-10-08T03:51:14","guid":{"rendered":"https:\/\/server.hk\/cnblog\/50529\/"},"modified":"2024-10-08T11:51:14","modified_gmt":"2024-10-08T03:51:14","slug":"nginx-%e5%ae%89%e5%85%a8%e7%ad%96%e7%95%a5%ef%bc%9a%e5%a6%82%e6%9e%9c%e8%88%87-nginx-%e4%b8%80%e8%b5%b7%e9%81%8b%e8%a1%8c-php%ef%bc%8c%e4%bd%bf%e7%94%a8%e5%ae%89%e5%85%a8%e4%b8%94%e6%9c%80%e6%96%b0","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/50529\/","title":{"rendered":"Nginx \u5b89\u5168\u7b56\u7565\uff1a\u5982\u679c\u8207 Nginx \u4e00\u8d77\u904b\u884c PHP\uff0c\u4f7f\u7528\u5b89\u5168\u4e14\u6700\u65b0\u7684 PHP \u7248\u672c"},"content":{"rendered":"<h1 id=\"nginx-%e5%ae%89%e5%85%a8%e7%ad%96%e7%95%a5%ef%bc%9a%e5%a6%82%e6%9e%9c%e8%88%87-nginx-%e4%b8%80%e8%b5%b7%e9%81%8b%e8%a1%8c-php%ef%bc%8c%e4%bd%bf%e7%94%a8%e5%ae%89%e5%85%a8%e4%b8%94%e6%9c%80%e6%96%b0-LOzCkxQOos\">Nginx \u5b89\u5168\u7b56\u7565\uff1a\u5982\u679c\u8207 Nginx \u4e00\u8d77\u904b\u884c PHP\uff0c\u4f7f\u7528\u5b89\u5168\u4e14\u6700\u65b0\u7684 PHP \u7248\u672c<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u74b0\u5883\u4e2d\uff0c\u7db2\u7ad9\u7684\u5b89\u5168\u6027\u81f3\u95dc\u91cd\u8981\u3002Nginx \u4f5c\u70ba\u4e00\u500b\u9ad8\u6548\u7684\u7db2\u9801\u4f3a\u670d\u5668\uff0c\u5ee3\u6cdb\u7528\u65bc\u8655\u7406 PHP \u61c9\u7528\u7a0b\u5e8f\u3002\u7136\u800c\uff0c\u50c5\u50c5\u4f7f\u7528 Nginx \u4e26\u4e0d\u4fdd\u8b49\u5b89\u5168\uff0c\u9084\u9700\u8981\u63a1\u53d6\u4e00\u7cfb\u5217\u7684\u5b89\u5168\u7b56\u7565\u4f86\u4fdd\u8b77\u4f3a\u670d\u5668\u548c\u61c9\u7528\u7a0b\u5e8f\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u5728 Nginx \u74b0\u5883\u4e2d\u5b89\u5168\u5730\u904b\u884c PHP\uff0c\u4e26\u5f37\u8abf\u4f7f\u7528\u6700\u65b0\u7684 PHP \u7248\u672c\u7684\u91cd\u8981\u6027\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e9%81%b8%e6%93%87%e6%9c%80%e6%96%b0%e7%9a%84-php-%e7%89%88%e6%9c%ac%ef%bc%9f-LOzCkxQOos\">\u70ba\u4ec0\u9ebc\u9078\u64c7\u6700\u65b0\u7684 PHP \u7248\u672c\uff1f<\/h2>\n<p>\u4f7f\u7528\u6700\u65b0\u7684 PHP \u7248\u672c\u5c0d\u65bc\u7db2\u7ad9\u7684\u5b89\u5168\u6027\u81f3\u95dc\u91cd\u8981\u3002\u6bcf\u500b\u65b0\u7248\u672c\u7684 PHP \u90fd\u6703\u4fee\u5fa9\u5148\u524d\u7248\u672c\u4e2d\u7684\u6f0f\u6d1e\uff0c\u4e26\u5f15\u5165\u65b0\u7684\u5b89\u5168\u529f\u80fd\u3002\u6839\u64da PHP \u5b98\u65b9\u7db2\u7ad9\u7684\u8cc7\u6599\uff0c\u820a\u7248\u672c\u7684 PHP \u53ef\u80fd\u6703\u5b58\u5728\u672a\u4fee\u88dc\u7684\u5b89\u5168\u6f0f\u6d1e\uff0c\u9019\u4e9b\u6f0f\u6d1e\u53ef\u80fd\u6703\u88ab\u9ed1\u5ba2\u5229\u7528\uff0c\u5c0e\u81f4\u6578\u64da\u6d29\u9732\u6216\u670d\u52d9\u4e2d\u65b7\u3002<\/p>\n<ul>\n<li><strong>\u5b89\u5168\u4fee\u88dc\uff1a<\/strong>\u6700\u65b0\u7248\u672c\u7684 PHP \u6703\u5b9a\u671f\u767c\u5e03\u5b89\u5168\u66f4\u65b0\uff0c\u9019\u4e9b\u66f4\u65b0\u80fd\u5920\u4fee\u88dc\u5df2\u77e5\u7684\u6f0f\u6d1e\u3002<\/li>\n<li><strong>\u6027\u80fd\u512a\u5316\uff1a<\/strong>\u65b0\u7248\u672c\u901a\u5e38\u6703\u5305\u542b\u6027\u80fd\u6539\u9032\uff0c\u9019\u4e0d\u50c5\u80fd\u63d0\u9ad8\u7db2\u7ad9\u7684\u97ff\u61c9\u901f\u5ea6\uff0c\u9084\u80fd\u6e1b\u5c11\u8cc7\u6e90\u6d88\u8017\u3002<\/li>\n<li><strong>\u65b0\u529f\u80fd\uff1a<\/strong>\u6700\u65b0\u7248\u672c\u7684 PHP \u6703\u5f15\u5165\u65b0\u7684\u8a9e\u6cd5\u548c\u529f\u80fd\uff0c\u9019\u4e9b\u529f\u80fd\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u9ad8\u6548\u5730\u7de8\u5beb\u4ee3\u78bc\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%9c%a8-nginx-%e4%b8%ad%e9%85%8d%e7%bd%ae-php-%e7%9a%84%e5%ae%89%e5%85%a8%e7%ad%96%e7%95%a5-LOzCkxQOos\">\u5728 Nginx \u4e2d\u914d\u7f6e PHP \u7684\u5b89\u5168\u7b56\u7565<\/h2>\n<p>\u5728 Nginx \u4e2d\u904b\u884c PHP \u6642\uff0c\u6b63\u78ba\u7684\u914d\u7f6e\u662f\u78ba\u4fdd\u5b89\u5168\u7684\u95dc\u9375\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5efa\u8b70\u7684\u5b89\u5168\u7b56\u7565\uff1a<\/p>\n<h3 id=\"1-%e9%99%90%e5%88%b6-php-%e6%96%87%e4%bb%b6%e7%9a%84%e5%9f%b7%e8%a1%8c-LOzCkxQOos\">1. \u9650\u5236 PHP \u6587\u4ef6\u7684\u57f7\u884c<\/h3>\n<p>\u70ba\u4e86\u9632\u6b62\u672a\u7d93\u6388\u6b0a\u7684 PHP \u6587\u4ef6\u57f7\u884c\uff0c\u53ef\u4ee5\u5728 Nginx \u914d\u7f6e\u4e2d\u9650\u5236 PHP \u6587\u4ef6\u7684\u57f7\u884c\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u53ea\u5141\u8a31\u7279\u5b9a\u76ee\u9304\u4e2d\u7684 PHP \u6587\u4ef6\u57f7\u884c\uff1a<\/p>\n<pre><code>location ~ .php$ {\n    include fastcgi_params;\n    fastcgi_pass 127.0.0.1:9000;\n    fastcgi_index index.php;\n    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;\n    # \u9650\u5236\u57f7\u884c\u76ee\u9304\n    if ($request_uri !~ ^\/allowed_directory\/) {\n        return 403;\n    }\n}<\/code><\/pre>\n<h3 id=\"2-%e7%a6%81%e7%94%a8%e4%b8%8d%e5%bf%85%e8%a6%81%e7%9a%84-php-%e5%8a%9f%e8%83%bd-LOzCkxQOos\">2. \u7981\u7528\u4e0d\u5fc5\u8981\u7684 PHP \u529f\u80fd<\/h3>\n<p>\u5728 php.ini \u6587\u4ef6\u4e2d\u7981\u7528\u4e0d\u5fc5\u8981\u7684\u529f\u80fd\u53ef\u4ee5\u6e1b\u5c11\u6f5b\u5728\u7684\u653b\u64ca\u9762\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u7981\u7528 `exec()`\u3001`shell_exec()` \u548c `system()` \u7b49\u51fd\u6578\uff1a<\/p>\n<pre><code>disable_functions = exec,shell_exec,system<\/code><\/pre>\n<h3 id=\"3-%e4%bd%bf%e7%94%a8%e9%98%b2%e7%81%ab%e7%89%86%e5%92%8c%e5%ae%89%e5%85%a8%e6%a8%a1%e5%a1%8a-LOzCkxQOos\">3. \u4f7f\u7528\u9632\u706b\u7246\u548c\u5b89\u5168\u6a21\u584a<\/h3>\n<p>\u4f7f\u7528\u9632\u706b\u7246\uff08\u5982 iptables \u6216 UFW\uff09\u4f86\u9650\u5236\u5c0d Nginx \u4f3a\u670d\u5668\u7684\u8a2a\u554f\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u6b64\u5916\uff0c\u60a8\u9084\u53ef\u4ee5\u8003\u616e\u4f7f\u7528 ModSecurity \u7b49\u5b89\u5168\u6a21\u584a\u4f86\u589e\u5f37 Nginx \u7684\u5b89\u5168\u6027\u3002<\/p>\n<h3 id=\"4-%e5%ae%9a%e6%9c%9f%e6%9b%b4%e6%96%b0%e5%92%8c%e5%82%99%e4%bb%bd-LOzCkxQOos\">4. \u5b9a\u671f\u66f4\u65b0\u548c\u5099\u4efd<\/h3>\n<p>\u5b9a\u671f\u66f4\u65b0 Nginx \u548c PHP \u7248\u672c\uff0c\u4e26\u9032\u884c\u6578\u64da\u5099\u4efd\u662f\u78ba\u4fdd\u7db2\u7ad9\u5b89\u5168\u7684\u57fa\u672c\u63aa\u65bd\u3002\u9019\u6a23\uff0c\u5373\u4f7f\u767c\u751f\u5b89\u5168\u4e8b\u4ef6\uff0c\u60a8\u4e5f\u80fd\u8fc5\u901f\u6062\u5fa9\u670d\u52d9\u3002<\/p>\n<h2 id=\"%e7%b5%90%e8%ab%96-LOzCkxQOos\">\u7d50\u8ad6<\/h2>\n<p>\u5728 Nginx \u74b0\u5883\u4e2d\u904b\u884c PHP \u6642\uff0c\u63a1\u53d6\u9069\u7576\u7684\u5b89\u5168\u7b56\u7565\u81f3\u95dc\u91cd\u8981\u3002\u4f7f\u7528\u6700\u65b0\u7684 PHP \u7248\u672c\u4e0d\u50c5\u80fd\u63d0\u9ad8\u7db2\u7ad9\u7684\u5b89\u5168\u6027\uff0c\u9084\u80fd\u589e\u5f37\u6027\u80fd\u548c\u529f\u80fd\u3002\u901a\u904e\u9650\u5236 PHP \u6587\u4ef6\u7684\u57f7\u884c\u3001\u7981\u7528\u4e0d\u5fc5\u8981\u7684\u529f\u80fd\u3001\u4f7f\u7528\u9632\u706b\u7246\u548c\u5b89\u5168\u6a21\u584a\uff0c\u4ee5\u53ca\u5b9a\u671f\u66f4\u65b0\u548c\u5099\u4efd\uff0c\u60a8\u53ef\u4ee5\u6709\u6548\u5730\u4fdd\u8b77\u60a8\u7684\u7db2\u7ad9\u514d\u53d7\u6f5b\u5728\u7684\u5a01\u8105\u3002<\/p>\n<p>\u5982\u9700\u4e86\u89e3\u66f4\u591a\u6709\u95dc <a href=\"https:\/\/server.hk\">\u9999\u6e2f VPS<\/a> \u548c\u4f3a\u670d\u5668\u5b89\u5168\u7684\u8cc7\u8a0a\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22 Nginx \u5b89\u5168\u7b56\u7565\uff0c\u78ba\u4fdd\u8207 PHP \u4e00\u8d77\u904b\u884c\u6642\u4f7f\u7528\u6700\u65b0\u7248\u672c\uff0c\u63d0\u5347\u7db2\u7ad9\u5b89\u5168\u6027\u8207\u6027\u80fd\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4924],"tags":[],"class_list":["post-50529","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/50529","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=50529"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/50529\/revisions"}],"predecessor-version":[{"id":50530,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/50529\/revisions\/50530"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=50529"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=50529"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=50529"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}