{"id":1334,"date":"2024-06-13T10:06:02","date_gmt":"2024-06-13T02:06:02","guid":{"rendered":"https:\/\/blog.quantoyo.com\/?p=1334"},"modified":"2024-06-13T10:06:02","modified_gmt":"2024-06-13T02:06:02","slug":"nginx-%e7%9a%84%e8%b2%a0%e8%bc%89%e5%b9%b3%e8%a1%a1","status":"publish","type":"post","link":"https:\/\/blog.quantoyo.com\/?p=1334","title":{"rendered":"nginx \u7684\u8ca0\u8f09\u5e73\u8861"},"content":{"rendered":"\n<p>\u7c21\u55ae\u7684\u8ca0\u8f09\u5e73\u8861\u7bc4\u4f8b<\/p>\n\n\n\n<!--more-->\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background has-small-font-size\"><code>#user  nobody;\nworker_processes  1;\nevents {\n    worker_connections  1024;\n}\n\nhttp {\n    include       mime.types;\n    default_type  application\/octet-stream;\n\n    upstream backend1 {\n        server www1.example.com;         #\u6b63\u5e38\u7528\u9019\u500b\n        server www2.example.com;         #\u6b63\u5e38\u7528\u9019\u500b\n        server www3.example.com;         #\u6b63\u5e38\u7528\u9019\u500b\n    }\n\n    upstream backend2 {\n        server www1.example.com;         #\u6b63\u5e38\u7528\u9019\u500b\n        server nowork.example.com down;    #\u66ab\u6642\u4e0d\u7528\u7684....\n        server backup.example.com backup;  #\u5099\u63f4\n    }\n\n    upstream backend3 {                  \n        server www.example.com max_fails=1 fail_timeout=10s;    #\u5065\u5eb7\u6aa2\u67e5\uff0c\u932f\u8aa4\u4e00\u6b21\u6703\u9032\u5165\u4f11\u7720\u72c0\u614b10\u79d2\n        server bad.example.com max_fails=1 fail_timeout=10s;\n        server bak.example.com max_fails=1 fail_timeout=10s;\n    }\n\n    upstream backend4 {\n        server www.example.com weight=3;   #\u9810\u8a2d\u662f1 \u6240\u4ee5\u9019\u500b\u6703\u6536\u5230\u4e09\u500d\u7684\u91cf\n        server bad.example.com;\n        server bak.example.com;\n    }\n\n    server {\n        listen       80;\n        server_name  localhost;                             #\u53ef\u4ee5\u6539\u6210\u771f\u6b63\u7684domain\n        add_header 'Access-Control-Allow-Origin' '*';       #\u5141\u8a31\u6240\u6709\u4f86\u6e90\u8a2a\u554f\n\n        location \/ {\n            add_header Access-Control-Allow-Origin *;\n            try_files $uri @backend;\n        }\n\n        location @backend {\n            proxy_pass http:\/\/backend;                         #\u6307\u5b9a backend \u9019\u500b\u670d\u52d9\u7576\u4f5c\u5f8c\u7aef\n            #proxy_next_upstream error timeout http_500;        #\u5982\u679c\u56de\u61c9500 error\/ timeout \u6703\u5617\u8a66\u4e0b\u4e00\u53f0 \n\n            proxy_set_header X-Real-IP $remote_addr;            #\u628aIP\u9001\u7d66\u53cd\u5411\u4ee3\u7406\u7684 server\n            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;    #\u628aProtocol\u9001\u7d66\u53cd\u5411\u4ee3\u7406\u7684 server\n            proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto;     #\u628aProtocol\u9001\u7d66\u53cd\u5411\u4ee3\u7406\u7684 server\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u9019\u88e1\u63d0\u4f9b\u4e86 upstream \u7684\u5e7e\u7a2e\u8a2d\u5b9a\u65b9\u6cd5\uff08\u53ef\u4ee5\u6df7\u7528\uff09<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>proxy_pass http:\/\/backend; \u9019\u908a\u53ef\u4ee5\u6307\u5b9a\u8981\u7528\u54ea\u5957 upstream<\/li>\n\n\n\n<li>\u6b63\u5e38\u4f86\u8aaa\u6703\u4f7f\u7528 backend1 \u5c31\u53ef\u4ee5\u90e8\u7f72\u591a\u53f0\u5f8c\u7aef\u4f3a\u670d\u5668<\/li>\n\n\n\n<li>backend2 \u63d0\u4f9b\u4e86 down \/backup \u7684\u63cf\u8ff0\uff0cdown\u4e0d\u6703\u88ab\u7528\u5230\uff0c\u4f46\u662fbackup \u5728\u6b63\u5e38\u6a5f\u5668\u5931\u6548\u6642\u6703\u88ab\u62ff\u4f86\u4f7f\u7528<\/li>\n\n\n\n<li>backend3 \u662f\u5065\u5eb7\u6aa2\u67e5\u6a5f\u5236\uff0c\u53ef\u4ee5\u5728\u5617\u8a66\u4e00\u6b21(max_fail \u8a2d\u5b9a\u503c) \u5931\u6557\u5f8c\uff0c\u8b93\u9019\u500bserver \u5148\u66ab\u6642\u4e0d\u88ab\u4f7f\u752810\u79d2\u9418\uff0c\u7576\u4e0b\u6703\u5148\u5617\u8a66\u4f7f\u7528\u5176\u4ed6\u7684server <\/li>\n\n\n\n<li>backend4 \u63d0\u4f9b \u6b0a\u91cd\u7684\u8cc7\u8a0a\uff0c\u6b63\u5e38\u4e0d\u5beb\u7684\u8a71\u90fd\u662f1 \uff0c\u4f46\u53ef\u4ee5\u52a0\u4e0aweight \u4f86\u8abf\u6574\u5404\u500bserver \u7684\u6b0a\u91cd\u6bd4\u4f8b\uff0c\u7562\u7adf\u4e0d\u662f\u5168\u90e8\u6a5f\u5668\u6548\u80fd\u90fd\u4e00\u6a23<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>\u7c21\u55ae\u7684\u8ca0\u8f09\u5e73\u8861\u7bc4\u4f8b<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[220,218,202,18,207,219],"class_list":["post-1334","post","type-post","status-publish","format-standard","hentry","category-technology","tag-backend","tag-balance","tag-nginx","tag-proxy","tag-proxy_pass","tag-219"],"_links":{"self":[{"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=\/wp\/v2\/posts\/1334","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1334"}],"version-history":[{"count":1,"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=\/wp\/v2\/posts\/1334\/revisions"}],"predecessor-version":[{"id":1335,"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=\/wp\/v2\/posts\/1334\/revisions\/1335"}],"wp:attachment":[{"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1334"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}