
我在連接正向代理和反向代理時遇到問題,兩者均受tinyproxy(版本 1.8.3)支援。不知道是不是配置的問題。
我正在使用 4 個運行 Debian GNU/Linux 8.9 (jessie) 的虛擬機器來測試這些代理程式。這些機器代表客戶端、正向代理、反向代理和伺服器。我正在設定轉發代理和反向代理之間的通訊。
伺服器正在運行 Apache 伺服器,它會傳回預設的 HTML 頁面,顯示「It Works」。
所需的行為是客戶端透過正向代理與伺服器進行通信,該正向代理上游到與伺服器互動的反向代理:客戶端(10.0.2.33) -> 正向代理(10.0.2.35) -> 反向代理(10.0.2.33) -> 正向代理(10.0.2.35) -> 反向代理(10.0.2.33) 2.36) -> 伺服器 (10.0.2.34)
為了測試系統,我在客戶端電腦中使用以下curl命令:
curl -v --proxy http://10.0.2.35:8888 http://10.0.2.34:80/
轉送代理設定檔:
## tinyproxy.conf -- tinyproxy daemon configuration file
User nobody
Group nogroup
Port 8888
Listen 10.0.2.35
BindSame yes
Timeout 600
DefaultErrorFile "/usr/share/tinyproxy/default.html"
StatFile "/usr/share/tinyproxy/stats.html"
Logfile "/var/log/tinyproxy/tinyproxy.log"
#Syslog On
LogLevel Info
PidFile "/var/run/tinyproxy/tinyproxy.pid"
#Upstream 10.0.2.36:8888 "10.0.2.0/24"
Upstream 10.0.2.36:8888
MaxClients 100
MinSpareServers 2
MaxSpareServers 5
StartServers 2
MaxRequestsPerChild 0
Allow 127.0.0.1
Allow 10.0.2.0/24
ViaProxyName "tinyproxy1"
ConnectPort 8888
ConnectPort 80
# The following two ports are used by SSL.
ConnectPort 443
ConnectPort 563
反向代理設定檔:
## tinyproxy.conf -- tinyproxy daemon configuration file
User nobody
Group nogroup
Port 8888
Listen 10.0.2.36
BindSame yes
Timeout 600
StatFile "/usr/share/tinyproxy/stats.html"
Logfile "/var/log/tinyproxy/tinyproxy.log"
#Syslog On
LogLevel Info
PidFile "/var/run/tinyproxy/tinyproxy.pid"
no upstream "10.0.2.34:80"
#no upstream "."
MaxClients 5
MinSpareServers 2
MaxSpareServers 5
StartServers 2
MaxRequestsPerChild 0
Allow 127.0.0.1
Allow 10.0.2.0/24
Allow 10.0.2.35
ViaProxyName "tinyproxy2"
ConnectPort 8888
ConnectPort 80
# The following two ports are used by SSL.
ConnectPort 443
ConnectPort 563
ReversePath "/" "http://10.0.2.34:80/"
ReversePath "/wired/" "http://www.wired.com/"
ReverseOnly Yes
ReverseMagic Yes
ReverseBaseURL "http://10.0.2.34:80/"
這些是Wireshark捕獲的TCP流:
流程[客戶端 <-> 轉發代理]
GET http://10.0.2.34:80/ HTTP/1.1
User-Agent: curl/7.38.0
Host: 10.0.2.34
Accept: */*
Proxy-Connection: Keep-Alive
HTTP/1.0 400 Bad Request
Via: 1.1 tinyproxy1 (tinyproxy/1.8.3)
Server: tinyproxy/1.8.3
Content-Type: text/html
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
<head><title>400 Bad Request</title></head>
<body>
<h1>Bad Request</h1>
<p>Request has an invalid URL</p>
<hr />
<p><em>Generated by tinyproxy version 1.8.3.</em></p>
</body>
</html>
流程[正向代理<->反向代理]
GET http://10.0.2.34:80/ HTTP/1.0
Host: 10.0.2.34
Connection: close
Via: 1.1 tinyproxy1 (tinyproxy/1.8.3)
User-Agent: curl/7.38.0
Accept: */*
HTTP/1.0 400 Bad Request
Server: tinyproxy/1.8.3
Content-Type: text/html
Connection: close
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
<head><title>400 Bad Request</title></head>
<body>
<h1>Bad Request</h1>
<p>Request has an invalid URL</p>
<hr />
<p><em>Generated by tinyproxy version 1.8.3.</em></p>
</body>
</html>
這些是 tinyproxy 日誌檔案的輸出:
正向代理
CONNECT Dec 10 22:05:08 [2788]: Connect (file descriptor 6): 10.0.2.33 [10.0.2.33] at [10.0.2.35]
CONNECT Dec 10 22:05:08 [2788]: Request (file descriptor 6): GET http://10.0.2.34:80/ HTTP/1.1
INFO Dec 10 22:05:08 [2788]: Found upstream proxy 10.0.2.36:8888 for 10.0.2.34
CONNECT Dec 10 22:05:08 [2788]: Established connection to upstream proxy "10.0.2.36" using file descriptor 7.
INFO Dec 10 22:05:08 [2788]: Closed connection between local client (fd:6) and remote client (fd:7)
反向代理
CONNECT Dec 10 22:05:08 [4487]: Connect (file descriptor 6): 10.0.2.35 [10.0.2.35] at [10.0.2.36]
CONNECT Dec 10 22:05:08 [4487]: Request (file descriptor 6): GET http://10.0.2.34:80/ HTTP/1.0
ERROR Dec 10 22:05:08 [4487]: Bad request
INFO Dec 10 22:05:08 [4487]: no entity
目前,我可以在反向代理機器中訪問伺服器頁面,“curl”inghttp://10.0.2.34:80/和http://10.0.2.36:8888/因為我的反向代理配置(ReversePath)。下圖顯示了反向代理計算機中的 Wireshark 擷取。
透過所有這些配置,我在客戶端電腦中得到的只是來自反向代理的 400 bad request 錯誤訊息。
答案1
這個問題已經解決了。測試這一點的方法不是直接向伺服器請求頁面,而是向反向代理機器請求頁面。另外,正向代理不應將流量上傳至反向代理。
如果正向代理將流量上傳到上游,則反向代理無法理解傳送到主機/伺服器的請求,因為它們的工作方式不同。
轉送代理的正確配置必須排除以下行:
upstream 10.0.2.36:8888
因為反向代理有以下規則:
ReversePath “/” “http://10.0.2.34:80/”
當請求伺服器資源(來自 Apache 的“It Works”頁面)時,一旦我們請求“,反向代理(10.0.2.36)將請求伺服器(10.0.2.34)”http://10.0.2.36:8888/」
我們應該使用以下命令運行curl到反向代理:
curl -v --proxy http://10.0.2.35:8888 http://10.0.2.36:8888/
然後我們可以看到配置有效,輸出如下:
root@debian:/home/debian# curl -v --proxy http://10.0.2.35:8888 http://10.0.2.36:8888
* Rebuilt URL to: http://10.0.2.36:8888/
* Hostname was NOT found in DNS cache
* Trying 10.0.2.35...
* Connected to 10.0.2.35 (10.0.2.35) port 8888 (#0)
> GET http://10.0.2.36:8888/ HTTP/1.1
> User-Agent: curl/7.38.0
> Host: 10.0.2.36:8888
> Accept: */*
> Proxy-Connection: Keep-Alive
>
< HTTP/1.1 200 OK
< Via: 1.0 tinyproxy2 (tinyproxy/1.8.3), 1.1 tinyproxy1 (tinyproxy/1.8.3)
< Last-Modified: Mon, 11 Jun 2007 18:53:14 GMT
< Date: Tue, 12 Dec 2017 23:01:37 GMT
< Content-Type: text/html
< ETag: "2d-432a5e4a73a80"
< Set-Cookie: yummy_magical_cookie=/; path=/
* Server Apache/2.4.29 (Unix) is not blacklisted
< Server: Apache/2.4.29 (Unix)
< Content-Length: 45
< Accept-Ranges: bytes
<
<html><body><h1>It works!</h1></body></html>
* Connection #0 to host 10.0.2.35 left intact