{"id":121300,"date":"2018-10-01T08:28:24","date_gmt":"2018-10-01T00:28:24","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=121300"},"modified":"2018-09-25T14:29:50","modified_gmt":"2018-09-25T06:29:50","slug":"nginx-proxy-pass","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/nginx-proxy-pass.html","title":{"rendered":"\u89e3\u6790nginx\u4e4bproxy_pass\u6307\u4ee4"},"content":{"rendered":"
\u4e00\u3001proxy_pass\u7684nginx\u5b98\u65b9\u6307\u5357<\/strong><\/div>\n

nginx\u4e2d\u6709\u4e24\u4e2a\u6a21\u5757\u90fd\u6709proxy_pass\u6307\u4ee4\u3002<\/p>\n

ngx_http_proxy_module\u7684proxy_pass\uff1a<\/p>\n

\r\n\u8bed\u6cd5: proxy_pass URL;\r\n\u573a\u666f: location, if in location, limit_except\r\n\u8bf4\u660e: \u8bbe\u7f6e\u540e\u7aef\u4ee3\u7406\u670d\u52a1\u5668\u7684\b\u534f\u8bae(protocol)\u548c\u5730\u5740(address),\u4ee5\u53calocation\u4e2d\u53ef\u4ee5\u5339\u914d\u7684\u4e00\u4e2a\u53ef\u9009\u7684URI\u3002\u534f\u8bae\u53ef\u4ee5\u662f\"http\"\u6216\"https\"\u3002\u5730\u5740\u53ef\u4ee5\u662f\u4e00\u4e2a\u57df\u540d\u6216ip\u5730\u5740\u548c\u7aef\u53e3\uff0c\u6216\u8005\u4e00\u4e2a unix-domain socket \u8def\u5f84\u3002  \r\n\u8be6\u89c1\u5b98\u65b9\u6587\u6863: http:\/\/nginx.org\/en\/docs\/http\/ngx_http_proxy_module.html#proxy_pass\r\nURI\u7684\u5339\u914d\uff0c\u672c\u6587\u7b2c\u56db\u90e8\u5206\b\u91cd\u70b9\u8ba8\u8bba\u3002\r\n<\/pre>\n

ngx_stream_proxy_module\u7684proxy_pass\uff1a<\/p>\n

\r\n\u8bed\u6cd5: proxy_pass address;\r\n\u573a\u666f: server\r\n\u8bf4\u660e: \u8bbe\u7f6e\u540e\u7aef\u4ee3\u7406\u670d\u52a1\u5668\u7684\u5730\u5740\u3002\u8fd9\u4e2a\u5730\u5740(address)\u53ef\u4ee5\u662f\u4e00\u4e2a\u57df\u540d\u6216ip\u5730\u5740\u548c\u7aef\u53e3\uff0c\u6216\u8005\u4e00\u4e2a unix-domain socket\u8def\u5f84\u3002  \r\n\u8be6\u89c1\u5b98\u65b9\u6587\u6863: http:\/\/nginx.org\/en\/docs\/stream\/ngx_stream_proxy_module.html#proxy_pass\r\n<\/pre>\n
\u4e8c\u3001\u4e24\u4e2aproxy_pass\u7684\u5173\u7cfb\u548c\u533a\u522b<\/strong><\/div>\n

\u5728\u4e24\u4e2a\u6a21\u5757\b\u4e2d\uff0c\u4e24\u4e2aproxy_pass\u90fd\u662f\u7528\u6765\u505a\u540e\u7aef\u4ee3\u7406\u7684\u6307\u4ee4\u3002<\/p>\n

ngx_stream_proxy_module\u6a21\u5757\u7684proxy_pass\u6307\u4ee4\u53ea\u80fd\u5728server\u6bb5\u4f7f\u7528\u4f7f\u7528, \b\u53ea\u9700\u8981\u63d0\u4f9b\u57df\u540d\u6216ip\u5730\u5740\u548c\u7aef\u53e3\u3002\u53ef\u4ee5\u7406\u89e3\u4e3a\u7aef\u53e3\u8f6c\u53d1\uff0c\u53ef\u4ee5\u662ftcp\u7aef\u53e3\uff0c\u4e5f\u53ef\u4ee5\u662fudp\u7aef\u53e3\u3002<\/p>\n

ngx_http_proxy_module\u6a21\u5757\u7684proxy_pass\b\u6307\u4ee4\u9700\u8981\u5728location\u6bb5\uff0clocation\u4e2d\u7684if\u6bb5\uff0climit_except\u6bb5\u4e2d\u4f7f\u7528\uff0c\u5904\u7406\u9700\u8981\u63d0\u4f9b\u57df\u540d\u6216ip\u5730\u5740\u548c\u7aef\u53e3\u5916\uff0c\u8fd8\u9700\u8981\u63d0\u4f9b\u534f\u8bae\uff0c\u5982\"http\"\u6216\"https\"\uff0c\u8fd8\u6709\u4e00\u4e2a\u53ef\u9009\u7684uri\u53ef\u4ee5\u914d\u7f6e\u3002<\/p>\n

\u4e09\u3001\bproxy_pass\u7684\u5177\u4f53\u7528\u6cd5<\/strong><\/div>\n

ngx_stream_proxy_module\u6a21\u5757\u7684proxy_pass\u6307\u4ee4<\/p>\n

\r\nserver {\r\n    listen 127.0.0.1:12345;\r\n    proxy_pass 127.0.0.1:8080;\r\n}\r\n\r\nserver {\r\n    listen 12345;\r\n    proxy_connect_timeout 1s;\r\n    proxy_timeout 1m;\r\n    proxy_pass example.com:12345;\r\n}\r\n\r\nserver {\r\n    listen 53 udp;\r\n    proxy_responses 1;\r\n    proxy_timeout 20s;\r\n    proxy_pass dns.example.com:53;\r\n}\r\n\r\nserver {\r\n    listen [::1]:12345;\r\n    proxy_pass unix:\/tmp\/stream.socket;\r\n}\r\n<\/pre>\n

ngx_http_proxy_module\u6a21\u5757\u7684proxy_pass\b\u6307\u4ee4<\/p>\n

\r\nserver {\r\n    listen      80;\r\n    server_name www.test.com;\r\n\r\n    # \u6b63\u5e38\u4ee3\u7406\uff0c\u4e0d\u4fee\u6539\u540e\u7aefurl\u7684\r\n    location \/some\/path\/ {\r\n        proxy_pass http:\/\/127.0.0.1;\r\n    }\r\n\r\n    # \u4fee\u6539\u540e\u7aefurl\u5730\u5740\u7684\u4ee3\u7406\uff08\u672c\u4f8b\u540e\u7aef\u5730\u5740\u4e2d\uff0c\u6700\u540e\u5e26\u4e86\u4e00\u4e2a\u659c\u7ebf)\r\n    location \/testb {\r\n        proxy_pass http:\/\/www.other.com:8801\/;\r\n    }\r\n\r\n    # \u4f7f\u7528 if in location\r\n    location \/google {\r\n        if ( $geoip_country_code ~ (RU|CN) ) {\r\n            proxy_pass http:\/\/www.google.hk;\r\n        }\r\n    }\r\n\r\n    location \/yongfu\/ {\r\n        # \u6ca1\u6709\u5339\u914d limit_except \u7684\uff0c\u4ee3\u7406\u5230 unix:\/tmp\/backend.socket:\/uri\/\r\n        proxy_pass http:\/\/unix:\/tmp\/backend.socket:\/uri\/;;\r\n\r\n        # \u5339\u914d\u5230\u8bf7\u6c42\u65b9\u6cd5\u4e3a: PUT or DELETE, \u4ee3\u7406\u52309080\r\n        limit_except PUT DELETE {\r\n            proxy_pass http:\/\/127.0.0.1:9080;\r\n        }\r\n    }\r\n\r\n}\r\n<\/pre>\n
\u56db\u3001proxy_pass\u540e\uff0c\u540e\u7aef\u670d\u52a1\u5668\u7684url(request_uri)\u60c5\u51b5\u5206\u6790<\/strong><\/div>\n
\r\nserver {\r\n    listen      80;\r\n    server_name www.test.com;\r\n\r\n    # \u60c5\u5f62A\r\n    # \u8bbf\u95ee http:\/\/www.test.com\/testa\/aaaa\r\n    # \u540e\u7aef\u7684request_uri\u4e3a: \/testa\/aaaa\r\n    location ^~ \/testa\/ {\r\n        proxy_pass http:\/\/127.0.0.1:8801;\r\n    }\r\n    \r\n    # \u60c5\u5f62B\r\n    # \u8bbf\u95ee http:\/\/www.test.com\/testb\/bbbb\r\n    # \u540e\u7aef\u7684request_uri\u4e3a: \/bbbb\r\n    location ^~ \/testb\/ {\r\n        proxy_pass http:\/\/127.0.0.1:8801\/;\r\n    }\r\n\r\n    # \u60c5\u5f62C\r\n    # \u4e0b\u9762\u8fd9\u6bb5location\u662f\u6b63\u786e\u7684\r\n    location ~ \/testc {\r\n        proxy_pass http:\/\/127.0.0.1:8801;\r\n    }\r\n\r\n    # \u60c5\u5f62D\r\n    # \u4e0b\u9762\u8fd9\u6bb5location\u662f\u9519\u8bef\u7684\r\n    #\r\n    # nginx -t \u65f6\uff0c\u4f1a\u62a5\u5982\u4e0b\u9519\u8bef: \r\n    #\r\n    # nginx: [emerg] \"proxy_pass\" cannot have URI part in location given by regular \r\n    # expression, or inside named location, or inside \"if\" statement, or inside \r\n    # \"limit_except\" block in \/opt\/app\/nginx\/conf\/vhost\/test.conf:17\r\n    # \r\n    # \u5f53location\u4e3a\u6b63\u5219\u8868\u8fbe\u5f0f\u65f6\uff0cproxy_pass \u4e0d\u80fd\u5305\u542bURI\u90e8\u5206\u3002\u672c\u4f8b\u4e2d\u5305\u542b\u4e86\"\/\"\r\n    location ~ \/testd {\r\n        proxy_pass http:\/\/127.0.0.1:8801\/;   # \u8bb0\u4f4f\uff0clocation\u4e3a\u6b63\u5219\u8868\u8fbe\u5f0f\u65f6\uff0c\u4e0d\u80fd\u8fd9\u6837\u5199\uff01\uff01\uff01\r\n    }\r\n\r\n    # \u60c5\u5f62E\r\n    # \u8bbf\u95ee http:\/\/www.test.com\/ccc\/bbbb\r\n    # \u540e\u7aef\u7684request_uri\u4e3a: \/aaa\/ccc\/bbbb\r\n    location \/ccc\/ {\r\n        proxy_pass http:\/\/127.0.0.1:8801\/aaa$request_uri;\r\n    }\r\n\r\n    # \u60c5\u5f62F\r\n    # \u8bbf\u95ee http:\/\/www.test.com\/namea\/ddd\r\n    # \u540e\u7aef\u7684request_uri\u4e3a: \/yongfu?namea=ddd\r\n    location \/namea\/ {\r\n        rewrite    \/namea\/([^\/]+) \/yongfu?namea=$1 break;\r\n        proxy_pass http:\/\/127.0.0.1:8801;\r\n    }\r\n\r\n    # \u60c5\u5f62G\r\n    # \u8bbf\u95ee http:\/\/www.test.com\/nameb\/eee\r\n    # \u540e\u7aef\u7684request_uri\u4e3a: \/yongfu?nameb=eee\r\n    location \/nameb\/ {\r\n        rewrite    \/nameb\/([^\/]+) \/yongfu?nameb=$1 break;\r\n        proxy_pass http:\/\/127.0.0.1:8801\/;\r\n    }\r\n\r\n    access_log \/data\/logs\/www\/www.test.com.log;\r\n}\r\n\r\nserver {\r\n    listen      8801;\r\n    server_name www.test.com;\r\n    \r\n    root        \/data\/www\/test;\r\n    index       index.php index.html;\r\n\r\n    rewrite ^(.*)$ \/test.php?u=$1 last;\r\n\r\n    location ~ \\.php$ {\r\n        try_files $uri =404;\r\n        fastcgi_pass unix:\/tmp\/php-cgi.sock;\r\n        fastcgi_index index.php;\r\n        include fastcgi.conf;\r\n    }\r\n\r\n    access_log \/data\/logs\/www\/www.test.com.8801.log;\r\n}\r\n<\/pre>\n

\u6587\u4ef6: \/data\/www\/test\/test.php<\/p>\n

\r\n< ?php\r\necho '$_SERVER[REQUEST_URI]:' . $_SERVER['REQUEST_URI'];\r\n<\/pre>\n

\u901a\u8fc7\u67e5\u770b $_SERVER['REQUEST_URI'] \u7684\u503c\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u6bcf\u6b21\u8bf7\u6c42\u7684\b\u540e\u7aef\u7684request_uri\u7684\u503c\uff0c\u8fdb\u884c\u9a8c\u8bc1\u3002<\/p>\n

\u5c0f\u7ed3<\/strong><\/div>\n

\u60c5\u5f62A\u548c\u60c5\u5f62B\u8fdb\u884c\u5bf9\u6bd4\uff0c\u53ef\u4ee5\u77e5\u9053proxy_pass\u540e\u5e26\u4e00\u4e2aURI,\u53ef\u4ee5\u662f\u659c\u6760(\/)\u4e5f\u53ef\u4ee5\u662f\u5176\u4ed6uri\uff0c\u5bf9\u540e\u7aefrequest_uri\u53d8\u91cf\u7684\u5f71\u54cd\u3002<\/p>\n

\u60c5\u5f62D\u8bf4\u660e\uff0c\u5f53location\u4e3a\u6b63\u5219\u8868\u8fbe\u5f0f\u65f6\uff0cproxy_pass\u4e0d\u80fd\u5305\u542bURI\u90e8\u5206\u3002<\/p>\n

\u60c5\u5f62E\u901a\u8fc7\u53d8\u91cf($request_uri, \u4e5f\u53ef\u4ee5\u662f\u5176\u4ed6\u53d8\u91cf)\uff0c\u5bf9\u540e\u7aef\u7684request_uri\u8fdb\u884c\u6539\u5199\u3002<\/p>\n

\u60c5\u5f62F\u548c\u60c5\u5f62G\u901a\u8fc7rewrite\u914d\u5408break\u6807\u5fd7,\u5bf9url\u8fdb\u884c\u6539\u5199\uff0c\u5e76\b\u6539\u5199\u540e\u7aef\u7684request_uri\u3002\u9700\u8981\u6ce8\u610f\uff0cproxy_pass\u5730\u5740\u7684URI\u90e8\u5206\u5728\u60c5\u5f62G\u4e2d\u65e0\u6548\uff0c\u4e0d\u7ba1\u5982\u4f55\u8bbe\u7f6e\uff0c\u90fd\u4f1a\u88ab\u5ffd\u7565\u3002<\/p>\n

\n

\u539f\u6587\u6765\u81ea\uff1ahttps:\/\/my.oschina.net\/foreverich\/blog\/1512304<\/a><\/p>\n

\u672c\u6587\u5730\u5740\uff1ahttps:\/\/lrxjmw.cn\/nginx-proxy-pass.html<\/a>\u7f16\u8f91\uff1a\u4f55\u4e91\u8273\uff0c\u5ba1\u6838\u5458\uff1a\u9004\u589e\u5b9d<\/span><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"

\u4e00\u3001proxy_pass\u7684nginx\u5b98\u65b9\u6307\u5357 nginx\u4e2d\u6709\u4e24\u4e2a\u6a21\u5757\u90fd\u6709proxy_pass\u6307\u4ee4\u3002 ngx_h […]<\/p>\n","protected":false},"author":1479,"featured_media":15116,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-121300","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/121300","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1479"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=121300"}],"version-history":[{"count":5,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/121300\/revisions"}],"predecessor-version":[{"id":121572,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/121300\/revisions\/121572"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/15116"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=121300"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=121300"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=121300"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}