{"id":180,"date":"2020-07-19T19:10:34","date_gmt":"2020-07-19T11:10:34","guid":{"rendered":"http:\/\/qssjx.com\/?p=180"},"modified":"2021-07-29T08:15:40","modified_gmt":"2021-07-29T00:15:40","slug":"nginx%e5%8f%8d%e5%90%91%e4%bb%a3%e7%90%86","status":"publish","type":"post","link":"https:\/\/qssjx.com\/?p=180","title":{"rendered":"nginx\u53cd\u5411\u4ee3\u7406"},"content":{"rendered":"<p>\u56e0\u4e3a\u4e1a\u52a1\u7cfb\u7edf\u9700\u6c42\uff0c\u9700\u8981\u5bf9web\u670d\u52a1\u4f5cnginx\u4ee3\u7406\uff0c\u5728\u4e0d\u65ad\u7684\u5c1d\u8bd5\u8fc7\u7a0b\u4e2d\uff0c\u7b80\u5355\u603b\u7ed3\u4e86\u4e00\u4e0b\u5e38\u89c1\u7684nginx\u4ee3\u7406\u914d\u7f6e\u3002<\/p>\n<h2>1. \u6700\u7b80\u53cd\u5411\u4ee3\u7406\u914d\u7f6e<\/h2>\n<p>\u5728http\u8282\u70b9\u4e0b\uff0c\u4f7f\u7528upstream\u914d\u7f6e\u670d\u52a1\u5730\u5740\uff0c\u4f7f\u7528server\u7684location\u914d\u7f6e\u4ee3\u7406\u6620\u5c04\u3002<\/p>\n<pre class=\"prettyprint linenums\" >\r\nupstream my_server {                                                         \r\n    server 10.0.0.2:8080;                                                \r\n    keepalive 2000;\r\n}\r\nserver {\r\n    listen       80;                                                         \r\n    server_name  10.0.0.1;                                               \r\n    client_max_body_size 1024M;\r\n\r\n    location \/my\/ {\r\n        proxy_pass http:\/\/my_server\/;\r\n        proxy_set_header Host $host:$server_port;\r\n    }\r\n}\r\n<\/pre>\n<p>\u901a\u8fc7\u8be5\u914d\u7f6e\uff0c\u8bbf\u95eenginx\u5730\u5740http:\/\/10.0.0.1:80\/my\u7684\u8bf7\u6c42\u4f1a\u88ab\u8f6c\u53d1\u5230my_server\u670d\u52a1\u5730\u5740http:\/\/10.0.0.2:8080\/\u3002<\/p>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5982\u679c\u6309\u7167\u5982\u4e0b\u914d\u7f6e\uff1a<\/p>\n<pre class=\"prettyprint linenums\" >\r\nupstream my_server {                                                         \r\n    server 10.0.0.2:8080;                                                \r\n    keepalive 2000;\r\n}\r\nserver {\r\n    listen       80;                                                         \r\n    server_name  10.0.0.1;                                               \r\n    client_max_body_size 1024M;\r\n\r\n    location \/my\/ {\r\n        proxy_pass http:\/\/my_server;\r\n        proxy_set_header Host $host:$server_port;\r\n    }\r\n}\r\n<\/pre>\n<p>\u90a3\u4e48\uff0c\u8bbf\u95eenginx\u5730\u5740http:\/\/10.0.0.1:80\/my\u7684\u8bf7\u6c42\u4f1a\u88ab\u8f6c\u53d1\u5230my_server\u670d\u52a1\u5730\u5740http:\/\/10.0.0.2:8080\/my\u3002\u8fd9\u662f\u56e0\u4e3aproxy_pass\u53c2\u6570\u4e2d\u5982\u679c\u4e0d\u5305\u542burl\u7684\u8def\u5f84\uff0c\u5219\u4f1a\u5c06location\u7684pattern\u8bc6\u522b\u7684\u8def\u5f84\u4f5c\u4e3a\u7edd\u5bf9\u8def\u5f84\u3002<\/p>\n<h2>2. \u91cd\u5b9a\u5411\u62a5\u6587\u4ee3\u7406<\/h2>\n<p>\u5373\u4fbf\u914d\u7f6e\u4e86nginx\u4ee3\u7406\uff0c\u5f53\u670d\u52a1\u8fd4\u56de\u91cd\u5b9a\u5411\u62a5\u6587\u65f6\uff08http code\u4e3a301\u6216302\uff09\uff0c\u4f1a\u5c06\u91cd\u5b9a\u5411\u7684\u76ee\u6807url\u5730\u5740\u653e\u5165http response\u62a5\u6587\u7684header\u7684location\u5b57\u6bb5\u5185\u3002\u7528\u6237\u6d4f\u89c8\u5668\u6536\u5230\u91cd\u5b9a\u5411\u62a5\u6587\u65f6\uff0c\u4f1a\u89e3\u6790\u51fa\u8be5\u5b57\u6bb5\u5e76\u4f5c\u8df3\u8f6c\u3002\u6b64\u65f6\u65b0\u7684\u8bf7\u6c42\u62a5\u6587\u5c06\u76f4\u63a5\u53d1\u9001\u7ed9\u670d\u52a1\u5730\u5740\uff0c\u800c\u975enginx\u5730\u5740\u3002\u4e3a\u4e86\u80fd\u8ba9nginx\u62e6\u622a\u6b64\u7c7b\u8bf7\u6c42\uff0c\u5fc5\u987b\u4fee\u6539\u91cd\u5b9a\u5411\u62a5\u6587\u7684location\u4fe1\u606f\u3002<\/p>\n<pre class=\"prettyprint linenums\" >\r\nlocation \/my\/ {\r\n    proxy_pass http:\/\/my_server;\r\n    proxy_set_header Host $host:$server_port;\r\n\r\n    proxy_redirect \/ \/my\/;\r\n}\r\n<\/pre>\n<p>\u4f7f\u7528proxy_redirect\u53ef\u4ee5\u4fee\u6539\u91cd\u5b9a\u5411\u62a5\u6587\u7684location\u5b57\u6bb5\uff0c\u4f8b\u5b50\u4e2d\u4f1a\u5c06\u6240\u6709\u7684\u6839\u8def\u5f84\u4e0b\u7684url\u4ee3\u7406\u5230nginx\u7684\/my\/\u8def\u5f84\u4e0b\u8fd4\u56de\u7ed9\u7528\u6237\u3002\u6bd4\u5982\u670d\u52a1\u8fd4\u56de\u7684\u91cd\u5b9a\u5411\u62a5\u6587\u7684location\u539f\u59cb\u503c\u4e3a\/login\uff0c\u90a3\u4e48\u7ecf\u8fc7nginx\u4ee3\u7406\u540e\uff0c\u7528\u6237\u6536\u5230\u7684\u62a5\u6587\u7684location\u5b57\u6bb5\u4e3a\/my\/login\u3002\u6b64\u65f6\uff0c\u6d4f\u89c8\u5668\u5c06\u4f1a\u8df3\u8f6c\u5230nginx\u7684\/my\/login\u5730\u5740\u8fdb\u884c\u8bbf\u95ee\u3002<\/p>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u670d\u52a1\u8fd4\u56de\u7684\u91cd\u5b9a\u5411\u62a5\u6587\u7684location\u5b57\u6bb5\u6709\u65f6\u4f1a\u586b\u5199\u7edd\u5bf9\u8def\u5f84\uff08\u5305\u542b\u670d\u52a1\u7684ip\/\u57df\u540d\u548c\u7aef\u53e3\uff09\uff0c\u6709\u65f6\u5019\u4f1a\u586b\u5199\u76f8\u5bf9\u8def\u5f84\uff0c\u6b64\u65f6\u9700\u8981\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u8fdb\u884c\u7504\u522b\u3002<\/p>\n<pre class=\"prettyprint linenums\" >\r\nlocation \/my\/ {\r\n    proxy_pass http:\/\/my_server;\r\n    proxy_set_header Host $host:$server_port;\r\n\r\n    proxy_redirect http:\/\/my_server\/ http:\/\/$host:$server_port\/my\/;\r\n}\r\n<\/pre>\n<p>\u4e0a\u8ff0\u914d\u7f6e\u4fbf\u662f\u5c06my_server\u670d\u52a1\u7684\u6839\u8def\u5f84\u4e0b\u7684\u6240\u6709\u8def\u5f84\u4ee3\u7406\u5230nginx\u5730\u5740\u7684\/my\/\u8def\u5f84\u4e0b\u3002\u5f53nginx\u914d\u7f6e\u53ea\u6709\u4e00\u4e2aserver\u65f6\uff0chttp:\/\/$host:$server_port\u524d\u7f00\u53ef\u4ee5\u7701\u7565\u3002<\/p>\n<h2>3. \u62a5\u6587\u6570\u636e\u66ff\u6362<\/h2>\n<p>\u4f7f\u7528nginx\u4ee3\u7406\u6700\u725b\uff08dan\uff09\u903c\uff08sui\uff09\u7684\u60c5\u51b5\u5c31\u662fhttp\u54cd\u5e94\u62a5\u6587\u5185\u5199\u6b7b\u4e86\u670d\u52a1\u5730\u5740\u6216web\u7edd\u5bf9\u8def\u5f84\u3002\u5199\u6b7b\u670d\u52a1\u5730\u5740\u7684\u60c5\u51b5\u6bd4\u8f83\u5c11\u89c1\uff0c\u4f46\u4e5f\u5076\u5c14\u5b58\u5728\u3002\u6700\u68d8\u624b\u7684\u662f\u5199\u6b7b\u4e86web\u7edd\u5bf9\u8def\u5f84\uff0c\u5c24\u5176\u662f\u7edd\u5bf9\u8def\u5f84\u90fd\u6ca1\u6709\u516c\u5171\u524d\u7f00\u3002\u4e3e\u4e2a\u4f8b\u5b50\u6765\u8bf4\uff1a<\/p>\n<p>\u4e00\u822c\u7684web\u9875\u9762\u4f1a\u5305\u542b\u5982\u4e0b\u7c7b\u4f3c\u8def\u5f84\uff1a<\/p>\n<p>\/public\uff1a\u7528\u4e8e\u9759\u6001\u9875\u9762\u8d44\u6e90\uff0c\u5982js\u811a\u672c\/public\/js\uff0c\u6837\u5f0f\u8868\/public\/css\uff0c\u56fe\u7247\/public\/img\u7b49\u3002<br \/>\n\/static\uff1a\u548c\/public\u7c7b\u4f3c\u3002<br \/>\n\/api\uff1a\u7528\u4e8e\u540e\u53f0\u670d\u52a1API\u63a5\u53e3\u3002<br \/>\n\/login\uff1a\u7528\u4e8e\u767b\u5f55\u9a8c\u8bc1\u3002<br \/>\n\u5176\u4ed6\u3002<br \/>\n\u5bf9\u4e8e\u8fd9\u6837\u7684\u670d\u52a1\uff0c\u53ef\u80fd\u7684\u4ee3\u7406\u914d\u7f6e\u5982\u4e0b\uff1a<\/p>\n<pre class=\"prettyprint linenums\" >\r\nlocation \/my\/ {\r\n    proxy_pass http:\/\/my_server\/;\r\n    proxy_set_header Host $host:$server_port;\r\n\r\n    proxy_redirect \/ \/my\/;\r\n}\r\nlocation \/login\/ {\r\n    proxy_pass http:\/\/my_server\/public;\r\n    proxy_set_header Host $host:$server_port;\r\n}\r\nlocation \/public\/ {\r\n    proxy_pass http:\/\/my_server\/public;\r\n    proxy_set_header Host $host:$server_port;\r\n}\r\nlocation \/api\/ {\r\n    proxy_pass http:\/\/my_server\/api;\r\n    proxy_set_header Host $host:$server_port;\r\n}\r\n<\/pre>\n<p>\u7531\u4e8eweb\u9875\u9762\u6216\u9759\u6001\u8d44\u6e90\u5185\u5199\u6b7b\u4e86\u7c7b\u4f3c\u7684\u7edd\u5bf9\u8def\u5f84\uff0c\u90a3\u4e48\u5bf9\u4e8e\u7528\u6237\u6765\u8bf4\uff0c\u901a\u8fc7\u9875\u9762\u5185\u7684\u94fe\u63a5\u8fdb\u884c\u8df3\u8f6c\u65f6\uff0c\u90fd\u4f1a\u8bf7\u6c42\u5230nginx\u670d\u52a1\u5bf9\u5e94\u7684\u8def\u5f84\u4e0a\u3002\u4e00\u65e6\u5b58\u5728\u53e6\u4e00\u4e2a\u670d\u52a1\u4e5f\u5305\u542b\u7c7b\u4f3c\u7684\u8def\u5f84\uff0c\u4e5f\u9700\u8981nginx\u8fdb\u884c\u4ee3\u7406\uff0c\u90a3\u4e48\u77db\u76fe\u5c31\u51fa\u73b0\u4e86\uff1a\u8bbf\u95eenginx\u7684\u540c\u4e00\u4e2a\u8def\u5f84\u4e0b\u7684\u8bf7\u6c42\u7a76\u7adf\u8f6c\u53d1\u7ed9\u54ea\u4e00\u4e2a\u670d\u52a1\uff1f<\/p>\n<p>\u8981\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u5fc5\u987b\u5728\u7528\u6237\u6536\u5230\u62a5\u6587\u524d\uff0c\u5c06\u62a5\u6587\u7684\u6570\u636e\u4e2d\u5305\u542b\u7684\u7edd\u5bf9\u8def\u5f84\u90fd\u6dfb\u52a0\u7edf\u4e00\u7684\u524d\u7f00\uff0c\u5982\/my\/public\uff0c\/my\/api\uff0c\/my\/login\uff0c\u8fd9\u6837nginx\u4ee3\u7406\u914d\u7f6e\u5219\u53ef\u4ee5\u7b80\u5316\u4e3a\uff1a<\/p>\n<pre class=\"prettyprint linenums\" >\r\nlocation \/my\/ {\r\n    proxy_pass http:\/\/my_server\/;\r\n    proxy_set_header Host $host:$server_port;\r\n\r\n    proxy_redirect \/ \/my\/;\r\n}\r\nlocation \/other\/ {\r\n    proxy_pass http:\/\/other_server\/;\r\n    proxy_set_header Host $host:$server_port;\r\n\r\n    proxy_redirect \/ \/other\/;\r\n}\r\n<\/pre>\n<p>nginx\u7684ngx_http_sub_module\u6a21\u5757\u63d0\u4f9b\u4e86\u7c7b\u4f3c\u7684\u62a5\u6587\u6570\u636e\u66ff\u6362\u529f\u80fd\uff0c\u8be5\u6a21\u5757\u9ed8\u8ba4\u4e0d\u4f1a\u5b89\u88c5\uff0c\u9700\u8981\u5728\u7f16\u8bd1nginx\u65f6\u6dfb\u52a0&#8211;with-http_sub_module\u53c2\u6570\uff0c\u6216\u8005\u76f4\u63a5\u4e0b\u8f7dnginx\u7684rpm\u5305\u3002<\/p>\n<p>\u4f7f\u7528sub_filter\u5bf9\u6570\u636e\u5305\u8fdb\u884c\u66ff\u6362\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre class=\"prettyprint linenums\" >\r\nlocation \/my\/ {\r\n    proxy_pass http:\/\/my_server\/;\r\n    proxy_set_header Host $host:$server_port;\r\n    \r\n    sub_filter &#039;href=&quot;\/&#039; &#039;href=&quot;\/my\/&#039;;\r\n    sub_filter &#039;src=&quot;\/&#039; &#039;src=&quot;\/my\/&#039;;\r\n    sub_filter_types text\/html;\r\n    sub_filter_once  off;\r\n}\r\n<\/pre>\n<p>\u4e0a\u8ff0\u914d\u7f6e\u4f1a\u5c06\/my\/\u4e0b\u7684\u6240\u6709\u54cd\u5e94\u62a5\u6587\u5185\u5bb9\u7684href=&#8221;\/\u66ff\u6362\u4e3ahref=&#8221;\/my\uff0c\u4ee5\u53casrc=&#8221;\/\u66ff\u6362\u4e3asrc=&#8221;\/my\uff0c\u5373\u4e3a\u6240\u6709\u7684\u7edd\u5bf9\u8def\u5f84\u6dfb\u52a0\u516c\u5171\u524d\u7f00\u3002<\/p>\n<p>\u6ce8\u610f\uff0c\u5982\u679c\u9700\u8981\u914d\u7f6e\u591a\u4e2asub_filter\uff0c\u5fc5\u987b\u4fdd\u8bc1nginx\u662f1.9.4\u7248\u672c\u4e4b\u4e0a\u7684\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u56e0\u4e3a\u4e1a\u52a1\u7cfb\u7edf\u9700\u6c42\uff0c\u9700\u8981\u5bf9web\u670d\u52a1\u4f5cnginx\u4ee3\u7406\uff0c\u5728\u4e0d\u65ad\u7684\u5c1d\u8bd5\u8fc7\u7a0b\u4e2d\uff0c\u7b80\u5355\u603b\u7ed3\u4e86\u4e00\u4e0b\u5e38\u89c1\u7684nginx\u4ee3\u7406\u914d\u7f6e [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[],"_links":{"self":[{"href":"https:\/\/qssjx.com\/index.php?rest_route=\/wp\/v2\/posts\/180"}],"collection":[{"href":"https:\/\/qssjx.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qssjx.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qssjx.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qssjx.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=180"}],"version-history":[{"count":1,"href":"https:\/\/qssjx.com\/index.php?rest_route=\/wp\/v2\/posts\/180\/revisions"}],"predecessor-version":[{"id":181,"href":"https:\/\/qssjx.com\/index.php?rest_route=\/wp\/v2\/posts\/180\/revisions\/181"}],"wp:attachment":[{"href":"https:\/\/qssjx.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=180"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qssjx.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=180"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qssjx.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=180"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}