Coder Social home page Coder Social logo

Comments (17)

avwo avatar avwo commented on April 30, 2024 1

可以用正则解决:

# 以域名 www.test.com 为例
# 把https请求转成http
https://www.test.com http://www.test.com
# 把根路径的请求转发到指定的ip和端口
/^https?:\/\/www\.test\.com(:\d*)?\/?(\?|$)/ 192.168.30.118:8877
# 处理后缀为gif|jpg|jpeg|png|bmp|swf|ico|js|css|html的请求
/^https?:\/\/www\.test\.com(:\d*)?\/\.(gif|jpg|jpeg|png|bmp|swf|ico|js|css|html)(\?|$)/i 127.0.0.1:8080

from whistle.

avwo avatar avwo commented on April 30, 2024

rulesFile关联脚本文件或Values里面设置的脚本,利用脚本判断url及请求的headers动态设置规则,如:www.test.com rules:///User/xxx/test.js

from whistle.

a526672351 avatar a526672351 commented on April 30, 2024

看了加入了rulesFile ,文档不是好清晰,能根据上面nginx配置写个Dome吗?

from whistle.

avwo avatar avwo commented on April 30, 2024

test.js:

url = url.replace(/\?.*$/, '');
if (/^https?:/.test(url)) {
  var index = url.indexOf('://') + 3;
  url = url.substring(index);
  index = url.indexOf('/');
  if (index === -1) {
    host = url;
    url = '/';
  } else {
    host = url.substring(0, index);
    url = url.substring(index);
  }
  var isRoot = url === '/';
  if (isRoot || /\.(gif|jpg|jpeg|png|bmp|swf|ico|js|css|html)$/i.test(url)) {
    rules.push(host + ' http://' + host);
    rules.push(host + ' ' + (isRoot ? '192.168.30.118:8877' : '127.0.0.1:8080'));
  }
}

示例图:

rulesFile

from whistle.

avwo avatar avwo commented on April 30, 2024

protocol://{test.js} 表示取Values中test.js的值,protocol:///User/xxx/test.js 表示取本地文件 /User/xxx/test.js 的值

from whistle.

a526672351 avatar a526672351 commented on April 30, 2024

刚测试了,不行,报错
Date: 2017-03-10 08:55:55.676
Error: connect ECONNREFUSED 127.0.0.1:80
at Object.exports._errnoException (util.js:873:11)
at exports._exceptionWithHostPort (util.js:896:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1077:14)

我改下测试可以了,就是配置略复杂,如果能和nginx那样简化配置就好了。

from whistle.

a526672351 avatar a526672351 commented on April 30, 2024

直接修改为这样了
url = url.replace(/?.*$/, '');
if (/^https?:/.test(url)) {
var index = url.indexOf('://') + 3;
url = url.substring(index);
index = url.indexOf('/');
if (index === -1) {
host = url;
url = '/';
} else {
host = url.substring(0, index);
url = url.substring(index);
}
var isRoot = url === '/';
if (isRoot || /.(gif|jpg|jpeg|png|bmp|swf|ico|js|css|html)$/i.test(url)) {
// rules.push(host + ' http://' + host);
// rules.push(host + ' ' + (isRoot ? '192.168.30.118:8877' : '127.0.0.1:8080'));
rules.push(host + ' http://' + '127.0.0.1:8080');
}else{
rules.push(host + ' http://' + '192.168.30.118:8877');
}
}

from whistle.

a526672351 avatar a526672351 commented on April 30, 2024

刚测试了,目前配置直接正则,不起效果

from whistle.

avwo avatar avwo commented on April 30, 2024

第三条规则改下,用这个

# 以域名 www.test.com 为例
# 把https请求转成http
https://www.test.com http://www.test.com
# 把根路径的请求转发到指定的ip和端口
/^https?:\/\/www\.test\.com(:\d*)?\/?(\?|$)/ 192.168.30.118:8877
# 处理后缀为gif|jpg|jpeg|png|bmp|swf|ico|js|css|html的请求
/^https?:\/\/www\.test\.com(:\d*)?\/[^?]*\.(gif|jpg|jpeg|png|bmp|swf|ico|js|css|html)(\?|$)/i 127.0.0.1:8080

from whistle.

a526672351 avatar a526672351 commented on April 30, 2024

我是这样配置,还是真实地址IP了,不起作用,我访问的是http,不是https直接配置如下:
#Rules
www.test.com rulesFile://{rulesFile.txt}

#rulesFile.txt:

以域名 www.test.com 为例

把https请求转成http

https://www.test.com http://www.test.com

把根路径的请求转发到指定的ip和端口

/^http?://www.test.com(:\d*)?/?(?|$)/ 192.168.30.118:8877

处理后缀为gif|jpg|jpeg|png|bmp|swf|ico|js|css|html的请求

/^http?://www.test.com(:\d*)?/.(gif|jpg|jpeg|png|bmp|swf|ico|js|css|html)(?|$)/i 127.0.0.1:8080

from whistle.

avwo avatar avwo commented on April 30, 2024

按我最后的这个规则去设置,自己不要乱更改,这个规则同时支持http和https,跟Nginx配置保持一致:

# 以域名 www.test.com 为例
# 把https请求转成http
https://www.test.com http://www.test.com
# 处理后缀为gif|jpg|jpeg|png|bmp|swf|ico|js|css|html的请求
/^https?:\/\/www\.test\.com(:\d*)?\/[^?]*\.(gif|jpg|jpeg|png|bmp|swf|ico|js|css|html)(\?|$)/i 127.0.0.1:8080
# 其它请求
www.test.com 192.168.30.118:8877

from whistle.

a526672351 avatar a526672351 commented on April 30, 2024

测试了,我在w2的网络监控看到了,域名IP无法转发,还是指向源地址,没有效果

from whistle.

avwo avatar avwo commented on April 30, 2024

加QQ群:462558941

from whistle.

yukiyuki1900 avatar yukiyuki1900 commented on April 30, 2024

我现在也同样遇到这个问题,使用配置:

/^http://www.subtest.test.com(:\d*)?/[^?]*.(gif|jpg|jpeg|png|bmp|swf|ico|js|css|html)(?|$)/i 192.170.30.12

无法转发到IP地址,还是指向原地址,请问这个问题是怎么解决的?

from whistle.

avwo avatar avwo commented on April 30, 2024

/^http://www.subtest.test.com(:\d*)?/[^?]*.(gif|jpg|jpeg|png|bmp|swf|ico|js|css|html)(\?|$)/i 192.170.30.12 最后一个问号要转义

from whistle.

yukiyuki1900 avatar yukiyuki1900 commented on April 30, 2024

我的url是http://subtest.test.com/view/page?query=%E5%88%98%E5%BE%B7%E5%8D%8E&refer=abc

然后我配的规则是:

/^http://www. subtest.test.com(:\d*)?/?(?|$)/ 192.170.30.12

还是没法命中,请问这个配置是否正确呢?

from whistle.

avwo avatar avwo commented on April 30, 2024

匹配方式很多种,不一定用正则,尽量用合适的方式:https://avwo.github.io/whistle/pattern.html

正则有点问题:(?|$) -> (\?|$)

下面这种就可以:

192.170.30.12 http://subtest.test.com/view/page 

from whistle.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.