File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -96,26 +96,29 @@ https://myhost.github.io 'my';
9696参数和 nginx -s 相同。
9797
9898
99- ## 项目特点
99+ # 项目特点
100100
101101相比传统在线代理,本项目具有以下特点:
102102
103- ### 服务端开销低
103+ ## 服务端开销低
104104
105- 传统在线代理的原理,基本都是在服务端替换 HTML/JS/CSS 等资源中的 URL。这不仅需要对内容做大量的分析和处理,还需对流量进行解压和再压缩,消耗大量的 CPU 资源。由于服务端的逻辑较为复杂,因此大多使用 Python/PHP 等自己实现 。
105+ 传统在线代理几乎都是在服务端替换 HTML/JS/CSS 等资源中的 URL。这不仅需要对内容做大量的分析和处理,还需对流量进行解压和再压缩,消耗大量 CPU 资源。并且由于逻辑较复杂,通常使用 Python/PHP 等编程语言自己实现 。
106106
107- 为降低服务端开销,本项目使用了浏览器的一个黑科技 —— Service Worker。它能让 JS 拦截网页产生的请求,并能自定义返回内容,相当于在浏览器内部实现一个反向代理。这使得绝大部分的内容处理都可以在浏览器上完成,服务器只需纯粹的转发流量。
107+ 为降低服务端开销,本项目使用浏览器的一个黑科技 —— Service Worker。它能让 JS 拦截网页产生的请求,并能自定义返回内容,相当于在浏览器内部实现一个反向代理。这使得绝大部分的内容处理都可以在浏览器上完成,服务器只需纯粹的转发流量。
108108
109109因此本项目服务端直接使用 nginx,并且转发过程不修改内容(只修改 HTTP 头),避免了内容处理产生的巨大开销。同时得益于 nginx 丰富的功能,很多常用需求无需重新造轮子,通过简单配置即可实现。并且无论性能还是稳定性,都远高于自己实现。
110110
111- ### API 虚拟化
111+ ## API 虚拟化
112112
113- 传统在线代理大多只针对静态 URL 的替换,而忽视了动态加载 URL,以及和调用 URL 相关的网页 API。例如 a.com 反向代理 google.com,但页面中 JS 读取 ` document.domain ` 得到的仍是 a.com。这可能会导致某些业务逻辑出现问题 。
113+ 传统在线代理大多只针对静态 URL 的替换,忽视了动态 URL 以及和 URL 相关的网页 API。例如 a.com 反向代理 google.com,但页面中 JS 读取 ` document.domain ` 得到的仍是 a.com。这可能导致某些业务逻辑出现问题 。
114114
115115为缓解这个问题,本代理在页面头部注入一个 JS,用以重写绝大部分和 URL 相关的 API,使得页面中的 JS 获取到的仍是原始 URL:
116116
117117![ ] ( https://raw.githubusercontent.com/EtherDream/jsproxy-localtest/temp/hook.png )
118118
119+ 对于有些无法重写的 API,例如 ` location ` ,本代理会将代码中字面出现的 ` location ` 替换成 ` __location ` ,从而将操作转移到自定义对象上。当然对于非字面的情况(例如 ` this['lo' + 'cation'] ` ),目前还无法处理。
120+
121+
119122### 界面和接口分离
120123
121124参见下文
Original file line number Diff line number Diff line change @@ -9,7 +9,7 @@ proxy_set_header Connection $http_connection;
99
1010set $_url '';
1111set $_ver '';
12-
12+ set $_ref '';
1313
1414location = /preflight {
1515 internal;
Original file line number Diff line number Diff line change @@ -21,6 +21,9 @@ for k, v in pairs(hdrs) do
2121 elseif k == ' ext' then
2222 extHdrs = require (' cjson' ).decode (v )
2323 else
24+ if k == ' referer' then
25+ ngx .var ._ref = v
26+ end
2427 ngx .req .set_header (k , v )
2528 end
2629
Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ if ngx.worker.id() ~= 0 then
55 return
66end
77
8- local function getDevTraffic (dev )
8+ local function buildDevTrafficFn (dev )
99 -- 0 1 2 3 4 5 6 7
1010 -- eth0: bytes packets errs drop fifo frame compressed multicast
1111 -- bytes packets errs drop fifo colls carrier compressed
@@ -45,7 +45,7 @@ if fileStat == nil then
4545end
4646
4747local firstRun = true
48- local getDevTraffic = getDevTraffic (' eth0' )
48+ local getDevTraffic = buildDevTrafficFn (' eth0' )
4949
5050
5151local function updateTraffic ()
Original file line number Diff line number Diff line change @@ -29,11 +29,12 @@ http {
2929 '$time_iso8601 $_origin_id $_ver $remote_addr '
3030 '$upstream_cache_status $request_time '
3131 '$request_length $bytes_sent '
32- '$request_method $_url $status $upstream_http_access_control_allow_origin '
33- '$http_user_agent'
32+ '$request_method $_url $status $upstream_http_access_control_allow_origin '
33+ '$http_user_agent $_ref '
3434 ;
3535 access_log logs/proxy.log log_proxy buffer=64k flush=1s ;
3636
37+ # 1MB = 8000key
3738 proxy_cache_path cache
3839 levels=1:2
3940 keys_zone=my_cache:32m
Original file line number Diff line number Diff line change @@ -11,7 +11,7 @@ for v in ${NODE[@]}; do
1111 echo " $v upload ..."
1212
1313 rsync . jsproxy@$v .$HOST :server \
14- -r -p \
14+ -a \
1515 --exclude=' nginx/cache/*' \
1616 --exclude=' nginx/logs/*'
1717
You can’t perform that action at this time.
0 commit comments