{"id":51117,"date":"2024-10-08T16:12:17","date_gmt":"2024-10-08T08:12:17","guid":{"rendered":"https:\/\/server.hk\/cnblog\/51117\/"},"modified":"2024-10-08T16:12:17","modified_gmt":"2024-10-08T08:12:17","slug":"nginx-%e6%95%99%e7%a8%8b%ef%bc%9anginx-%e5%9f%ba%e6%9c%ac%e6%95%b8%e6%93%9a%e7%b5%90%e6%a7%8b","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/51117\/","title":{"rendered":"nginx \u6559\u7a0b\uff1aNginx \u57fa\u672c\u6578\u64da\u7d50\u69cb"},"content":{"rendered":"<h1 id=\"nginx-%e6%95%99%e7%a8%8b%ef%bc%9anginx-%e5%9f%ba%e6%9c%ac%e6%95%b8%e6%93%9a%e7%b5%90%e6%a7%8b-kjDvfweCVk\">Nginx \u6559\u7a0b\uff1aNginx \u57fa\u672c\u6578\u64da\u7d50\u69cb<\/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\u4e86\u89e3 Nginx \u7684\u57fa\u672c\u6578\u64da\u7d50\u69cb\u5c0d\u65bc\u914d\u7f6e\u548c\u512a\u5316\u4f3a\u670d\u5668\u81f3\u95dc\u91cd\u8981\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e Nginx \u7684\u57fa\u672c\u6578\u64da\u7d50\u69cb\uff0c\u5e6b\u52a9\u8b80\u8005\u66f4\u597d\u5730\u7406\u89e3\u5176\u904b\u4f5c\u539f\u7406\u3002<\/p>\n<h2 id=\"nginx-%e7%9a%84%e9%85%8d%e7%bd%ae%e6%96%87%e4%bb%b6%e7%b5%90%e6%a7%8b-kjDvfweCVk\">Nginx \u7684\u914d\u7f6e\u6587\u4ef6\u7d50\u69cb<\/h2>\n<p>Nginx \u7684\u914d\u7f6e\u6587\u4ef6\u901a\u5e38\u4f4d\u65bc <code>\/etc\/nginx\/nginx.conf<\/code>\uff0c\u5176\u7d50\u69cb\u7531\u591a\u500b\u5340\u584a\u7d44\u6210\u3002\u4e3b\u8981\u7684\u5340\u584a\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>\u4e3b\u5340\u584a\uff08Main Block\uff09<\/strong>\uff1a\u5b9a\u7fa9\u5168\u5c40\u8a2d\u7f6e\uff0c\u5982\u7528\u6236\u3001\u5de5\u4f5c\u9032\u7a0b\u6578\u7b49\u3002<\/li>\n<li><strong>\u4e8b\u4ef6\u5340\u584a\uff08Events Block\uff09<\/strong>\uff1a\u914d\u7f6e\u8207\u9023\u63a5\u76f8\u95dc\u7684\u8a2d\u7f6e\uff0c\u5982\u6700\u5927\u9023\u63a5\u6578\u3002<\/li>\n<li><strong>HTTP \u5340\u584a\uff08HTTP Block\uff09<\/strong>\uff1a\u5b9a\u7fa9 HTTP \u4f3a\u670d\u5668\u7684\u8a2d\u7f6e\uff0c\u5305\u62ec\u4f3a\u670d\u5668\u5340\u584a\u548c\u4f4d\u7f6e\u5340\u584a\u3002<\/li>\n<\/ul>\n<h3 id=\"%e4%b8%bb%e5%8d%80%e5%a1%8a-kjDvfweCVk\">\u4e3b\u5340\u584a<\/h3>\n<pre><code>user  www-data;\nworker_processes  auto;\nerror_log  \/var\/log\/nginx\/error.log;\npid        \/var\/run\/nginx.pid;\n<\/code><\/pre>\n<p>\u5728\u4e3b\u5340\u584a\u4e2d\uff0c<code>user<\/code> \u6307\u5b9a\u904b\u884c Nginx \u7684\u7528\u6236\uff0c<code>worker_processes<\/code> \u5b9a\u7fa9\u5de5\u4f5c\u9032\u7a0b\u7684\u6578\u91cf\uff0c<code>error_log<\/code> \u548c <code>pid<\/code> \u5206\u5225\u6307\u5b9a\u932f\u8aa4\u65e5\u8a8c\u548c\u9032\u7a0b ID \u6587\u4ef6\u7684\u4f4d\u7f6e\u3002<\/p>\n<h3 id=\"%e4%ba%8b%e4%bb%b6%e5%8d%80%e5%a1%8a-kjDvfweCVk\">\u4e8b\u4ef6\u5340\u584a<\/h3>\n<pre><code>events {\n    worker_connections  1024;\n}\n<\/code><\/pre>\n<p>\u4e8b\u4ef6\u5340\u584a\u4e3b\u8981\u7528\u65bc\u8a2d\u7f6e\u8207\u9023\u63a5\u76f8\u95dc\u7684\u53c3\u6578\uff0c\u4f8b\u5982 <code>worker_connections<\/code> \u5b9a\u7fa9\u6bcf\u500b\u5de5\u4f5c\u9032\u7a0b\u53ef\u4ee5\u8655\u7406\u7684\u6700\u5927\u9023\u63a5\u6578\u3002<\/p>\n<h3 id=\"http-%e5%8d%80%e5%a1%8a-kjDvfweCVk\">HTTP \u5340\u584a<\/h3>\n<p>HTTP \u5340\u584a\u662f Nginx \u914d\u7f6e\u7684\u6838\u5fc3\uff0c\u5305\u542b\u591a\u500b\u4f3a\u670d\u5668\u5340\u584a\u548c\u4f4d\u7f6e\u5340\u584a\u3002\u4f3a\u670d\u5668\u5340\u584a\u7528\u65bc\u5b9a\u7fa9\u865b\u64ec\u4e3b\u6a5f\u7684\u8a2d\u7f6e\uff0c\u800c\u4f4d\u7f6e\u5340\u584a\u5247\u7528\u65bc\u5339\u914d\u8acb\u6c42\u7684 URI\u3002<\/p>\n<h4 id=\"%e4%bc%ba%e6%9c%8d%e5%99%a8%e5%8d%80%e5%a1%8a-kjDvfweCVk\">\u4f3a\u670d\u5668\u5340\u584a<\/h4>\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}\n<\/code><\/pre>\n<p>\u5728\u4f3a\u670d\u5668\u5340\u584a\u4e2d\uff0c<code>listen<\/code> \u6307\u5b9a\u76e3\u807d\u7684\u7aef\u53e3\uff0c<code>server_name<\/code> \u5b9a\u7fa9\u4f3a\u670d\u5668\u7684\u57df\u540d\uff0c<code>location<\/code> \u5340\u584a\u5247\u6307\u5b9a\u8acb\u6c42\u7684\u8655\u7406\u65b9\u5f0f\u3002<\/p>\n<h4 id=\"%e4%bd%8d%e7%bd%ae%e5%8d%80%e5%a1%8a-kjDvfweCVk\">\u4f4d\u7f6e\u5340\u584a<\/h4>\n<p>\u4f4d\u7f6e\u5340\u584a\u53ef\u4ee5\u4f7f\u7528\u591a\u7a2e\u5339\u914d\u65b9\u5f0f\uff0c\u4f8b\u5982\u7cbe\u78ba\u5339\u914d\u3001\u524d\u7db4\u5339\u914d\u548c\u6b63\u5247\u8868\u9054\u5f0f\u5339\u914d\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<pre><code>location = \/favicon.ico {\n    access_log off;\n    log_not_found off;\n}\n\nlocation \/images\/ {\n    root \/var\/www\/html;\n}\n\nlocation ~ .php$ {\n    include fastcgi_params;\n    fastcgi_pass 127.0.0.1:9000;\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u4e9b\u793a\u4f8b\u4e2d\uff0c\u7b2c\u4e00\u500b\u4f4d\u7f6e\u5340\u584a\u7cbe\u78ba\u5339\u914d\u8acb\u6c42\u7684 URI\uff0c\u7b2c\u4e8c\u500b\u4f4d\u7f6e\u5340\u584a\u5339\u914d\u4ee5 <code>\/images\/<\/code> \u958b\u982d\u7684\u8acb\u6c42\uff0c\u800c\u7b2c\u4e09\u500b\u4f4d\u7f6e\u5340\u584a\u5247\u4f7f\u7528\u6b63\u5247\u8868\u9054\u5f0f\u5339\u914d\u4ee5 <code>.php<\/code> \u7d50\u5c3e\u7684\u8acb\u6c42\u3002<\/p>\n<h2 id=\"nginx-%e7%9a%84%e8%ae%8a%e9%87%8f-kjDvfweCVk\">Nginx \u7684\u8b8a\u91cf<\/h2>\n<p>Nginx \u652f\u6301\u591a\u7a2e\u8b8a\u91cf\uff0c\u9019\u4e9b\u8b8a\u91cf\u53ef\u4ee5\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u4f7f\u7528\uff0c\u4ee5\u4fbf\u6839\u64da\u8acb\u6c42\u7684\u4e0d\u540c\u60c5\u6cc1\u9032\u884c\u52d5\u614b\u914d\u7f6e\u3002\u5e38\u898b\u7684\u8b8a\u91cf\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><code>$host<\/code>\uff1a\u8acb\u6c42\u7684\u4e3b\u6a5f\u540d\u3002<\/li>\n<li><code>$uri<\/code>\uff1a\u8acb\u6c42\u7684 URI\u3002<\/li>\n<li><code>$remote_addr<\/code>\uff1a\u5ba2\u6236\u7aef\u7684 IP \u5730\u5740\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-kjDvfweCVk\">\u7e3d\u7d50<\/h2>\n<p>\u4e86\u89e3 Nginx \u7684\u57fa\u672c\u6578\u64da\u7d50\u69cb\u5c0d\u65bc\u6709\u6548\u914d\u7f6e\u548c\u7ba1\u7406\u4f3a\u670d\u5668\u81f3\u95dc\u91cd\u8981\u3002\u901a\u904e\u638c\u63e1\u4e3b\u5340\u584a\u3001\u4e8b\u4ef6\u5340\u584a\u3001HTTP \u5340\u584a\u53ca\u5176\u5167\u90e8\u7684\u4f3a\u670d\u5668\u548c\u4f4d\u7f6e\u5340\u584a\uff0c\u4f7f\u7528\u8005\u53ef\u4ee5\u9748\u6d3b\u5730\u8abf\u6574\u4f3a\u670d\u5668\u7684\u884c\u70ba\u4ee5\u6eff\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u3002\u82e5\u60a8\u9700\u8981\u9032\u4e00\u6b65\u7684\u652f\u6301\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 \u7684\u57fa\u672c\u6578\u64da\u7d50\u69cb\uff0c\u4e86\u89e3\u5176\u67b6\u69cb\u3001\u914d\u7f6e\u548c\u6027\u80fd\u512a\u5316\uff0c\u52a9\u4f60\u8f15\u9b06\u638c\u63e1 Nginx \u7684\u6838\u5fc3\u6982\u5ff5\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-51117","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/51117","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=51117"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/51117\/revisions"}],"predecessor-version":[{"id":51118,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/51117\/revisions\/51118"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=51117"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=51117"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=51117"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}