{"id":49972,"date":"2024-10-08T09:01:41","date_gmt":"2024-10-08T01:01:41","guid":{"rendered":"https:\/\/server.hk\/cnblog\/49972\/"},"modified":"2024-10-08T09:01:41","modified_gmt":"2024-10-08T01:01:41","slug":"nginx-%e6%8a%80%e5%b7%a7%ef%bc%9a%e8%a8%ad%e7%bd%ae%e5%85%a7%e5%ae%b9%e5%88%86%e7%99%bc%e7%b6%b2%e7%b5%a1%ef%bc%88cdn%ef%bc%89%e4%bb%a5%e5%af%a6%e7%8f%be%e5%85%a8%e7%90%83%e8%a6%86%e8%93%8b","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/49972\/","title":{"rendered":"Nginx \u6280\u5de7\uff1a\u8a2d\u7f6e\u5167\u5bb9\u5206\u767c\u7db2\u7d61\uff08CDN\uff09\u4ee5\u5be6\u73fe\u5168\u7403\u8986\u84cb"},"content":{"rendered":"<h1 id=\"nginx-%e6%8a%80%e5%b7%a7%ef%bc%9a%e8%a8%ad%e7%bd%ae%e5%85%a7%e5%ae%b9%e5%88%86%e7%99%bc%e7%b6%b2%e7%b5%a1%ef%bc%88cdn%ef%bc%89%e4%bb%a5%e5%af%a6%e7%8f%be%e5%85%a8%e7%90%83%e8%a6%86%e8%93%8b-yqpuyByRrc\">Nginx \u6280\u5de7\uff1a\u8a2d\u7f6e\u5167\u5bb9\u5206\u767c\u7db2\u7d61\uff08CDN\uff09\u4ee5\u5be6\u73fe\u5168\u7403\u8986\u84cb<\/h1>\n<p>\u96a8\u8457\u4e92\u806f\u7db2\u7684\u8fc5\u901f\u767c\u5c55\uff0c\u7db2\u7ad9\u7684\u8a2a\u554f\u901f\u5ea6\u548c\u7a69\u5b9a\u6027\u8b8a\u5f97\u8d8a\u4f86\u8d8a\u91cd\u8981\u3002\u5167\u5bb9\u5206\u767c\u7db2\u7d61\uff08CDN\uff09\u662f\u4e00\u7a2e\u6709\u6548\u7684\u89e3\u6c7a\u65b9\u6848\uff0c\u53ef\u4ee5\u5e6b\u52a9\u7db2\u7ad9\u5728\u5168\u7403\u7bc4\u570d\u5167\u63d0\u4f9b\u66f4\u5feb\u7684\u5167\u5bb9\u4ea4\u4ed8\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528 Nginx \u8a2d\u7f6e CDN\uff0c\u4ee5\u5be6\u73fe\u5168\u7403\u8986\u84cb\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af%e5%85%a7%e5%ae%b9%e5%88%86%e7%99%bc%e7%b6%b2%e7%b5%a1%ef%bc%88cdn%ef%bc%89%ef%bc%9f-yqpuyByRrc\">\u4ec0\u9ebc\u662f\u5167\u5bb9\u5206\u767c\u7db2\u7d61\uff08CDN\uff09\uff1f<\/h2>\n<p>\u5167\u5bb9\u5206\u767c\u7db2\u7d61\uff08CDN\uff09\u662f\u4e00\u7d44\u5206\u4f48\u5728\u4e0d\u540c\u5730\u7406\u4f4d\u7f6e\u7684\u4f3a\u670d\u5668\uff0c\u65e8\u5728\u52a0\u901f\u7db2\u7ad9\u5167\u5bb9\u7684\u4ea4\u4ed8\u3002\u7576\u7528\u6236\u8a2a\u554f\u7db2\u7ad9\u6642\uff0cCDN \u6703\u6839\u64da\u7528\u6236\u7684\u5730\u7406\u4f4d\u7f6e\uff0c\u5c07\u8acb\u6c42\u8def\u7531\u5230\u6700\u8fd1\u7684\u4f3a\u670d\u5668\uff0c\u5f9e\u800c\u6e1b\u5c11\u5ef6\u9072\u548c\u63d0\u9ad8\u52a0\u8f09\u901f\u5ea6\u3002\u9019\u5c0d\u65bc\u9700\u8981\u8655\u7406\u5927\u91cf\u6d41\u91cf\u7684\u7db2\u7ad9\u5c24\u70ba\u91cd\u8981\u3002<\/p>\n<h2 id=\"nginx-%e4%bd%9c%e7%82%ba-cdn-%e7%9a%84%e5%84%aa%e5%8b%a2-yqpuyByRrc\">Nginx \u4f5c\u70ba CDN \u7684\u512a\u52e2<\/h2>\n<ul>\n<li><strong>\u9ad8\u6027\u80fd\uff1a<\/strong>Nginx \u4ee5\u5176\u9ad8\u6548\u7684\u4e8b\u4ef6\u9a45\u52d5\u67b6\u69cb\u800c\u805e\u540d\uff0c\u80fd\u5920\u8655\u7406\u5927\u91cf\u4e26\u767c\u9023\u63a5\u3002<\/li>\n<li><strong>\u9748\u6d3b\u6027\uff1a<\/strong>Nginx \u63d0\u4f9b\u4e86\u8c50\u5bcc\u7684\u914d\u7f6e\u9078\u9805\uff0c\u5141\u8a31\u7528\u6236\u6839\u64da\u9700\u6c42\u9032\u884c\u81ea\u5b9a\u7fa9\u8a2d\u7f6e\u3002<\/li>\n<li><strong>\u958b\u6e90\uff1a<\/strong>Nginx \u662f\u4e00\u500b\u958b\u6e90\u9805\u76ee\uff0c\u9019\u610f\u5473\u8457\u7528\u6236\u53ef\u4ee5\u81ea\u7531\u4f7f\u7528\u548c\u4fee\u6539\u5176\u6e90\u4ee3\u78bc\u3002<\/li>\n<\/ul>\n<h2 id=\"%e8%a8%ad%e7%bd%ae-nginx-%e4%bd%9c%e7%82%ba-cdn-%e7%9a%84%e6%ad%a5%e9%a9%9f-yqpuyByRrc\">\u8a2d\u7f6e Nginx \u4f5c\u70ba CDN \u7684\u6b65\u9a5f<\/h2>\n<h3 id=\"1-%e5%ae%89%e8%a3%9d-nginx-yqpuyByRrc\">1. \u5b89\u88dd Nginx<\/h3>\n<p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u5728\u4f3a\u670d\u5668\u4e0a\u5b89\u88dd Nginx\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5728 Ubuntu \u7cfb\u7d71\u4e0a\u5b89\u88dd\uff1a<\/p>\n<pre><code>sudo apt update\nsudo apt install nginx<\/code><\/pre>\n<h3 id=\"2-%e9%85%8d%e7%bd%ae-nginx-%e4%bb%a5%e6%94%af%e6%8c%81%e9%9d%9c%e6%85%8b%e5%85%a7%e5%ae%b9-yqpuyByRrc\">2. \u914d\u7f6e Nginx \u4ee5\u652f\u6301\u975c\u614b\u5167\u5bb9<\/h3>\n<p>\u5728 Nginx \u7684\u914d\u7f6e\u6587\u4ef6\u4e2d\uff0c\u60a8\u9700\u8981\u8a2d\u7f6e\u975c\u614b\u5167\u5bb9\u7684\u8def\u5f91\u3002\u6253\u958b Nginx \u914d\u7f6e\u6587\u4ef6\uff1a<\/p>\n<pre><code>sudo nano \/etc\/nginx\/sites-available\/default<\/code><\/pre>\n<p>\u7136\u5f8c\u6dfb\u52a0\u4ee5\u4e0b\u914d\u7f6e\uff1a<\/p>\n<pre><code>server {\n    listen 80;\n    server_name your_domain.com;\n\n    location \/ {\n        root \/var\/www\/html;\n        index index.html index.htm;\n    }\n\n    location ~* .(jpg|jpeg|png|gif|css|js|ico|html)$ {\n        expires 30d;\n        access_log off;\n    }\n}<\/code><\/pre>\n<p>\u9019\u6bb5\u914d\u7f6e\u5c07\u975c\u614b\u6587\u4ef6\u7684\u904e\u671f\u6642\u9593\u8a2d\u7f6e\u70ba 30 \u5929\uff0c\u5f9e\u800c\u6e1b\u5c11\u4f3a\u670d\u5668\u7684\u8ca0\u64d4\u3002<\/p>\n<h3 id=\"3-%e8%a8%ad%e7%bd%ae%e5%8f%8d%e5%90%91%e4%bb%a3%e7%90%86-yqpuyByRrc\">3. \u8a2d\u7f6e\u53cd\u5411\u4ee3\u7406<\/h3>\n<p>\u5982\u679c\u60a8\u5e0c\u671b Nginx \u4f5c\u70ba\u53cd\u5411\u4ee3\u7406\u4f86\u8f49\u767c\u8acb\u6c42\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u914d\u7f6e\uff1a<\/p>\n<pre><code>location \/api\/ {\n    proxy_pass http:\/\/backend_server;\n    proxy_set_header Host $host;\n    proxy_set_header X-Real-IP $remote_addr;\n    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n}<\/code><\/pre>\n<p>\u9019\u6bb5\u914d\u7f6e\u5c07\u6240\u6709\u4ee5 \/api\/ \u958b\u982d\u7684\u8acb\u6c42\u8f49\u767c\u5230\u5f8c\u7aef\u4f3a\u670d\u5668\u3002<\/p>\n<h3 id=\"4-%e5%95%9f%e7%94%a8%e7%b7%a9%e5%ad%98-yqpuyByRrc\">4. \u555f\u7528\u7de9\u5b58<\/h3>\n<p>\u70ba\u4e86\u9032\u4e00\u6b65\u63d0\u9ad8\u6027\u80fd\uff0c\u60a8\u53ef\u4ee5\u555f\u7528 Nginx \u7684\u7de9\u5b58\u529f\u80fd\u3002\u6dfb\u52a0\u4ee5\u4e0b\u914d\u7f6e\uff1a<\/p>\n<pre><code>proxy_cache_path \/tmp\/nginx_cache levels=1:2 keys_zone=my_cache:10m max_size=1g inactive=60m use_temp_path=off;\n\nlocation \/api\/ {\n    proxy_cache my_cache;\n    proxy_pass http:\/\/backend_server;\n}<\/code><\/pre>\n<p>\u9019\u5c07\u7de9\u5b58\u4f86\u81ea\u5f8c\u7aef\u4f3a\u670d\u5668\u7684\u97ff\u61c9\uff0c\u5f9e\u800c\u6e1b\u5c11\u5c0d\u5f8c\u7aef\u7684\u8acb\u6c42\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-yqpuyByRrc\">\u7e3d\u7d50<\/h2>\n<p>\u901a\u904e\u4ee5\u4e0a\u6b65\u9a5f\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 Nginx \u8a2d\u7f6e\u4e00\u500b\u9ad8\u6548\u7684\u5167\u5bb9\u5206\u767c\u7db2\u7d61\uff08CDN\uff09\uff0c\u4ee5\u5be6\u73fe\u5168\u7403\u8986\u84cb\u3002\u9019\u4e0d\u50c5\u80fd\u63d0\u9ad8\u7db2\u7ad9\u7684\u52a0\u8f09\u901f\u5ea6\uff0c\u9084\u80fd\u6539\u5584\u7528\u6236\u9ad4\u9a57\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6027\u80fd\u548c\u7a69\u5b9a\u6027\u7684\u7db2\u7ad9\uff0c\u9078\u64c7\u5408\u9069\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2f VPS<\/a> \u6216 <a href=\"https:\/\/server.hk\">\u96f2\u4f3a\u670d\u5668<\/a> \u662f\u81f3\u95dc\u91cd\u8981\u7684\u3002\u4e86\u89e3\u66f4\u591a\u95dc\u65bc <a href=\"https:\/\/server.hk\">\u4f3a\u670d\u5668<\/a> \u7684\u4fe1\u606f\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528 Nginx \u8a2d\u7f6e\u5167\u5bb9\u5206\u767c\u7db2\u7d61\uff08CDN\uff09\uff0c\u5be6\u73fe\u5168\u7403\u8986\u84cb\uff0c\u63d0\u5347\u7db2\u7ad9\u6027\u80fd\u8207\u7528\u6236\u9ad4\u9a57\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-49972","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/49972","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=49972"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/49972\/revisions"}],"predecessor-version":[{"id":49973,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/49972\/revisions\/49973"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=49972"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=49972"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=49972"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}