最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Nginx location 和 proxy_pass路徑配置問(wèn)題小結(jié)

 更新時(shí)間:2021年09月01日 08:36:13   作者:自由早晚亂余生  
本文是基于 location 的匹配末尾是否配置 / 和 proxy_pass 末尾是否配置 / ,進(jìn)行測(cè)試,完全還原了整個(gè)測(cè)試過(guò)程,本文給大家介紹Nginx location 基本配置及相關(guān)配置文件,感興趣的朋友跟隨小編一起看看吧

本文是基于 location 的匹配末尾是否配置 / 和 proxy_pass 末尾是否配置 / ,進(jìn)行測(cè)試,完全還原了整個(gè)測(cè)試過(guò)程。幫助了解具體的情況。

一、Nginx location 基本配置

1.1、Nginx 配置文件

upstream test1{
server 127.0.0.1:8000;
}
upstream test2{
server 127.0.0.1:8000;
}
server{
	server_name  test.com;
	listen 80;
        access_log /usr/local/openresty/nginx/logs/test.com_access.log latest;
        error_log  /usr/local/openresty/nginx/logs/test.com.log error;
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_connect_timeout   3s;
        proxy_read_timeout 120s;
        proxy_send_timeout 120s;
        proxy_next_upstream error timeout invalid_header http_404 http_502 http_504 http_500;
	
        location /user/ {
			proxy_set_header Connection "";
        	proxy_http_version 1.1;
			proxy_pass http://test1/;
		}
        location / {
                proxy_set_header Connection "";
                proxy_http_version 1.1;
                proxy_pass http://test2/;
        }
}

1.2 、Python 腳本

python2 可以運(yùn)行

該腳本用于獲取請(qǐng)求內(nèi)容。 這個(gè)作為后端,也就是 proxy_pass 代理的后端。

#!/usr/bin/env python

import SimpleHTTPServer
import SocketServer

PORT = 8000

class GetHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
    def do_GET(self):
        print(self.headers)
        self.send_response(200, "")
    def do_POST(self):
        print(self.headers)
        content_length = self.headers.getheaders('content-length')
        length = int(content_length[0]) if content_length else 0
        print(self.rfile.read(length))
        self.send_response(200, "")

Handler = GetHandler
httpd = SocketServer.TCPServer(("", PORT), Handler)
httpd.serve_forever()

二、測(cè)試

2.1、測(cè)試 location

末尾存在 / 和 proxy_pass末尾存在 /

nginx配置如下

 location /user/ {
			proxy_set_header Connection "";
        	proxy_http_version 1.1;
			proxy_pass http://test1/;
		}

請(qǐng)求url

test.com/user/test.html

后端內(nèi)容

打印的內(nèi)容:

Host: test1
Content-Length: 0
User-Agent: PostmanRuntime/7.26.8
Accept: */*
Postman-Token: f2bfe770-4f44-4ee9-91c4-060f59dfb26c
Accept-Encoding: gzip, deflate, br


127.0.0.1 - - [10/Apr/2021 16:54:26] "POST /test.html HTTP/1.1" 200 -

小結(jié)論:proxy_pass 地址加了 / 的話, 請(qǐng)求 test.com/user/test.html 實(shí)際請(qǐng)求是 http://test1/test.html。

2.2、測(cè)試 location

末尾存在 / 和 proxy_pass末尾不存在 /

nginx配置如下

 location /user/ {
			proxy_set_header Connection "";
        	proxy_http_version 1.1;
			proxy_pass http://test1;
		}

請(qǐng)求url

test.com/user/test.html

后端內(nèi)容

打印的內(nèi)容:

Host: test1
Content-Length: 0
User-Agent: PostmanRuntime/7.26.8
Accept: */*
Postman-Token: e33d0a2c-1965-4152-b87c-94fca50f2899
Accept-Encoding: gzip, deflate, br

127.0.0.1 - - [10/Apr/2021 16:57:18] "POST /user/test.html HTTP/1.1" 200 -

小結(jié)論: proxy_pass 地址不加了 / 的話, 請(qǐng)求 test.com/user/test.html 實(shí)際請(qǐng)求是 http://test1/user/test.html

2.3、測(cè)試三 location

不加末尾 / 且 proxy_pass 不加 末尾 /

nginx配置如下

 location /user {
			proxy_set_header Connection "";
        	proxy_http_version 1.1;
			proxy_pass http://test1;
		}

請(qǐng)求url

test.com/user/test.html

后端內(nèi)容

打印的內(nèi)容:

