{"id":51093,"date":"2024-10-08T16:02:17","date_gmt":"2024-10-08T08:02:17","guid":{"rendered":"https:\/\/server.hk\/cnblog\/51093\/"},"modified":"2024-10-08T16:02:17","modified_gmt":"2024-10-08T08:02:17","slug":"%e5%8f%b2%e4%b8%8a%e6%9c%80%e5%85%a8%e7%9a%84-nginx-%e9%85%8d%e7%bd%ae%e5%8f%83%e6%95%b8%e4%b8%ad%e6%96%87%e8%aa%aa%e6%98%8e","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/51093\/","title":{"rendered":"\u53f2\u4e0a\u6700\u5168\u7684 Nginx \u914d\u7f6e\u53c3\u6578\u4e2d\u6587\u8aaa\u660e"},"content":{"rendered":"<h1 id=\"%e5%8f%b2%e4%b8%8a%e6%9c%80%e5%85%a8%e7%9a%84-nginx-%e9%85%8d%e7%bd%ae%e5%8f%83%e6%95%b8%e4%b8%ad%e6%96%87%e8%aa%aa%e6%98%8e-deWOIIpGBp\">\u53f2\u4e0a\u6700\u5168\u7684 Nginx \u914d\u7f6e\u53c3\u6578\u4e2d\u6587\u8aaa\u660e<\/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\u5176\u9748\u6d3b\u7684\u914d\u7f6e\u9078\u9805\u4f7f\u5f97\u7528\u6236\u80fd\u5920\u6839\u64da\u9700\u6c42\u9032\u884c\u8abf\u6574\u3002\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39 Nginx \u7684\u4e3b\u8981\u914d\u7f6e\u53c3\u6578\uff0c\u5e6b\u52a9\u7528\u6236\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528\u9019\u6b3e\u4f3a\u670d\u5668\u3002<\/p>\n<h2 id=\"%e5%9f%ba%e6%9c%ac%e9%85%8d%e7%bd%ae%e5%8f%83%e6%95%b8-deWOIIpGBp\">\u57fa\u672c\u914d\u7f6e\u53c3\u6578<\/h2>\n<h3 id=\"worker_processes-deWOIIpGBp\">worker_processes<\/h3>\n<p>\u6b64\u53c3\u6578\u7528\u65bc\u8a2d\u5b9a Nginx \u7684\u5de5\u4f5c\u9032\u7a0b\u6578\u91cf\u3002\u4e00\u822c\u5efa\u8b70\u8a2d\u7f6e\u70ba CPU \u6838\u5fc3\u6578\u7684\u5169\u500d\uff0c\u4ee5\u5145\u5206\u5229\u7528\u7cfb\u7d71\u8cc7\u6e90\u3002<\/p>\n<pre><code>worker_processes auto;<\/code><\/pre>\n<h3 id=\"worker_connections-deWOIIpGBp\">worker_connections<\/h3>\n<p>\u6b64\u53c3\u6578\u5b9a\u7fa9\u6bcf\u500b\u5de5\u4f5c\u9032\u7a0b\u53ef\u4ee5\u8655\u7406\u7684\u6700\u5927\u9023\u63a5\u6578\u3002\u6839\u64da\u4f3a\u670d\u5668\u7684\u6027\u80fd\u548c\u9700\u6c42\u9032\u884c\u8abf\u6574\u3002<\/p>\n<pre><code>worker_connections 1024;<\/code><\/pre>\n<h2 id=\"%e4%bc%ba%e6%9c%8d%e5%99%a8%e9%85%8d%e7%bd%ae-deWOIIpGBp\">\u4f3a\u670d\u5668\u914d\u7f6e<\/h2>\n<h3 id=\"server-deWOIIpGBp\">server<\/h3>\n<p>server \u5340\u584a\u7528\u65bc\u5b9a\u7fa9\u4e00\u500b\u865b\u64ec\u4f3a\u670d\u5668\u7684\u914d\u7f6e\u3002\u53ef\u4ee5\u5728\u6b64\u5340\u584a\u4e2d\u8a2d\u5b9a\u4f3a\u670d\u5668\u7684\u76e3\u807d\u7aef\u53e3\u3001\u57df\u540d\u7b49\u3002<\/p>\n<pre><code>server {\n    listen 80;\n    server_name example.com;\n}<\/code><\/pre>\n<h3 id=\"location-deWOIIpGBp\">location<\/h3>\n<p>location \u5340\u584a\u7528\u65bc\u5339\u914d\u8acb\u6c42\u7684 URI\uff0c\u4e26\u6839\u64da\u5339\u914d\u7d50\u679c\u57f7\u884c\u76f8\u61c9\u7684\u64cd\u4f5c\u3002\u53ef\u4ee5\u4f7f\u7528\u6b63\u5247\u8868\u9054\u5f0f\u9032\u884c\u66f4\u9748\u6d3b\u7684\u5339\u914d\u3002<\/p>\n<pre><code>location \/images\/ {\n    root \/var\/www\/images;\n}<\/code><\/pre>\n<h2 id=\"%e5%8f%8d%e5%90%91%e4%bb%a3%e7%90%86%e9%85%8d%e7%bd%ae-deWOIIpGBp\">\u53cd\u5411\u4ee3\u7406\u914d\u7f6e<\/h2>\n<h3 id=\"proxy_pass-deWOIIpGBp\">proxy_pass<\/h3>\n<p>\u6b64\u53c3\u6578\u7528\u65bc\u8a2d\u7f6e\u53cd\u5411\u4ee3\u7406\u7684\u76ee\u6a19\u5730\u5740\u3002\u7576 Nginx \u6536\u5230\u8acb\u6c42\u6642\uff0c\u6703\u5c07\u8acb\u6c42\u8f49\u767c\u5230\u6307\u5b9a\u7684\u5f8c\u7aef\u4f3a\u670d\u5668\u3002<\/p>\n<pre><code>location \/api\/ {\n    proxy_pass http:\/\/backend_server;\n}<\/code><\/pre>\n<h3 id=\"proxy_set_header-deWOIIpGBp\">proxy_set_header<\/h3>\n<p>\u6b64\u53c3\u6578\u7528\u65bc\u8a2d\u7f6e\u8f49\u767c\u8acb\u6c42\u6642\u7684 HTTP \u6a19\u982d\u3002\u53ef\u4ee5\u7528\u4f86\u50b3\u905e\u5ba2\u6236\u7aef\u7684 IP \u5730\u5740\u7b49\u4fe1\u606f\u3002<\/p>\n<pre><code>proxy_set_header Host $host;\nproxy_set_header X-Real-IP $remote_addr;<\/code><\/pre>\n<h2 id=\"%e5%ae%89%e5%85%a8%e6%80%a7%e9%85%8d%e7%bd%ae-deWOIIpGBp\">\u5b89\u5168\u6027\u914d\u7f6e<\/h2>\n<h3 id=\"ssl_certificate-deWOIIpGBp\">ssl_certificate<\/h3>\n<p>\u6b64\u53c3\u6578\u7528\u65bc\u6307\u5b9a SSL \u8b49\u66f8\u7684\u8def\u5f91\uff0c\u5be6\u73fe HTTPS \u52a0\u5bc6\u50b3\u8f38\u3002<\/p>\n<pre><code>server {\n    listen 443 ssl;\n    ssl_certificate \/etc\/ssl\/certs\/example.com.crt;\n    ssl_certificate_key \/etc\/ssl\/private\/example.com.key;\n}<\/code><\/pre>\n<h3 id=\"deny-deWOIIpGBp\">deny<\/h3>\n<p>\u6b64\u53c3\u6578\u7528\u65bc\u62d2\u7d55\u7279\u5b9a IP \u5730\u5740\u7684\u8a2a\u554f\uff0c\u589e\u5f37\u4f3a\u670d\u5668\u7684\u5b89\u5168\u6027\u3002<\/p>\n<pre><code>location \/ {\n    deny 192.168.1.1;\n}<\/code><\/pre>\n<h2 id=\"%e6%80%a7%e8%83%bd%e5%84%aa%e5%8c%96%e5%8f%83%e6%95%b8-deWOIIpGBp\">\u6027\u80fd\u512a\u5316\u53c3\u6578<\/h2>\n<h3 id=\"gzip-deWOIIpGBp\">gzip<\/h3>\n<p>\u555f\u7528 Gzip \u58d3\u7e2e\u53ef\u4ee5\u6e1b\u5c11\u50b3\u8f38\u7684\u6578\u64da\u91cf\uff0c\u63d0\u9ad8\u52a0\u8f09\u901f\u5ea6\u3002<\/p>\n<pre><code>gzip on;\ngzip_types text\/plain text\/css application\/json application\/javascript text\/xml application\/xml application\/xml+rss text\/javascript;<\/code><\/pre>\n<h3 id=\"keepalive_timeout-deWOIIpGBp\">keepalive_timeout<\/h3>\n<p>\u6b64\u53c3\u6578\u7528\u65bc\u8a2d\u5b9a\u4fdd\u6301\u9023\u63a5\u7684\u8d85\u6642\u6642\u9593\uff0c\u9069\u7576\u7684\u8a2d\u7f6e\u53ef\u4ee5\u63d0\u9ad8\u4f3a\u670d\u5668\u7684\u6027\u80fd\u3002<\/p>\n<pre><code>keepalive_timeout 65;<\/code><\/pre>\n<h2 id=\"%e6%97%a5%e8%aa%8c%e9%85%8d%e7%bd%ae-deWOIIpGBp\">\u65e5\u8a8c\u914d\u7f6e<\/h2>\n<h3 id=\"access_log-deWOIIpGBp\">access_log<\/h3>\n<p>\u6b64\u53c3\u6578\u7528\u65bc\u6307\u5b9a\u8a2a\u554f\u65e5\u8a8c\u7684\u5b58\u5132\u8def\u5f91\uff0c\u4fbf\u65bc\u5f8c\u7e8c\u7684\u5206\u6790\u548c\u76e3\u63a7\u3002<\/p>\n<pre><code>access_log \/var\/log\/nginx\/access.log;<\/code><\/pre>\n<h3 id=\"error_log-deWOIIpGBp\">error_log<\/h3>\n<p>\u6b64\u53c3\u6578\u7528\u65bc\u6307\u5b9a\u932f\u8aa4\u65e5\u8a8c\u7684\u5b58\u5132\u8def\u5f91\uff0c\u5e6b\u52a9\u7528\u6236\u53ca\u6642\u767c\u73fe\u548c\u89e3\u6c7a\u554f\u984c\u3002<\/p>\n<pre><code>error_log \/var\/log\/nginx\/error.log;<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-deWOIIpGBp\">\u7e3d\u7d50<\/h2>\n<p>Nginx \u7684\u914d\u7f6e\u53c3\u6578\u591a\u6a23\u4e14\u9748\u6d3b\uff0c\u80fd\u5920\u6eff\u8db3\u4e0d\u540c\u5834\u666f\u7684\u9700\u6c42\u3002\u901a\u904e\u5408\u7406\u7684\u914d\u7f6e\uff0c\u53ef\u4ee5\u986f\u8457\u63d0\u5347\u4f3a\u670d\u5668\u7684\u6027\u80fd\u548c\u5b89\u5168\u6027\u3002\u82e5\u60a8\u9700\u8981\u66f4\u5c08\u696d\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u670d\u52d9\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u52a9\u60a8\u8f15\u9b06\u642d\u5efa\u9ad8\u6548\u80fd\u7684\u7db2\u7ad9\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a> \u9084\u662f <a href=\"https:\/\/server.hk\">\u4e91\u670d\u52a1\u5668<\/a>\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u6700\u4f73\u7684\u89e3\u6c7a\u65b9\u6848\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u53f2\u4e0a\u6700\u5168\u7684 Nginx \u914d\u7f6e\u53c3\u6578\u4e2d\u6587\u8aaa\u660e\uff0c\u6df1\u5165\u4e86\u89e3\u6bcf\u500b\u53c3\u6578\u7684\u529f\u80fd\u8207\u6700\u4f73\u5be6\u8e10\uff0c\u52a9\u4f60\u512a\u5316\u4f3a\u670d\u5668\u6027\u80fd\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-51093","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/51093","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=51093"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/51093\/revisions"}],"predecessor-version":[{"id":51094,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/51093\/revisions\/51094"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=51093"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=51093"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=51093"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}