{"id":61582,"date":"2025-04-29T09:14:45","date_gmt":"2025-04-29T01:14:45","guid":{"rendered":"https:\/\/fwq.ai\/blog\/61582\/"},"modified":"2025-04-29T09:14:45","modified_gmt":"2025-04-29T01:14:45","slug":"%e7%a7%92%e6%87%82nginx%e9%85%8d%e7%bd%ae%e6%95%99%e7%a8%8b%ef%bc%8c%e5%bf%ab%e9%80%9f%e4%b8%8a%e6%89%8b%e5%bc%80%e5%8f%91%e7%bd%91%e7%ab%99-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/61582\/","title":{"rendered":"\u79d2\u61c2nginx\u914d\u7f6e\u6559\u7a0b\uff0c\u5feb\u901f\u4e0a\u624b\u5f00\u53d1\u7f51\u7ad9"},"content":{"rendered":"<p>\u79d2\u61c2\u914d\u7f6e\u6559\u7a0b\uff0c\u5feb\u901f\u4e0a\u624b\u5f00\u53d1\u7f51\u7ad9<\/p>\n<p>\u6700\u8fd1\uff0c\u8d8a\u6765\u8d8a\u591a\u7684\u5f00\u53d1\u8005\u9009\u62e9\u4f7f\u7528nginx\u4f5c\u4e3a\u7f51\u7ad9\u5f00\u53d1\u7684\u4ee3\u7406\u670d\u52a1\u5668\u3002nginx\u4ee5\u5176\u9ad8\u6027\u80fd\u3001\u4f4e\u8d44\u6e90\u6d88\u8017\u548c\u7075\u6d3b\u7684\u914d\u7f6e\uff0c\u6210\u4e3a\u4e86\u5f88\u591a\u4eba\u5fc3\u76ee\u4e2d\u7684\u9996\u9009\u3002\u4f46\u662f\u5bf9\u4e8e\u65b0\u624b\u6765\u8bf4\uff0c\u914d\u7f6enginx\u53ef\u80fd\u4f1a\u6709\u4e00\u5b9a\u7684\u96be\u5ea6\u3002\u672c\u6587\u5c06\u5e26\u4f60\u4e00\u6b65\u6b65\u7406\u89e3nginx\u914d\u7f6e\uff0c\u5e76\u901a\u8fc7\u4ee3\u7801\u793a\u4f8b\u5feb\u901f\u4e0a\u624b\u5f00\u53d1\u7f51\u7ad9\u3002<\/p>\n<p>\u4e00\u3001\u5b89\u88c5nginx<\/p>\n<p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5728\u4f60\u7684\u7535\u8111\u6216\u670d\u52a1\u5668\u4e0a\u5b89\u88c5nginx\u3002\u5177\u4f53\u7684\u6b65\u9aa4\u56e0\u64cd\u4f5c\u7cfb\u7edf\u800c\u5f02\uff0c\u8fd9\u91cc\u4ee5Ubuntu Linux\u4e3a\u4f8b\uff1a<\/p>\n<ol>\n<li>\u6253\u5f00\u7ec8\u7aef\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5nginx\uff1a<\/li>\n<\/ol>\n<pre>sudo apt-get update\nsudo apt-get install nginx<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u542f\u52a8nginx\u670d\u52a1\uff1a<\/li>\n<\/ol>\n<pre>sudo service nginx start<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li>\u6253\u5f00\u6d4f\u89c8\u5668\uff0c\u8f93\u5165http:\/\/localhost\uff0c\u5982\u679c\u80fd\u770b\u5230nginx\u7684\u6b22\u8fce\u9875\u9762\uff0c\u5219\u8bf4\u660enginx\u5b89\u88c5\u6210\u529f\u3002<\/li>\n<\/ol>\n<p>\u4e8c\u3001\u914d\u7f6enginx<\/p>\n<p>nginx\u7684\u914d\u7f6e\u6587\u4ef6\u4f4d\u4e8e\/etc\/nginx\/nginx.conf\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4efb\u4f55\u6587\u672c\u7f16\u8f91\u5668\u6253\u5f00\u5b83\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u7684nginx\u914d\u7f6e\u793a\u4f8b\uff1a<\/p>\n<pre>worker_processes 1;\n\nevents {\n    worker_connections 1024;\n}\n\nhttp {\n    server {\n        listen 80;\n        server_name localhost;\n        \n        location \/ {\n            root \/var\/www\/html;\n            index index.html;\n        }\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u914d\u7f6e\uff1a<\/p>\n<ul>\n<li>worker_processes\u8868\u793anginx\u4f7f\u7528\u7684\u8fdb\u7a0b\u6570\uff0c\u901a\u5e38\u8bbe\u7f6e\u4e3aCPU\u6838\u5fc3\u6570\u76842\u500d\u3002<\/li>\n<li>worker_connections\u8868\u793a\u6bcf\u4e2a\u8fdb\u7a0b\u7684\u6700\u5927\u8fde\u63a5\u6570\u3002<\/li>\n<li>http\u5757\u7528\u4e8e\u914d\u7f6eHTTP\u670d\u52a1\uff0c\u9ed8\u8ba4\u76d1\u542c80\u7aef\u53e3\u3002<\/li>\n<li>server\u5757\u7528\u4e8e\u914d\u7f6e\u4e00\u4e2a\u865a\u62df\u4e3b\u673a\uff0c\u53ef\u4ee5\u6709\u591a\u4e2a\u3002<\/li>\n<li>listen\u6307\u5b9a\u76d1\u542c\u7684\u7aef\u53e3\u3002<\/li>\n<li>server_name\u6307\u5b9a\u865a\u62df\u4e3b\u673a\u7684\u57df\u540d\u3002<\/li>\n<li>location\u5757\u7528\u4e8e\u914d\u7f6e\u8bf7\u6c42\u7684\u8def\u7531\u548c\u5904\u7406\u65b9\u5f0f\u3002<\/li>\n<\/ul>\n<p>\u4e09\u3001\u5f00\u53d1\u7f51\u7ad9<\/p>\n<ol>\n<li>\u5728\/var\/www\/html\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3aexample.com\u7684\u6587\u4ef6\u5939\uff0c\u7528\u4e8e\u5b58\u653e\u7f51\u7ad9\u6587\u4ef6\u3002<\/li>\n<li>\u5728example.com\u6587\u4ef6\u5939\u4e2d\u521b\u5efa\u4e00\u4e2a\u540d\u4e3aindex.html\u7684\u6587\u4ef6\uff0c\u8f93\u5165\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/li>\n<\/ol>\n<pre>\n\n\n    &lt;title&gt;Welcome to example.com&lt;\/title&gt;&lt;h1&gt;Hello, world!&lt;\/h1&gt;\n\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li>\u6253\u5f00\u7ec8\u7aef\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u91cd\u65b0\u52a0\u8f7dnginx\u7684\u914d\u7f6e\u6587\u4ef6\uff1a<\/li>\n<\/ol>\n<pre>sudo service nginx reload<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li>\u6253\u5f00\u6d4f\u89c8\u5668\uff0c\u8f93\u5165http:\/\/localhost\u6216http:\/\/example.com\uff0c\u4f60\u5c06\u770b\u5230\u521a\u624d\u521b\u5efa\u7684\u7f51\u9875\u3002<\/li>\n<\/ol>\n<p>\u56db\u3001\u8fdb\u4e00\u6b65\u914d\u7f6e<\/p>\n<p>\u9664\u4e86\u57fa\u672c\u914d\u7f6e\u5916\uff0cnginx\u8fd8\u6709\u5f88\u591a\u529f\u80fd\u5f3a\u5927\u7684\u6a21\u5757\u53ef\u4ee5\u4f7f\u7528\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u914d\u7f6e\u793a\u4f8b\uff1a<\/p>\n<ol>\n<li>\u9759\u6001\u6587\u4ef6\u7f13\u5b58<\/li>\n<\/ol>\n<pre>http {\n    server {\n        ...\n        \n        location \/static {\n            root \/var\/www\/html;\n            expires 7d;\n        }\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u914d\u7f6e\u5c06\/var\/www\/html\/static\u76ee\u5f55\u4e0b\u7684\u9759\u6001\u6587\u4ef6\u8fdb\u884c\u7f13\u5b58\uff0c\u6709\u6548\u671f\u4e3a7\u5929\u3002<\/p>\n<ol>\n<li>\u53cd\u5411\u4ee3\u7406<\/li>\n<\/ol>\n<pre>http {\n    server {\n        ...\n        \n        location \/api {\n            proxy_pass http:\/\/api.example.com;\n        }\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u914d\u7f6e\u5c06\u5339\u914d\/api\u8def\u5f84\u7684\u8bf7\u6c42\u8f6c\u53d1\u7ed9http:\/\/api.example.com\u3002<\/p>\n<ol>\n<li>\u8d1f\u8f7d\u5747\u8861<\/li>\n<\/ol>\n<pre>http {\n    upstream backend {\n        server backend1.example.com;\n        server backend2.example.com;\n    }\n    \n    server {\n        ...\n        \n        location \/ {\n            proxy_pass http:\/\/backend;\n        }\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u914d\u7f6e\u5c06\u8bf7\u6c42\u5206\u53d1\u7ed9backend\u4e2d\u5b9a\u4e49\u7684\u591a\u4e2a\u540e\u7aef\u670d\u52a1\u5668\uff0c\u5b9e\u73b0\u8d1f\u8f7d\u5747\u8861\u3002<\/p>\n<p>\u4e94\u3001\u603b\u7ed3<\/p>\n<p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u76f8\u4fe1\u5927\u5bb6\u5df2\u7ecf\u5bf9nginx\u7684\u914d\u7f6e\u6709\u4e86\u4e00\u5b9a\u7684\u4e86\u89e3\uff0c\u5e76\u80fd\u591f\u5feb\u901f\u4e0a\u624b\u5f00\u53d1\u7f51\u7ad9\u3002\u5f53\u7136\uff0cnginx\u7684\u529f\u80fd\u8fdc\u4e0d\u6b62\u4e8e\u6b64\uff0c\u8fd8\u6709\u5f88\u591a\u9ad8\u7ea7\u7279\u6027\u548c\u63d2\u4ef6\u7b49\u5f85\u4f60\u53bb\u63a2\u7d22\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff0c\u795d\u4f60\u5728nginx\u7684\u4e16\u754c\u4e2d\u5f00\u53d1\u51fa\u4e00\u6d41\u7684\u7f51\u7ad9\uff01<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u79d2\u61c2\u914d\u7f6e\u6559\u7a0b\uff0c\u5feb\u901f\u4e0a\u624b\u5f00\u53d1\u7f51\u7ad9\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8FDCServers\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u79d2\u61c2\u914d\u7f6e\u6559\u7a0b\uff0c\u5feb\u901f\u4e0a\u624b\u5f00\u53d1\u7f51\u7ad9 \u6700\u8fd1\uff0c\u8d8a\u6765\u8d8a\u591a\u7684\u5f00\u53d1\u8005\u9009\u62e9\u4f7f\u7528nginx\u4f5c\u4e3a\u7f51\u7ad9\u5f00\u53d1\u7684\u4ee3\u7406\u670d\u52a1\u5668\u3002nginx\u4ee5\u5176\u9ad8\u6027\u80fd\u3001\u4f4e\u8d44\u6e90\u6d88\u8017\u548c\u7075\u6d3b\u7684\u914d\u7f6e\uff0c\u6210\u4e3a\u4e86\u5f88\u591a\u4eba\u5fc3\u76ee\u4e2d\u7684\u9996\u9009\u3002\u4f46\u662f\u5bf9\u4e8e\u65b0\u624b\u6765\u8bf4\uff0c\u914d\u7f6enginx\u53ef\u80fd\u4f1a\u6709\u4e00\u5b9a\u7684\u96be\u5ea6\u3002\u672c\u6587\u5c06\u5e26\u4f60\u4e00\u6b65\u6b65\u7406\u89e3nginx\u914d\u7f6e\uff0c\u5e76\u901a\u8fc7\u4ee3\u7801\u793a\u4f8b\u5feb\u901f\u4e0a\u624b\u5f00\u53d1\u7f51\u7ad9\u3002 \u4e00\u3001\u5b89\u88c5nginx \u9996\u5148\uff0c\u4f60\u9700\u8981\u5728\u4f60\u7684\u7535\u8111\u6216\u670d\u52a1\u5668\u4e0a\u5b89\u88c5nginx\u3002\u5177\u4f53\u7684\u6b65\u9aa4\u56e0\u64cd\u4f5c\u7cfb\u7edf\u800c\u5f02\uff0c\u8fd9\u91cc\u4ee5Ubuntu Linux\u4e3a\u4f8b\uff1a \u6253\u5f00\u7ec8\u7aef\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5nginx\uff1a sudo apt-get update sudo apt-get install nginx \u767b\u5f55\u540e\u590d\u5236 \u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u542f\u52a8nginx\u670d\u52a1\uff1a sudo service nginx start \u767b\u5f55\u540e\u590d\u5236 \u6253\u5f00\u6d4f\u89c8\u5668\uff0c\u8f93\u5165http:\/\/localhost\uff0c\u5982\u679c\u80fd\u770b\u5230nginx\u7684\u6b22\u8fce\u9875\u9762\uff0c\u5219\u8bf4\u660enginx\u5b89\u88c5\u6210\u529f\u3002 \u4e8c\u3001\u914d\u7f6enginx nginx\u7684\u914d\u7f6e\u6587\u4ef6\u4f4d\u4e8e\/etc\/nginx\/nginx.conf\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4efb\u4f55\u6587\u672c\u7f16\u8f91\u5668\u6253\u5f00\u5b83\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u7684nginx\u914d\u7f6e\u793a\u4f8b\uff1a worker_processes 1; events { worker_connections 1024; } http { server { listen 80; server_name localhost; location \/ { root \/var\/www\/html; index index.html; } } } \u767b\u5f55\u540e\u590d\u5236 \u4ee5\u4e0a\u914d\u7f6e\uff1a worker_processes\u8868\u793anginx\u4f7f\u7528\u7684\u8fdb\u7a0b\u6570\uff0c\u901a\u5e38\u8bbe\u7f6e\u4e3aCPU\u6838\u5fc3\u6570\u76842\u500d\u3002 worker_connections\u8868\u793a\u6bcf\u4e2a\u8fdb\u7a0b\u7684\u6700\u5927\u8fde\u63a5\u6570\u3002 http\u5757\u7528\u4e8e\u914d\u7f6eHTTP\u670d\u52a1\uff0c\u9ed8\u8ba4\u76d1\u542c80\u7aef\u53e3\u3002 server\u5757\u7528\u4e8e\u914d\u7f6e\u4e00\u4e2a\u865a\u62df\u4e3b\u673a\uff0c\u53ef\u4ee5\u6709\u591a\u4e2a\u3002 listen\u6307\u5b9a\u76d1\u542c\u7684\u7aef\u53e3\u3002 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-61582","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/61582","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=61582"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/61582\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=61582"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=61582"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=61582"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}