{"id":6485,"date":"2024-08-19T14:12:15","date_gmt":"2024-08-19T06:12:15","guid":{"rendered":"https:\/\/t.n-years.com\/?p=6485"},"modified":"2024-08-19T14:12:15","modified_gmt":"2024-08-19T06:12:15","slug":"%e5%ae%b9%e5%99%a8%e5%86%85%e5%a4%96%e7%9a%84-nginx%e5%9c%a8%e5%a4%84%e7%90%86%e5%a4%96%e9%83%a8%e8%af%b7%e6%b1%82%e4%b8%8a%e7%9a%84%e6%b5%81%e7%a8%8b%e6%9c%89%e4%bb%80%e4%b9%88%e4%b8%8d%e5%90%8c","status":"publish","type":"post","link":"https:\/\/t.n-years.com\/?p=6485","title":{"rendered":"\u5bb9\u5668\u5185\u5916\u7684 nginx\u5728\u5904\u7406\u5916\u90e8\u8bf7\u6c42\u4e0a\u7684\u6d41\u7a0b\u6709\u4ec0\u4e48\u4e0d\u540c"},"content":{"rendered":"<blockquote>\n<p>\u975e\u539f\u521b\u3001ChatGPT \u95ee\u7b54\u751f\u6210<\/p>\n<\/blockquote>\n<p>\u5bb9\u5668\u5185\u548c\u5bb9\u5668\u5916\u7684 Nginx \u5728\u5904\u7406\u5916\u90e8\u8bf7\u6c42\u7684\u6d41\u7a0b\u4e0a\uff0c\u672c\u8d28\u4e0a\u6ca1\u6709\u592a\u5927\u7684\u533a\u522b\uff0c\u56e0\u4e3a Nginx \u7684\u6838\u5fc3\u529f\u80fd\u548c\u914d\u7f6e\u903b\u8f91\u662f\u76f8\u540c\u7684\u3002\u4e3b\u8981\u7684\u4e0d\u540c\u5728\u4e8e\u7f51\u7edc\u5c42\u7ea7\u3001\u5730\u5740\u6620\u5c04\u548c\u5bb9\u5668\u5316\u7684\u76f8\u5173\u673a\u5236\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5173\u952e\u7684\u533a\u522b\u548c\u6d41\u7a0b\u7684\u4e0d\u540c\u4e4b\u5904\uff1a<\/p>\n<h3>1. <strong>\u7f51\u7edc\u914d\u7f6e\u548c\u5730\u5740\u89e3\u6790<\/strong><\/h3>\n<ul>\n<li>\n<p><strong>\u5bb9\u5668\u5916\u7684 Nginx<\/strong>:<\/p>\n<ul>\n<li>\u5bb9\u5668\u5916\u7684 Nginx \u901a\u5e38\u76f4\u63a5\u76d1\u542c\u5bbf\u4e3b\u673a\u7684\u7f51\u7edc\u63a5\u53e3\uff08\u5982 <code>0.0.0.0:80<\/code>\uff09\uff0c\u901a\u8fc7\u5bbf\u4e3b\u673a\u7684 IP \u5730\u5740\u63a5\u53d7\u5916\u90e8\u8bf7\u6c42\u3002<\/li>\n<li>\u5916\u90e8\u8bf7\u6c42\u76f4\u63a5\u901a\u8fc7\u5bbf\u4e3b\u673a\u7684\u7f51\u7edc\u6808\u4f20\u9012\u7ed9 Nginx\uff0c\u7136\u540e\u6839\u636e\u914d\u7f6e\u5904\u7406\u8bf7\u6c42\uff08\u4f8b\u5982\u4ee3\u7406\u5230\u540e\u7aef\u5e94\u7528\u670d\u52a1\u5668\uff09\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u5bb9\u5668\u5185\u7684 Nginx<\/strong>:<\/p>\n<ul>\n<li>\u5bb9\u5668\u5185\u7684 Nginx \u901a\u5e38\u8fd0\u884c\u5728\u4e00\u4e2a\u72ec\u7acb\u7684\u7f51\u7edc\u547d\u540d\u7a7a\u95f4\u4e2d\u3002\u5bb9\u5668\u81ea\u8eab\u6709\u81ea\u5df1\u7684 IP \u5730\u5740\uff0c\u901a\u5e38\u901a\u8fc7 Docker \u7684\u7f51\u7edc\u6865\u63a5\u6216\u7f51\u7edc\u6a21\u5f0f\uff08\u5982 bridge, host, overlay\uff09\u8fdb\u884c\u8fde\u63a5\u3002<\/li>\n<li>\u5916\u90e8\u8bf7\u6c42\u9996\u5148\u5230\u8fbe\u5bbf\u4e3b\u673a\uff0c\u7136\u540e\u901a\u8fc7 Docker \u7684\u7aef\u53e3\u6620\u5c04\uff08\u5982 <code>-p 80:80<\/code>\uff09\u8f6c\u53d1\u5230\u5bb9\u5668\u5185\u3002\u5bb9\u5668\u5185\u7684 Nginx \u4f1a\u76d1\u542c\u5bb9\u5668\u5185\u90e8\u7684\u7aef\u53e3\uff08\u901a\u5e38\u662f <code>80<\/code> \u6216 <code>443<\/code>\uff09\uff0c\u5904\u7406\u8bf7\u6c42\u540e\u518d\u6839\u636e\u914d\u7f6e\u4ee3\u7406\u5230\u540e\u7aef\u670d\u52a1\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>2. <strong>\u7aef\u53e3\u6620\u5c04\u4e0e\u7f51\u7edc\u6a21\u5f0f<\/strong><\/h3>\n<ul>\n<li>\n<p><strong>\u5bb9\u5668\u5916\u7684 Nginx<\/strong>:<\/p>\n<ul>\n<li>Nginx \u901a\u5e38\u76f4\u63a5\u7ed1\u5b9a\u5230\u5bbf\u4e3b\u673a\u7684\u67d0\u4e2a\u7aef\u53e3\uff0c\u4f8b\u5982 <code>80<\/code> \u6216 <code>443<\/code>\uff0c\u8fd9\u4e9b\u7aef\u53e3\u76f4\u63a5\u66b4\u9732\u7ed9\u5916\u90e8\u7528\u6237\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u5bb9\u5668\u5185\u7684 Nginx<\/strong>:<\/p>\n<ul>\n<li>\u5bb9\u5668\u5316\u90e8\u7f72\u4e2d\uff0c\u5bbf\u4e3b\u673a\u7684\u7aef\u53e3\u9700\u8981\u6620\u5c04\u5230\u5bb9\u5668\u7684\u7aef\u53e3\u3002\u4f8b\u5982\u4f7f\u7528 <code>docker run -p 80:80 nginx<\/code>\uff0c\u5c06\u5bbf\u4e3b\u673a\u7684\u7aef\u53e3 <code>80<\/code> \u6620\u5c04\u5230\u5bb9\u5668\u5185\u90e8\u7684\u7aef\u53e3 <code>80<\/code>\u3002<\/li>\n<li>\u5728\u67d0\u4e9b\u7f51\u7edc\u6a21\u5f0f\u4e0b\uff08\u5982 <code>host<\/code> \u6a21\u5f0f\uff09\uff0c\u5bb9\u5668\u5185\u7684 Nginx \u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u5bbf\u4e3b\u673a\u7684\u7f51\u7edc\u63a5\u53e3\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\u4e0e\u5bb9\u5668\u5916 Nginx \u7684\u5904\u7406\u6d41\u7a0b\u57fa\u672c\u4e00\u81f4\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>3. <strong>\u8d1f\u8f7d\u5747\u8861\u548c\u53cd\u5411\u4ee3\u7406\u7684\u76ee\u6807<\/strong><\/h3>\n<ul>\n<li>\n<p><strong>\u5bb9\u5668\u5916\u7684 Nginx<\/strong>:<\/p>\n<ul>\n<li>\u5bb9\u5668\u5916\u7684 Nginx \u53cd\u5411\u4ee3\u7406\u65f6\uff0c\u901a\u5e38\u76f4\u63a5\u4ee3\u7406\u5230\u5bbf\u4e3b\u673a\u7684\u67d0\u4e2a IP \u5730\u5740\u6216\u57df\u540d\u3002\u4f8b\u5982\uff0c\u914d\u7f6e <code>proxy_pass http:\/\/backend-service:8080;<\/code> \u6765\u5c06\u8bf7\u6c42\u8f6c\u53d1\u5230\u53e6\u4e00\u4e2a\u670d\u52a1\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u5bb9\u5668\u5185\u7684 Nginx<\/strong>:<\/p>\n<ul>\n<li>\u5bb9\u5668\u5185\u7684 Nginx \u5728\u5904\u7406\u8bf7\u6c42\u65f6\uff0c\u53ef\u80fd\u4f1a\u5c06\u8bf7\u6c42\u4ee3\u7406\u5230\u540c\u4e00\u4e2a Docker \u7f51\u7edc\u4e2d\u7684\u5176\u4ed6\u5bb9\u5668\u3002\u901a\u5e38\u901a\u8fc7\u670d\u52a1\u540d\uff08\u5982 <code>proxy_pass http:\/\/web-app:8080;<\/code>\uff09\u6765\u8fdb\u884c\u4ee3\u7406\uff0c\u800c\u670d\u52a1\u540d\u901a\u8fc7 Docker \u5185\u90e8\u7684 DNS \u89e3\u6790\u4e3a\u5bf9\u5e94\u5bb9\u5668\u7684 IP \u5730\u5740\u3002<\/li>\n<li>\u5982\u679c Nginx \u9700\u8981\u4ee3\u7406\u5230\u5916\u90e8\u670d\u52a1\uff0c\u4e5f\u53ef\u4ee5\u914d\u7f6e\u6210\u76f4\u63a5\u8bbf\u95ee\u5916\u90e8\u7684 IP \u5730\u5740\u6216\u57df\u540d\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>4. <strong>SSL\/TLS \u5904\u7406<\/strong><\/h3>\n<ul>\n<li>\n<p><strong>\u5bb9\u5668\u5916\u7684 Nginx<\/strong>:<\/p>\n<ul>\n<li>\u5bb9\u5668\u5916\u7684 Nginx \u76f4\u63a5\u7ba1\u7406 SSL \u8bc1\u4e66\uff08\u5982\u5b58\u50a8\u5728 <code>\/etc\/nginx\/ssl<\/code> \u4e2d\uff09\uff0c\u5e76\u5728\u5904\u7406 HTTPS \u8bf7\u6c42\u65f6\u8fdb\u884c\u8bc1\u4e66\u7684\u8bfb\u53d6\u548c\u9a8c\u8bc1\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u5bb9\u5668\u5185\u7684 Nginx<\/strong>:<\/p>\n<ul>\n<li>\u5bb9\u5668\u5185\u7684 Nginx \u540c\u6837\u53ef\u4ee5\u7ba1\u7406 SSL \u8bc1\u4e66\uff0c\u4f46\u9700\u8981\u6ce8\u610f\u8bc1\u4e66\u7684\u6301\u4e45\u5316\u548c\u6302\u8f7d\u3002\u901a\u5e38\u8bc1\u4e66\u4f1a\u901a\u8fc7 Docker Volume \u6216\u6302\u8f7d\u5230\u5bb9\u5668\u5185\u7684\u7279\u5b9a\u8def\u5f84\uff08\u5982 <code>\/etc\/nginx\/ssl<\/code>\uff09\uff0c\u4ee5\u786e\u4fdd\u5bb9\u5668\u91cd\u542f\u65f6\u4e0d\u4f1a\u4e22\u5931\u8bc1\u4e66\u6570\u636e\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>5. <strong>\u914d\u7f6e\u6587\u4ef6\u7684\u7ba1\u7406<\/strong><\/h3>\n<ul>\n<li>\n<p><strong>\u5bb9\u5668\u5916\u7684 Nginx<\/strong>:<\/p>\n<ul>\n<li>\u914d\u7f6e\u6587\u4ef6\u76f4\u63a5\u5728\u5bbf\u4e3b\u673a\u7684\u6587\u4ef6\u7cfb\u7edf\u4e2d\u7ba1\u7406\uff0c\u901a\u5e38\u5728 <code>\/etc\/nginx\/nginx.conf<\/code> \u6216 <code>\/etc\/nginx\/conf.d\/<\/code> \u76ee\u5f55\u4e0b\u3002\u7ba1\u7406\u5458\u53ef\u4ee5\u76f4\u63a5\u7f16\u8f91\u914d\u7f6e\u6587\u4ef6\u5e76\u91cd\u542f Nginx \u670d\u52a1\u4ee5\u5e94\u7528\u66f4\u6539\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u5bb9\u5668\u5185\u7684 Nginx<\/strong>:<\/p>\n<ul>\n<li>\u914d\u7f6e\u6587\u4ef6\u901a\u5e38\u6253\u5305\u5728\u955c\u50cf\u4e2d\uff0c\u6216\u8005\u901a\u8fc7\u6302\u8f7d\u5916\u90e8\u914d\u7f6e\u6587\u4ef6\u5230\u5bb9\u5668\u5185\uff08\u5982\u6302\u8f7d\u5230 <code>\/etc\/nginx\/conf.d\/<\/code>\uff09\u3002\u66f4\u6539\u914d\u7f6e\u6587\u4ef6\u540e\uff0c\u9700\u8981\u901a\u8fc7\u5bb9\u5668\u91cd\u542f\u6216 <code>nginx -s reload<\/code> \u547d\u4ee4\u91cd\u65b0\u52a0\u8f7d\u914d\u7f6e\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>6. <strong>\u5bb9\u5668\u7f16\u6392\u548c\u52a8\u6001\u914d\u7f6e<\/strong><\/h3>\n<ul>\n<li>\n<p><strong>\u5bb9\u5668\u5916\u7684 Nginx<\/strong>:<\/p>\n<ul>\n<li>\u5728\u4f20\u7edf\u73af\u5883\u4e2d\uff0cNginx \u7684\u914d\u7f6e\u4e00\u822c\u662f\u9759\u6001\u7684\u3002\u5bf9\u4e8e\u52a8\u6001\u670d\u52a1\u53d1\u73b0\u548c\u81ea\u52a8\u914d\u7f6e\uff0c\u9700\u8981\u624b\u52a8\u7ef4\u62a4\u914d\u7f6e\u6587\u4ef6\u6216\u8005\u501f\u52a9\u989d\u5916\u5de5\u5177\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u5bb9\u5668\u5185\u7684 Nginx<\/strong>:<\/p>\n<ul>\n<li>\u5728\u5bb9\u5668\u5316\u73af\u5883\u4e2d\uff0c\u5c24\u5176\u662f\u4f7f\u7528 Docker Compose \u6216 Kubernetes \u65f6\uff0cNginx \u53ef\u4ee5\u7ed3\u5408\u5de5\u5177\uff08\u5982 <code>consul-template<\/code>\u3001<code>envsubst<\/code> \u6216\u8005 Kubernetes Ingress Controller\uff09\u5b9e\u73b0\u52a8\u6001\u914d\u7f6e\u66f4\u65b0\u3002\u5bb9\u5668\u5185 Nginx \u7684\u914d\u7f6e\u66f4\u9002\u5408\u81ea\u52a8\u5316\u548c\u52a8\u6001\u53d8\u5316\u7684\u73af\u5883\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>\u603b\u7ed3<\/h3>\n<p>\u5bb9\u5668\u5185\u5916\u7684 Nginx \u5728\u5904\u7406\u8bf7\u6c42\u7684\u6838\u5fc3\u903b\u8f91\u4e0a\u76f8\u4f3c\uff0c\u4f46\u5bb9\u5668\u5185\u7684 Nginx \u9700\u8981\u5904\u7406\u989d\u5916\u7684\u7f51\u7edc\u5c42\u3001\u7aef\u53e3\u6620\u5c04\u3001\u670d\u52a1\u53d1\u73b0\u7b49\u95ee\u9898\u3002\u5bb9\u5668\u5185\u7684\u90e8\u7f72\u66f4\u7075\u6d3b\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u4e0e\u73b0\u4ee3\u7684\u5bb9\u5668\u7f16\u6392\u5de5\u5177\u96c6\u6210\uff0c\u800c\u5bb9\u5668\u5916\u7684 Nginx \u66f4\u76f4\u63a5\uff0c\u4f46\u5728\u591a\u5bb9\u5668\u548c\u5206\u5e03\u5f0f\u73af\u5883\u4e2d\u53ef\u80fd\u9700\u8981\u624b\u52a8\u7ef4\u62a4\u66f4\u591a\u914d\u7f6e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u975e\u539f\u521b\u3001ChatGPT \u95ee\u7b54\u751f\u6210 \u5bb9\u5668\u5185\u548c\u5bb9\u5668\u5916\u7684 Nginx \u5728\u5904\u7406\u5916\u90e8\u8bf7\u6c42\u7684\u6d41\u7a0b\u4e0a\uff0c\u672c\u8d28\u4e0a\u6ca1\u6709\u592a\u5927\u7684\u533a\u522b\uff0c&#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/t.n-years.com\/?p=6485\">\u9605\u8bfb\u66f4\u591a<i class=\"fa fa-angle-double-right\"><\/i><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[123],"tags":[147],"class_list":["post-6485","post","type-post","status-publish","format-standard","hentry","category-123","tag-147"],"_links":{"self":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6485","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6485"}],"version-history":[{"count":1,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6485\/revisions"}],"predecessor-version":[{"id":6486,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6485\/revisions\/6486"}],"wp:attachment":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6485"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6485"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6485"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}