Host: test1
Content-Length: 0
User-Agent: PostmanRuntime/7.26.8
Accept: */*
Postman-Token: 31cd33c6-4c95-41b5-a095-28cdc7113dcd
Accept-Encoding: gzip, deflate, br

127.0.0.1 - - [10/Apr/2021 16:59:34] "POST /user/test.html HTTP/1.1" 200 -

請(qǐng)求 test.com/user/test.html 實(shí)際請(qǐng)求是 http://test1/user/test.html

2.4、location 不加

末尾 / 且 proxy_pass 加 末尾 /

nginx配置如下

  location /user {
			proxy_set_header Connection "";
        	proxy_http_version 1.1;
			proxy_pass http://test1/;
		}

請(qǐng)求url

test.com/user/test.html

后端內(nèi)容

打印的內(nèi)容:

Host: test1
Content-Length: 0
User-Agent: PostmanRuntime/7.26.8
Accept: */*
Postman-Token: d0f4b83f-6482-41ba-8a01-c059eececc2d
Accept-Encoding: gzip, deflate, br

127.0.0.1 - - [10/Apr/2021 17:00:21] "POST //test.html HTTP/1.1" 200 -

請(qǐng)求 test.com/user/test.html 實(shí)際請(qǐng)求是 http://test1//test.html

2.5、location 末尾

/ proxy_pass 末尾其他有路徑,且末尾加 /

nginx配置如下

   location /user/ {
			proxy_set_header Connection "";
        	proxy_http_version 1.1;
			proxy_pass http://test1/haha/;
		}

請(qǐng)求url

test.com/user/test.html

后端內(nèi)容

打印的內(nèi)容:

Host: test1
Content-Length: 0
User-Agent: PostmanRuntime/7.26.8
Accept: */*
Postman-Token: 6447cf0b-5988-4f96-81a4-2b621fe32604
Accept-Encoding: gzip, deflate, br

127.0.0.1 - - [10/Apr/2021 17:03:27] "POST /haha/test.html HTTP/1.1" 200 -

請(qǐng)求 test.com/user/test.html 實(shí)際請(qǐng)求是 http://test1/haha/test.html

2.6、 location 末尾

/ proxy_pass 末尾其他有路徑,且末尾不加 /

nginx配置如下

 location /user/ {
			proxy_set_header Connection "";
        	proxy_http_version 1.1;
			proxy_pass http://test1/haha;
		}

請(qǐng)求url

test.com/user/test.html

后端內(nèi)容

打印的內(nèi)容:

Host: test1
Content-Length: 0
User-Agent: PostmanRuntime/7.26.8
Accept: */*
Postman-Token: 32fb2a50-1e7c-4131-9804-1828e21ca841
Accept-Encoding: gzip, deflate, br

127.0.0.1 - - [10/Apr/2021 17:05:03] "POST /hahatest.html HTTP/1.1" 200 -

請(qǐng)求 test.com/user/test.html 實(shí)際請(qǐng)求是 http://test1/hahatest.html

三、總結(jié)

序號(hào) 訪問(wèn)URL location配置 proxy_pass配置 后端接收的請(qǐng)求 備注
1 test.com/user/test.html /user/ http://test1/ /test.html
2 test.com/user/test.html /user/ http://test1 /user/test.html
3 test.com/user/test.html /user http://test1 /user/test.html
4 test.com/user/test.html /user http://test1/ //test.html
5 test.com/user/test.html /user/ http://test1/haha/ /haha/test.html
6 test.com/user/test.html /user/ http://test1/haha /hahatest.html

注意上表格中的后端是指 python 腳本對(duì)應(yīng)的web服務(wù)。

在日常的web網(wǎng)站部署中,經(jīng)常會(huì)用到 nginxproxy_pass 反向代理,有一個(gè)配置需要弄清楚:配置 proxy_pass 時(shí),

  • 當(dāng)在后面的 upstram_name 后面出現(xiàn)了 /,相當(dāng)于是絕對(duì)根路徑,則 nginx 不會(huì)把 location 中匹配的路徑部分代理走;
  • 如果沒(méi)有 /,則會(huì)把匹配的路徑部分也給代理走。

