{"id":51103,"date":"2024-10-08T16:06:59","date_gmt":"2024-10-08T08:06:59","guid":{"rendered":"https:\/\/server.hk\/cnblog\/51103\/"},"modified":"2024-10-08T16:06:59","modified_gmt":"2024-10-08T08:06:59","slug":"%e5%be%b9%e5%ba%95%e6%90%9e%e6%87%82-nginx-%e4%ba%94%e5%a4%a7%e6%87%89%e7%94%a8%e5%a0%b4%e6%99%af","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/51103\/","title":{"rendered":"\u5fb9\u5e95\u641e\u61c2 Nginx \u4e94\u5927\u61c9\u7528\u5834\u666f"},"content":{"rendered":"<h1 id=\"%e5%be%b9%e5%ba%95%e6%90%9e%e6%87%82-nginx-%e4%ba%94%e5%a4%a7%e6%87%89%e7%94%a8%e5%a0%b4%e6%99%af-TKZvAArEWa\">\u5fb9\u5e95\u641e\u61c2 Nginx \u4e94\u5927\u61c9\u7528\u5834\u666f<\/h1>\n<p>Nginx \u662f\u4e00\u6b3e\u9ad8\u6548\u80fd\u7684\u7db2\u9801\u4f3a\u670d\u5668\uff0c\u56e0\u5176\u8f15\u91cf\u7d1a\u548c\u9ad8\u6548\u80fd\u7684\u7279\u6027\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u5404\u7a2e\u7db2\u8def\u74b0\u5883\u4e2d\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e Nginx \u7684\u4e94\u5927\u61c9\u7528\u5834\u666f\uff0c\u5e6b\u52a9\u8b80\u8005\u66f4\u597d\u5730\u7406\u89e3\u5176\u529f\u80fd\u548c\u512a\u52e2\u3002<\/p>\n<h2 id=\"1-%e9%9d%9c%e6%85%8b%e5%85%a7%e5%ae%b9%e4%bc%ba%e6%9c%8d%e5%99%a8-TKZvAArEWa\">1. \u975c\u614b\u5167\u5bb9\u4f3a\u670d\u5668<\/h2>\n<p>Nginx \u6700\u5e38\u898b\u7684\u61c9\u7528\u4e4b\u4e00\u662f\u4f5c\u70ba\u975c\u614b\u5167\u5bb9\u4f3a\u670d\u5668\u3002\u975c\u614b\u5167\u5bb9\u5305\u62ec HTML \u6587\u4ef6\u3001CSS\u3001JavaScript \u548c\u5716\u7247\u7b49\u3002\u7531\u65bc Nginx \u7684\u4e8b\u4ef6\u9a45\u52d5\u67b6\u69cb\uff0c\u5b83\u80fd\u5920\u9ad8\u6548\u5730\u8655\u7406\u5927\u91cf\u7684\u4e26\u767c\u8acb\u6c42\uff0c\u9019\u4f7f\u5f97\u5b83\u6210\u70ba\u975c\u614b\u5167\u5bb9\u4f3a\u670d\u7684\u7406\u60f3\u9078\u64c7\u3002<\/p>\n<pre><code>server {\n    listen 80;\n    server_name example.com;\n    location \/ {\n        root \/var\/www\/html;\n        index index.html index.htm;\n    }\n}<\/code><\/pre>\n<p>\u4e0a\u8ff0\u914d\u7f6e\u793a\u7bc4\u4e86\u5982\u4f55\u8a2d\u7f6e Nginx \u4f86\u63d0\u4f9b\u975c\u614b\u5167\u5bb9\u3002\u7576\u7528\u6236\u8a2a\u554f\u8a72\u4f3a\u670d\u5668\u6642\uff0cNginx \u5c07\u5f9e\u6307\u5b9a\u7684\u76ee\u9304\u4e2d\u63d0\u4f9b\u975c\u614b\u6587\u4ef6\u3002<\/p>\n<h2 id=\"2-%e5%8f%8d%e5%90%91%e4%bb%a3%e7%90%86%e4%bc%ba%e6%9c%8d%e5%99%a8-TKZvAArEWa\">2. \u53cd\u5411\u4ee3\u7406\u4f3a\u670d\u5668<\/h2>\n<p>Nginx \u4e5f\u5e38\u7528\u4f5c\u53cd\u5411\u4ee3\u7406\u4f3a\u670d\u5668\uff0c\u9019\u610f\u5473\u8457\u5b83\u53ef\u4ee5\u63a5\u6536\u5ba2\u6236\u7aef\u7684\u8acb\u6c42\u4e26\u5c07\u5176\u8f49\u767c\u5230\u5f8c\u7aef\u4f3a\u670d\u5668\u3002\u9019\u7a2e\u67b6\u69cb\u53ef\u4ee5\u63d0\u9ad8\u5b89\u5168\u6027\u548c\u6027\u80fd\uff0c\u4e26\u4e14\u80fd\u5920\u5be6\u73fe\u8ca0\u8f09\u5747\u8861\u3002<\/p>\n<pre><code>server {\n    listen 80;\n    server_name example.com;\n    location \/ {\n        proxy_pass http:\/\/backend_server;\n        proxy_set_header Host $host;\n        proxy_set_header X-Real-IP $remote_addr;\n    }\n}<\/code><\/pre>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0cNginx \u5c07\u6240\u6709\u8acb\u6c42\u8f49\u767c\u5230\u540d\u70ba backend_server \u7684\u5f8c\u7aef\u4f3a\u670d\u5668\uff0c\u4e26\u4fdd\u7559\u539f\u59cb\u8acb\u6c42\u7684\u76f8\u95dc\u4fe1\u606f\u3002<\/p>\n<h2 id=\"3-%e8%b2%a0%e8%bc%89%e5%9d%87%e8%a1%a1-TKZvAArEWa\">3. \u8ca0\u8f09\u5747\u8861<\/h2>\n<p>Nginx \u4e5f\u53ef\u4ee5\u7528\u4f5c\u8ca0\u8f09\u5747\u8861\u5668\uff0c\u5c07\u8acb\u6c42\u5206\u914d\u5230\u591a\u500b\u5f8c\u7aef\u4f3a\u670d\u5668\u4e0a\uff0c\u4ee5\u63d0\u9ad8\u61c9\u7528\u7684\u53ef\u7528\u6027\u548c\u6027\u80fd\u3002Nginx \u652f\u6301\u591a\u7a2e\u8ca0\u8f09\u5747\u8861\u7b97\u6cd5\uff0c\u5982\u8f2a\u8a62\u3001\u6700\u5c11\u9023\u63a5\u548c IP \u54c8\u5e0c\u7b49\u3002<\/p>\n<pre><code>http {\n    upstream backend {\n        server backend1.example.com;\n        server backend2.example.com;\n    }\n    \n    server {\n        listen 80;\n        location \/ {\n            proxy_pass http:\/\/backend;\n        }\n    }\n}<\/code><\/pre>\n<p>\u9019\u6bb5\u914d\u7f6e\u5c55\u793a\u4e86\u5982\u4f55\u8a2d\u7f6e Nginx \u9032\u884c\u8ca0\u8f09\u5747\u8861\uff0c\u5c07\u8acb\u6c42\u5206\u914d\u5230\u591a\u500b\u5f8c\u7aef\u4f3a\u670d\u5668\u3002<\/p>\n<h2 id=\"4-ssl-tls-%e7%b5%82%e7%ab%af-TKZvAArEWa\">4. SSL\/TLS \u7d42\u7aef<\/h2>\n<p>Nginx \u4e5f\u53ef\u4ee5\u4f5c\u70ba SSL\/TLS \u7d42\u7aef\uff0c\u8ca0\u8cac\u8655\u7406 HTTPS \u8acb\u6c42\u3002\u9019\u6a23\u53ef\u4ee5\u6e1b\u8f15\u5f8c\u7aef\u4f3a\u670d\u5668\u7684\u8ca0\u64d4\uff0c\u4e26\u63d0\u9ad8\u5b89\u5168\u6027\u3002<\/p>\n<pre><code>server {\n    listen 443 ssl;\n    server_name example.com;\n\n    ssl_certificate \/path\/to\/certificate.crt;\n    ssl_certificate_key \/path\/to\/private.key;\n\n    location \/ {\n        proxy_pass http:\/\/backend_server;\n    }\n}<\/code><\/pre>\n<p>\u5728\u9019\u500b\u914d\u7f6e\u4e2d\uff0cNginx \u5c07\u8655\u7406 SSL\/TLS \u52a0\u5bc6\uff0c\u4e26\u5c07\u8acb\u6c42\u8f49\u767c\u5230\u5f8c\u7aef\u4f3a\u670d\u5668\u3002<\/p>\n<h2 id=\"5-%e7%b6%b2%e7%ab%99%e9%98%b2%e7%81%ab%e7%89%86-TKZvAArEWa\">5. \u7db2\u7ad9\u9632\u706b\u7246<\/h2>\n<p>Nginx \u9084\u53ef\u4ee5\u7528\u4f5c\u7db2\u7ad9\u9632\u706b\u7246\uff0c\u901a\u904e\u8a2d\u7f6e\u8a2a\u554f\u63a7\u5236\u4f86\u4fdd\u8b77\u7db2\u7ad9\u514d\u53d7\u60e1\u610f\u653b\u64ca\u3002\u53ef\u4ee5\u6839\u64da IP \u5730\u5740\u3001\u8acb\u6c42\u65b9\u6cd5\u7b49\u9032\u884c\u904e\u6ffe\u3002<\/p>\n<pre><code>server {\n    listen 80;\n    server_name example.com;\n\n    location \/ {\n        deny 192.168.1.1;  # \u62d2\u7d55\u7279\u5b9a IP\n        allow all;         # \u5141\u8a31\u6240\u6709\u5176\u4ed6 IP\n    }\n}<\/code><\/pre>\n<p>\u9019\u6bb5\u914d\u7f6e\u793a\u7bc4\u4e86\u5982\u4f55\u4f7f\u7528 Nginx \u4f86\u9650\u5236\u7279\u5b9a IP \u7684\u8a2a\u554f\uff0c\u5f9e\u800c\u589e\u5f37\u7db2\u7ad9\u7684\u5b89\u5168\u6027\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-TKZvAArEWa\">\u7e3d\u7d50<\/h2>\n<p>Nginx \u662f\u4e00\u6b3e\u529f\u80fd\u5f37\u5927\u7684\u4f3a\u670d\u5668\uff0c\u9069\u7528\u65bc\u591a\u7a2e\u61c9\u7528\u5834\u666f\uff0c\u5305\u62ec\u975c\u614b\u5167\u5bb9\u4f3a\u670d\u3001\u53cd\u5411\u4ee3\u7406\u3001\u8ca0\u8f09\u5747\u8861\u3001SSL\/TLS \u7d42\u7aef\u548c\u7db2\u7ad9\u9632\u706b\u7246\u7b49\u3002\u7121\u8ad6\u662f\u5c0f\u578b\u7db2\u7ad9\u9084\u662f\u5927\u578b\u61c9\u7528\uff0cNginx \u90fd\u80fd\u63d0\u4f9b\u9ad8\u6548\u7684\u89e3\u6c7a\u65b9\u6848\u3002\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u53ef\u9760\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u670d\u52d9\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6df1\u5165\u4e86\u89e3 Nginx \u7684\u4e94\u5927\u61c9\u7528\u5834\u666f\uff0c\u638c\u63e1\u5176\u5728\u7db2\u7ad9\u6027\u80fd\u3001\u8ca0\u8f09\u5747\u8861\u548c\u5b89\u5168\u6027\u7b49\u65b9\u9762\u7684\u6700\u4f73\u5be6\u8e10\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-51103","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/51103","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=51103"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/51103\/revisions"}],"predecessor-version":[{"id":51104,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/51103\/revisions\/51104"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=51103"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=51103"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=51103"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}