{"id":50038,"date":"2024-10-08T09:30:13","date_gmt":"2024-10-08T01:30:13","guid":{"rendered":"https:\/\/server.hk\/cnblog\/50038\/"},"modified":"2024-10-08T09:30:13","modified_gmt":"2024-10-08T01:30:13","slug":"nginx-%e6%8a%80%e5%b7%a7%ef%bc%9a%e6%9c%89%e6%95%88%e4%bd%bf%e7%94%a8%e4%bd%8d%e7%bd%ae%e5%a1%8a","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/50038\/","title":{"rendered":"Nginx \u6280\u5de7\uff1a\u6709\u6548\u4f7f\u7528\u4f4d\u7f6e\u584a"},"content":{"rendered":"<h1 id=\"nginx-%e6%8a%80%e5%b7%a7%ef%bc%9a%e6%9c%89%e6%95%88%e4%bd%bf%e7%94%a8%e4%bd%8d%e7%bd%ae%e5%a1%8a-zfmocIVGYl\">Nginx \u6280\u5de7\uff1a\u6709\u6548\u4f7f\u7528\u4f4d\u7f6e\u584a<\/h1>\n<p>Nginx \u662f\u4e00\u6b3e\u9ad8\u6548\u80fd\u7684\u7db2\u9801\u4f3a\u670d\u5668\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u975c\u614b\u5167\u5bb9\u7684\u63d0\u4f9b\u3001\u53cd\u5411\u4ee3\u7406\u3001\u8ca0\u8f09\u5747\u8861\u7b49\u591a\u7a2e\u5834\u666f\u3002\u5728 Nginx \u7684\u914d\u7f6e\u4e2d\uff0c\u4f4d\u7f6e\u584a\uff08location block\uff09\u662f\u7528\u4f86\u5b9a\u7fa9\u5982\u4f55\u8655\u7406\u7279\u5b9a\u8acb\u6c42\u7684\u91cd\u8981\u90e8\u5206\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u6709\u6548\u4f7f\u7528\u4f4d\u7f6e\u584a\uff0c\u4ee5\u63d0\u5347 Nginx \u7684\u6027\u80fd\u548c\u9748\u6d3b\u6027\u3002<\/p>\n<h2 id=\"%e4%bd%8d%e7%bd%ae%e5%a1%8a%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5-zfmocIVGYl\">\u4f4d\u7f6e\u584a\u7684\u57fa\u672c\u6982\u5ff5<\/h2>\n<p>\u4f4d\u7f6e\u584a\u7528\u65bc\u5339\u914d\u8acb\u6c42\u7684 URI\uff0c\u4e26\u6839\u64da\u5339\u914d\u7d50\u679c\u57f7\u884c\u76f8\u61c9\u7684\u914d\u7f6e\u3002\u4f4d\u7f6e\u584a\u7684\u57fa\u672c\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code>location [modifier] [URI] {\n    # \u914d\u7f6e\u6307\u4ee4\n}<\/code><\/pre>\n<p>\u5176\u4e2d\uff0cmodifier \u53ef\u4ee5\u662f\u4ee5\u4e0b\u5e7e\u7a2e\uff1a<\/p>\n<ul>\n<li><code>=<\/code>\uff1a\u7cbe\u78ba\u5339\u914d\uff0c\u53ea\u6709\u7576\u8acb\u6c42\u7684 URI \u5b8c\u5168\u7b49\u65bc\u6307\u5b9a\u7684 URI \u6642\uff0c\u624d\u6703\u4f7f\u7528\u6b64\u4f4d\u7f6e\u584a\u3002<\/li>\n<li><code>^~<\/code>\uff1a\u524d\u7db4\u5339\u914d\uff0c\u7576\u8acb\u6c42\u7684 URI \u4ee5\u6307\u5b9a\u7684 URI \u958b\u982d\u6642\uff0c\u5c07\u4f7f\u7528\u6b64\u4f4d\u7f6e\u584a\u3002<\/li>\n<li><code>~<\/code>\uff1a\u6b63\u5247\u8868\u9054\u5f0f\u5339\u914d\uff0c\u4f7f\u7528\u6b63\u5247\u8868\u9054\u5f0f\u4f86\u5339\u914d URI\u3002<\/li>\n<li><code>~*<\/code>\uff1a\u4e0d\u5340\u5206\u5927\u5c0f\u5beb\u7684\u6b63\u5247\u8868\u9054\u5f0f\u5339\u914d\u3002<\/li>\n<li>\u7121\u4fee\u98fe\u7b26\uff1a\u524d\u7db4\u5339\u914d\uff0c\u7576\u8acb\u6c42\u7684 URI \u4ee5\u6307\u5b9a\u7684 URI \u958b\u982d\u6642\uff0c\u5c07\u4f7f\u7528\u6b64\u4f4d\u7f6e\u584a\u3002<\/li>\n<\/ul>\n<h2 id=\"%e4%bd%8d%e7%bd%ae%e5%a1%8a%e7%9a%84%e4%bd%bf%e7%94%a8%e6%8a%80%e5%b7%a7-zfmocIVGYl\">\u4f4d\u7f6e\u584a\u7684\u4f7f\u7528\u6280\u5de7<\/h2>\n<h3 id=\"1-%e7%b2%be%e7%a2%ba%e5%8c%b9%e9%85%8d%e8%88%87%e5%89%8d%e7%b6%b4%e5%8c%b9%e9%85%8d-zfmocIVGYl\">1. \u7cbe\u78ba\u5339\u914d\u8207\u524d\u7db4\u5339\u914d<\/h3>\n<p>\u5728\u914d\u7f6e Nginx \u6642\uff0c\u61c9\u6839\u64da\u5be6\u969b\u9700\u6c42\u9078\u64c7\u5408\u9069\u7684\u5339\u914d\u65b9\u5f0f\u3002\u5c0d\u65bc\u975c\u614b\u8cc7\u6e90\uff0c\u5982\u5716\u7247\u6216 CSS \u6587\u4ef6\uff0c\u4f7f\u7528\u524d\u7db4\u5339\u914d\u53ef\u4ee5\u63d0\u9ad8\u6027\u80fd\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>location \/images\/ {\n    root \/var\/www\/html;\n}<\/code><\/pre>\n<p>\u9019\u6a23\u914d\u7f6e\u5f8c\uff0c\u6240\u6709\u4ee5 \/images\/ \u958b\u982d\u7684\u8acb\u6c42\u90fd\u6703\u88ab\u6307\u5411 \/var\/www\/html\/images\/ \u76ee\u9304\u3002<\/p>\n<h3 id=\"2-%e4%bd%bf%e7%94%a8%e6%ad%a3%e5%89%87%e8%a1%a8%e9%81%94%e5%bc%8f-zfmocIVGYl\">2. \u4f7f\u7528\u6b63\u5247\u8868\u9054\u5f0f<\/h3>\n<p>\u6b63\u5247\u8868\u9054\u5f0f\u5339\u914d\u63d0\u4f9b\u4e86\u66f4\u5927\u7684\u9748\u6d3b\u6027\uff0c\u4f46\u4e5f\u53ef\u80fd\u5f71\u97ff\u6027\u80fd\u3002\u56e0\u6b64\uff0c\u61c9\u8b39\u614e\u4f7f\u7528\u3002\u4f8b\u5982\uff0c\u82e5\u8981\u5339\u914d\u6240\u6709\u4ee5 .php \u7d50\u5c3e\u7684\u8acb\u6c42\uff0c\u53ef\u4ee5\u9019\u6a23\u914d\u7f6e\uff1a<\/p>\n<pre><code>location ~ .php$ {\n    include fastcgi_params;\n    fastcgi_pass 127.0.0.1:9000;\n}<\/code><\/pre>\n<h3 id=\"3-%e4%bd%8d%e7%bd%ae%e5%a1%8a%e7%9a%84%e5%84%aa%e5%85%88%e7%b4%9a-zfmocIVGYl\">3. \u4f4d\u7f6e\u584a\u7684\u512a\u5148\u7d1a<\/h3>\n<p>Nginx \u5728\u8655\u7406\u8acb\u6c42\u6642\u6703\u6839\u64da\u4f4d\u7f6e\u584a\u7684\u512a\u5148\u7d1a\u9032\u884c\u5339\u914d\u3002\u512a\u5148\u7d1a\u5f9e\u9ad8\u5230\u4f4e\u4f9d\u6b21\u70ba\uff1a<\/p>\n<ul>\n<li>\u7cbe\u78ba\u5339\u914d\uff08=\uff09<\/li>\n<li>\u524d\u7db4\u5339\u914d\uff08^~\uff09<\/li>\n<li>\u6b63\u5247\u8868\u9054\u5f0f\u5339\u914d\uff08~ \u548c ~*\uff09<\/li>\n<li>\u7121\u4fee\u98fe\u7b26\u7684\u524d\u7db4\u5339\u914d<\/li>\n<\/ul>\n<p>\u56e0\u6b64\uff0c\u82e5\u6709\u591a\u500b\u4f4d\u7f6e\u584a\u5339\u914d\u540c\u4e00\u8acb\u6c42\uff0cNginx \u5c07\u9078\u64c7\u512a\u5148\u7d1a\u6700\u9ad8\u7684\u90a3\u4e00\u500b\u3002<\/p>\n<h3 id=\"4-%e4%bd%bf%e7%94%a8%e4%bd%8d%e7%bd%ae%e5%a1%8a%e9%80%b2%e8%a1%8c%e9%87%8d%e5%ae%9a%e5%90%91-zfmocIVGYl\">4. \u4f7f\u7528\u4f4d\u7f6e\u584a\u9032\u884c\u91cd\u5b9a\u5411<\/h3>\n<p>\u4f4d\u7f6e\u584a\u9084\u53ef\u4ee5\u7528\u65bc URL \u91cd\u5b9a\u5411\u3002\u4f8b\u5982\uff0c\u5c07\u6240\u6709 HTTP \u8acb\u6c42\u91cd\u5b9a\u5411\u5230 HTTPS\uff1a<\/p>\n<pre><code>server {\n    listen 80;\n    server_name example.com;\n    \n    location \/ {\n        return 301 https:\/\/$host$request_uri;\n    }\n}<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-zfmocIVGYl\">\u7e3d\u7d50<\/h2>\n<p>\u6709\u6548\u4f7f\u7528 Nginx \u7684\u4f4d\u7f6e\u584a\u53ef\u4ee5\u986f\u8457\u63d0\u5347\u4f3a\u670d\u5668\u7684\u6027\u80fd\u548c\u9748\u6d3b\u6027\u3002\u900f\u904e\u7cbe\u78ba\u5339\u914d\u3001\u524d\u7db4\u5339\u914d\u548c\u6b63\u5247\u8868\u9054\u5f0f\u7684\u5408\u7406\u904b\u7528\uff0c\u60a8\u53ef\u4ee5\u66f4\u597d\u5730\u7ba1\u7406\u8acb\u6c42\u4e26\u512a\u5316\u8cc7\u6e90\u7684\u63d0\u4f9b\u3002\u82e5\u60a8\u9700\u8981\u9032\u4e00\u6b65\u4e86\u89e3\u5982\u4f55\u914d\u7f6e Nginx \u6216\u5c0b\u627e\u5408\u9069\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2f VPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u4ee5\u7372\u53d6\u66f4\u591a\u8cc7\u8a0a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22 Nginx \u6280\u5de7\uff0c\u5b78\u7fd2\u5982\u4f55\u6709\u6548\u4f7f\u7528\u4f4d\u7f6e\u584a\u4f86\u512a\u5316\u7db2\u7ad9\u6027\u80fd\u548c\u7ba1\u7406\u8acb\u6c42\u8def\u7531\u3002\u63d0\u5347\u4f60\u7684\u4f3a\u670d\u5668\u914d\u7f6e\uff01<\/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-50038","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/50038","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=50038"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/50038\/revisions"}],"predecessor-version":[{"id":50039,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/50038\/revisions\/50039"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=50038"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=50038"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=50038"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}