到此這篇關(guān)于Nginx location 和 proxy_pass路徑配置詳解的文章就介紹到這了,更多相關(guān)Nginx location 和 proxy_pass路徑配置內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • nginx如何使用openssl自簽名實(shí)現(xiàn)https登錄

    nginx如何使用openssl自簽名實(shí)現(xiàn)https登錄

    這篇文章主要介紹了nginx使用openssl自簽名實(shí)現(xiàn)https登錄,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-08-08
  • 關(guān)閉nginx空主機(jī)頭 防止nginx空主機(jī)頭及惡意域名指向

    關(guān)閉nginx空主機(jī)頭 防止nginx空主機(jī)頭及惡意域名指向

    nginx的默認(rèn)配置中的虛擬主機(jī)允許用戶通過(guò)IP訪問(wèn),或者通過(guò)未設(shè)置的域名訪問(wèn),比如有人惡意把他自己的域名指向了你的ip,需要的朋友可以參考下
    2016-09-09
  • Nginx下配置pathinfo及ThinkPHP的URL Rewrite模式支持

    Nginx下配置pathinfo及ThinkPHP的URL Rewrite模式支持

    這篇文章主要介紹了Nginx下配置pathinfo及ThinkPHP的URL Rewrite模式支持,使用Nginx運(yùn)行ThinkPHP的必備配置,需要的朋友可以參考下
    2015-07-07
  • Centos下編譯安裝Nginx教程詳解

    Centos下編譯安裝Nginx教程詳解

    這篇文章主要介紹了Centos下編譯安裝Nginx的教程詳解,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-12-12
  • 詳細(xì)nginx多域名配置的方法

    詳細(xì)nginx多域名配置的方法

    Nginx綁定多個(gè)域名,可通過(guò)把多個(gè)域名規(guī)則寫(xiě)一個(gè)配置文件里實(shí)現(xiàn),也可通過(guò)分別建立多個(gè)域名配置文件實(shí)現(xiàn),為了管理方便,建議每個(gè)域名建一個(gè)文件,有些同類(lèi)域名則可寫(xiě)在一個(gè)總的配置文件里。下面這篇文章就來(lái)詳細(xì)看看nginx多域名配置的方法,有需要的朋友們可以參考。
    2016-12-12
  • 解讀nginx反向代理location和proxy_pass的映射關(guān)系

    解讀nginx反向代理location和proxy_pass的映射關(guān)系

    這篇文章主要介紹了解讀nginx反向代理location和proxy_pass的映射關(guān)系,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • Nginx?禁止直接訪問(wèn)目錄或文件的操作方法

    Nginx?禁止直接訪問(wèn)目錄或文件的操作方法

    Nginx?默認(rèn)是不允許列出整個(gè)目錄的,那么需要這樣的功能怎么操作呢,下面小編給大家介紹下Nginx?禁止直接訪問(wèn)目錄或文件的方法,需要的朋友可以參考下
    2022-10-10
  • Nginx配置srcache_nginx模塊搭配Redis建立緩存系統(tǒng)

    Nginx配置srcache_nginx模塊搭配Redis建立緩存系統(tǒng)

    這篇文章主要介紹了Nginx配置srcache_nginx模塊搭配Redis建立緩存系統(tǒng)的方法,文中關(guān)于Nginx模塊和Redis數(shù)據(jù)庫(kù)的安裝就不再說(shuō)明了,這里只關(guān)注配置搭建階段,需要的朋友可以參考下
    2016-01-01
  • 詳解Nginx如何處理WebSocket連接

    詳解Nginx如何處理WebSocket連接

    在當(dāng)今互聯(lián)網(wǎng)的世界中,實(shí)時(shí)通信變得越來(lái)越重要,WebSocket 作為一種實(shí)現(xiàn)實(shí)時(shí)雙向通信的技術(shù),正被廣泛應(yīng)用于各種場(chǎng)景,而 Nginx 作為一款高性能的 Web 服務(wù)器和反向代理服務(wù)器,在處理 WebSocket 連接方面也有著出色的表現(xiàn),本文介紹了Nginx如何處理WebSocket連接
    2024-07-07
  • 詳解Nginx輪詢算法底層實(shí)現(xiàn)的方法

    詳解Nginx輪詢算法底層實(shí)現(xiàn)的方法

    這篇文章主要介紹了詳解Nginx輪詢算法底層實(shí)現(xiàn)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-01-01

最新評(píng)論

珠海市| 临夏县| 光泽县| 巢湖市| 和静县| 台山市| 德令哈市| 班玛县| 岳普湖县| 龙川县| 钦州市| 麻江县| 固原市| 高雄县| 元江| 平湖市| 海伦市| 建瓯市| 邻水| 西畴县| 宝鸡市| 宾川县| 东安县| 醴陵市| 宿松县| 沙洋县| 横峰县| 黑河市| 怀仁县| 蓝田县| 葵青区| 三台县| 弋阳县| 兰坪| 石嘴山市| 大竹县| 梨树县| 那曲县| 周口市| 武强县| 潞西市|