<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Nginx反向代理設(shè)置

          共 3879字,需瀏覽 8分鐘

           ·

          2021-05-28 21:39

          須彌零一

          文章轉(zhuǎn)自:
          https://xuexb.github.io/learn-nginx/example/proxy_pass.html

          Nginx反向代理設(shè)置

          說到 Nginx 就不得不說 Nginx 的反向代理是多么的好用,一個(gè)指令 proxy_pass 搞定反向代理,對于接口代理、負(fù)載均衡很是實(shí)用,但 proxy_pass 指令后面的參數(shù)很有講究。
          網(wǎng)上有很多什么絕對路徑、相對路徑的說法,其實(shí)在實(shí)際的應(yīng)用中就分為兩種情況:

          url 只是 host

          這里指不包含 $uri ,如:

          ?http://host - √?https://host - √?http://host:port - √?https://host:port - √?http://host/ - x?http://host:port/ - x

          這時(shí)候 location 匹配的完整路徑將直接透傳給 url ,如:
          // 訪問:/                               后端:/// 訪問:/api/xx                         后端:/api/xx// 訪問:/api/xx?aa                      后端:/api/xx?aalocation / {    proxy_pass http://node:8080;}// 訪問:/api/                           后端:/api/// 訪問:/api/xx                         后端:/api/xx// 訪問:/api/xx?aa                      后端:/api/xx?aa// 訪問:/api-xx?aa                      后端:location /api/ {    proxy_pass http://node:8080;}// 訪問:/api/                           后端:/api/// 訪問:/api/xx                         后端:/api/xx// 訪問:/api/xx?aa                      后端:/api/xx?aa// 訪問:/api-xx?aa                      后端:/api-xx?aalocation /api {    proxy_pass http://node:8080;}

          url 包含路徑

          注意,這里的路徑哪怕只是一個(gè) / 也是存在的,如:

          ?http://host - x?https//host/ - √?http://host:port - x?https://host:port/ - √?http://host/api - √?http://host/api/ - √

          當(dāng) proxy_pass url 的 url 包含路徑時(shí),匹配時(shí)會根據(jù) location 的匹配后的鏈接透傳給 url ,注意匹配后就是這樣:
          location 規(guī)則訪問的原始鏈接匹配之后的路徑
          location //
          location //aa
          location //a/b/c?da/b/c?d
          location /a//a/
          location /a//a/b/c?db/c?d
          明白匹配之后的路徑后,在 proxy_pass url 包含路徑時(shí),將會把匹配之后的路徑透傳給 url ,如:
          // 訪問:/                               后端:/// 訪問:/api/xx                         后端:/api/xx// 訪問:/api/xx?aa                      后端:/api/xx?aalocation / {    proxy_pass http://node:8080/;}// 訪問:/api/                           后端:/// 訪問:/api/xx                         后端:/xx// 訪問:/api/xx?aa                      后端:/xx?aa// 訪問:/api-xx?aa                      未匹配location /api/ {    proxy_pass http://node:8080/;}// 訪問:/api                            后端:/// 訪問:/api/                           后端://// 訪問:/api/xx                         后端://xx// 訪問:/api/xx?aa                      后端://xx?aa// 訪問:/api-xx?aa                      后端:/-xx?aalocation /api {    proxy_pass http://node:8080/;}// 訪問:/api/                           后端:/v1// 訪問:/api/xx                         后端:/v1xx// 訪問:/api/xx?aa                      后端:/v1xx// 訪問:/api-xx?aa                      未匹配location /api/ {    proxy_pass http://node:8080/v1;}// 訪問:/api/                           后端:/v1/// 訪問:/api/xx                         后端:/v1/xx// 訪問:/api/xx?aa                      后端:/v1/xx// 訪問:/api-xx?aa                      未匹配location /api/ {    proxy_pass http://node:8080/v1/;}
          由以上規(guī)則可以看出,當(dāng) proxy_pass url 中包含路徑時(shí),結(jié)尾的 / 最好同 location 匹配規(guī)則一致。

          當(dāng) proxy_pass 遇到正則

          當(dāng) location 以正則形式匹配時(shí),proxy_pass 就不能以 / 結(jié)束了,也就是不能包含路徑了,比如錯(cuò)誤的:
          location ~* ^/api/ {    proxy_pass http://host/;}location / {    if ($uri ~* ^/api/) {        proxy_pass http://host/;    }}
          解決辦法就是把鏈接中的路徑去掉。

          重寫代理鏈接 - url rewrite

          當(dāng)原始鏈接(瀏覽器訪問的鏈接)和代理服務(wù)器鏈接規(guī)則不一致時(shí),可以使用 Nginx URL Rewrite 功能去動態(tài)的重寫,如:
          location ~* ^/api/ {    rewrite ^/api/(.*) /?path=$1 break;    proxy_pass http://node:8080;}
          以上請求會把匹配 /api/的鏈接重寫為 /?path= 的鏈接透傳給 node:8080 服務(wù),有意思的是當(dāng)使用 rewrite 指令并且生效后,proxy_pass url 鏈接中的路徑會被忽略,如:
          // 訪問:/                               后端:/node/// 訪問:/api                            后端:/node/api// 訪問:/api/                           后端:/?path=// 訪問:/api/a/b/c                      后端:/?path=a/b/clocation / {    rewrite ^/api/(.*) /?path=$1 break;    proxy_pass http://node:8080/node/;}


          ---- END ----



          歡迎關(guān)注我的公眾號“須彌零一”,更多技術(shù)文章第一時(shí)間推送。


          瀏覽 45
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評論
          圖片
          表情
          推薦
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  爱草av | 日本親子亂子偷XXXX50 | 少妇淫荡视频 | 人人搡人人肉久久精品 | 成人做爱视频在线观看免费版网站 |