{"id":304,"date":"2025-06-30T09:29:30","date_gmt":"2025-06-30T01:29:30","guid":{"rendered":"https:\/\/qkd.koudaipc.com\/?p=304"},"modified":"2025-06-30T09:29:30","modified_gmt":"2025-06-30T01:29:30","slug":"nginx-php-%e6%94%af%e6%8c%81-http-%e7%9b%ae%e5%bd%95%e5%88%97%e8%a1%a8%e7%9a%84%e7%a4%ba%e4%be%8b","status":"publish","type":"post","link":"https:\/\/ns1.koudaipc.com\/en\/2025\/06\/30\/nginx-php-%e6%94%af%e6%8c%81-http-%e7%9b%ae%e5%bd%95%e5%88%97%e8%a1%a8%e7%9a%84%e7%a4%ba%e4%be%8b\/","title":{"rendered":"Nginx + PHP \u652f\u6301 HTTP \u76ee\u5f55\u5217\u8868\u7684\u793a\u4f8b"},"content":{"rendered":"<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u914d\u7f6e Nginx + PHP \u652f\u6301 HTTP \u76ee\u5f55\u5217\u8868\u7684\u793a\u4f8b\uff1a<\/p>\n<h3>\u6838\u5fc3\u914d\u7f6e\u8bf4\u660e<\/h3>\n<ol>\n<li><strong>\u76ee\u5f55\u5217\u8868<\/strong>\uff1a\u4f7f\u7528 <code>autoindex on<\/code> \u5f00\u542f\u76ee\u5f55\u6d4f\u89c8<\/li>\n<li><strong>PHP \u5904\u7406<\/strong>\uff1a\u901a\u8fc7 FastCGI \u5c06 PHP \u8bf7\u6c42\u4ea4\u7ed9 PHP-FPM \u5904\u7406<\/li>\n<li><strong>\u5b89\u5168\u9694\u79bb<\/strong>\uff1a\u7981\u6b62\u663e\u793a\u654f\u611f\u6587\u4ef6\uff08\u5982 <code>.env<\/code>\uff09<\/li>\n<\/ol>\n<h3>\u5b8c\u6574 Nginx \u914d\u7f6e\u793a\u4f8b<\/h3>\n<pre><code class=\"language-nginx\">server {\n    listen 80;\n    server_name yourdomain.com;\n    root \/var\/www\/html;\n    index index.php index.html;\n\n    # \u4e3b\u76ee\u5f55\u914d\u7f6e\n    location \/ {\n        autoindex on;                   # \u542f\u7528\u76ee\u5f55\u5217\u8868\n        autoindex_exact_size off;      # \u663e\u793a\u6587\u4ef6\u5927\u5c0f\uff08KB\/MB\u683c\u5f0f\uff09\n        autoindex_localtime on;         # \u663e\u793a\u672c\u5730\u65f6\u95f4\n        autoindex_format html;          # HTML\u683c\u5f0f\u8f93\u51fa\n    }\n\n    # PHP \u5904\u7406\u914d\u7f6e\n    location ~ \\.php$ {\n        include fastcgi_params;\n        fastcgi_pass unix:\/var\/run\/php\/php8.1-fpm.sock; # \u6839\u636e\u5b9e\u9645\u7248\u672c\u4fee\u6539\n        fastcgi_index index.php;\n        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;\n    }\n\n    # \u9690\u85cf\u654f\u611f\u6587\u4ef6\uff08\u53ef\u9009\uff09\n    location ~ \/\\.(env|git) {\n        deny all;\n        return 403;\n    }\n}<\/code><\/pre>\n<h3>\u5173\u952e\u53c2\u6570\u8bf4\u660e<\/h3>\n<table>\n<thead>\n<tr>\n<th>\u6307\u4ee4<\/th>\n<th>\u4f5c\u7528<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>autoindex on<\/code><\/td>\n<td>\u542f\u7528\u76ee\u5f55\u5217\u8868<\/td>\n<\/tr>\n<tr>\n<td><code>autoindex_exact_size off<\/code><\/td>\n<td>\u4eba\u6027\u5316\u663e\u793a\u6587\u4ef6\u5927\u5c0f<\/td>\n<\/tr>\n<tr>\n<td><code>autoindex_localtime on<\/code><\/td>\n<td>\u663e\u793a\u670d\u52a1\u5668\u672c\u5730\u65f6\u95f4<\/td>\n<\/tr>\n<tr>\n<td><code>autoindex_format html<\/code><\/td>\n<td>\u4f7f\u7528 HTML \u683c\u5f0f\u5316\u8f93\u51fa<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u6548\u679c\u793a\u4f8b<\/h3>\n<p>\u8bbf\u95ee <code>http:\/\/yourdomain.com\/some-directory\/<\/code> \u5c06\u663e\u793a\uff1a<\/p>\n<pre><code>\ud83d\udcc1 Parent Directory\/\n\ud83d\udcc1 Subfolder\/\n\ud83d\udcc4 image.jpg (1.2 MB)\n\ud83d\udcc4 document.pdf (350 KB)\n\ud83d\udcc4 script.php (\u53ef\u6267\u884c)<\/code><\/pre>\n<h3>\u5b89\u5168\u5efa\u8bae<\/h3>\n<ol>\n<li><strong>\u4ec5\u9650\u5185\u7f51\u4f7f\u7528<\/strong>\uff1a\u751f\u4ea7\u73af\u5883\u4e0d\u5efa\u8bae\u5f00\u542f\u76ee\u5f55\u5217\u8868<\/li>\n<li><strong>\u8bbf\u95ee\u9650\u5236<\/strong>\uff1a\u6dfb\u52a0\u57fa\u7840\u8ba4\u8bc1\n<pre><code class=\"language-nginx\">location \/protected {\n   auth_basic \"Restricted\";\n   auth_basic_user_file \/etc\/nginx\/.htpasswd;\n}<\/code><\/pre>\n<\/li>\n<li><strong>\u7981\u7528 PHP \u6267\u884c<\/strong>\uff08\u7279\u5b9a\u76ee\u5f55\uff09\uff1a\n<pre><code class=\"language-nginx\">location ^~ \/uploads {\n   location ~ \\.php$ { deny all; }\n}<\/code><\/pre>\n<\/li>\n<\/ol>\n<h3>\u9a8c\u8bc1\u6b65\u9aa4<\/h3>\n<ol>\n<li>\u4fdd\u5b58\u914d\u7f6e\u5230 <code>\/etc\/nginx\/sites-available\/default<\/code><\/li>\n<li>\u6d4b\u8bd5\u914d\u7f6e\uff1a<code>sudo nginx -t<\/code><\/li>\n<li>\u91cd\u8f7d\u670d\u52a1\uff1a<code>sudo systemctl reload nginx<\/code><\/li>\n<li>\u521b\u5efa\u6d4b\u8bd5\u76ee\u5f55\uff1a\n<pre><code class=\"language-bash\">mkdir -p \/var\/www\/html\/{public,uploads}\ntouch \/var\/www\/html\/public\/{test.php,image.jpg}<\/code><\/pre>\n<\/li>\n<\/ol>\n<blockquote>\n<p>\u26a0\ufe0f \u6ce8\u610f\uff1a\u786e\u4fdd PHP-FPM \u670d\u52a1\u6b63\u5e38\u8fd0\u884c\uff0c\u4e14 sock \u6587\u4ef6\u8def\u5f84\u4e0e\u5b9e\u9645\u7248\u672c\u5339\u914d\uff08\u53ef\u901a\u8fc7 <code>ls \/var\/run\/php\/<\/code> \u67e5\u770b\uff09<\/p>\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u914d\u7f6e Nginx + PHP \u652f\u6301 HTTP \u76ee\u5f55\u5217\u8868\u7684\u793a\u4f8b\uff1a \u6838\u5fc3\u914d\u7f6e\u8bf4\u660e \u76ee\u5f55\u5217\u8868\uff1a\u4f7f\u7528 aut [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pmpro_default_level":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-304","post","type-post","status-publish","format-standard","hentry","category-uncategorized","pmpro-has-access"],"_links":{"self":[{"href":"https:\/\/ns1.koudaipc.com\/en\/wp-json\/wp\/v2\/posts\/304","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ns1.koudaipc.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ns1.koudaipc.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ns1.koudaipc.com\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ns1.koudaipc.com\/en\/wp-json\/wp\/v2\/comments?post=304"}],"version-history":[{"count":1,"href":"https:\/\/ns1.koudaipc.com\/en\/wp-json\/wp\/v2\/posts\/304\/revisions"}],"predecessor-version":[{"id":305,"href":"https:\/\/ns1.koudaipc.com\/en\/wp-json\/wp\/v2\/posts\/304\/revisions\/305"}],"wp:attachment":[{"href":"https:\/\/ns1.koudaipc.com\/en\/wp-json\/wp\/v2\/media?parent=304"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ns1.koudaipc.com\/en\/wp-json\/wp\/v2\/categories?post=304"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ns1.koudaipc.com\/en\/wp-json\/wp\/v2\/tags?post=304"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}