Rewrite urls for response with absolute urls


#1

Hi all.

I have Jenkins running on Tomcat with HAProxy as a reverse proxy in front of it. That works kind of OK and my haproxy.conf is listet at the end of my post, but I have a problem with rewriting paths in the response.

when I GET ht.tp://192.168.1.16/jenkins I get the correct HTML returned, but all links are absolute to the host’s root, and thus my webbrowser cannot fetch them as it tries e.g. ht.tp://192.168.1.16/static/style.css instead of ht.tp://192.168.1.16/jenkins/static/style.css.

If only it was a matter of rewriting domains, I would be home safe, but because of that pesky start o the path I am stuck.
I have tried to change the server and/or host to ht.tp://192.168.1.16/jenkins as in the configuration file below.

Can what I want be one?
How? Any pointers and hints are appreciated. (I tried setting the JENKINS_URL in the jenkins control panel, which didn’t work. Also a generic solution would be convenient.)

Thanks,
Morten

I am running HA-Proxy version 1.6.6 2016/06/26

My haproxy.conf:

global
    maxconn 4096

defaults
    log  global
    maxconn  2000
    mode  http
    option  redispatch
    option  forwardfor
    option  http-server-close
    retries  3
    timeout  http-request 10s
    timeout  queue 1m
    timeout  connect 10s
    timeout  client 1m
    timeout  server 1m
    timeout  check 10s

frontend http-in
    bind *:80
    
    reqadd  X-Forwarded-Proto:\ http

    acl is_MyWebApp path_beg /MyWebApp
    use_backend MyWebApp if is_MyWebApp
    
        
        
backend MyWebApp
    option httpclose
    option forwardfor
    
    http-response  set-header Location ht.tp://192.168.1.16/MyWebApp
    http-response  set-header Server ht.tp://192.168.1.16/MyWebApp
    http-response  set-header Host ht.tp://192.168.1.16/MyWebApp    
    
    reqrep ^([^\ ]*\ /)MyWebApp[/]?(.*)     \1\2
    server s2 192.168.1.16:8080  maxconn 32


#2

Hi,

I’m looking for a simalar solution .Have appended /Jenkins and then need to forward to the server .


#3

This should be it (untested):
reqrep ^([^\ :]*)\ /(.*) \1\ /MyWebApp/\2


#4

The URL now goes to login page , but then after login it shows page not found as Jenkins does not resolve the url


#5

I can help you to setup haproxy if you know what has to be done. Can’t help you with your end application though.


#6

I require proxy-ip/jenkins to be the proxy url for Jenkins host ie http://192.168.2.67:8080

I have few other acls configurd in haproxy config.

Jenkins page adds /login (proxy-ip/login) and the frontend rule in haproxy fails to find suitable acl. I get 404.

Current config:-

frontend http-in
bind *:80
option http-server-close
option forwardfor
acl has_jenkins path_beg /jenkins
use_backend jenkins_server if has_jenkins
default_backend jenkins_server

backend jenkins_server
balance roundrobin
reqrep ^([^\ ]\ /)jenkins[/]?(.) \1\2
server ws2 192.168.9.67:8080


#7

The ACL on the frontend matches what the browser sends you, (so before the rewrite). It would not make any sense otherwise.

What you would have to explain, is what you expect from haproxy on a HTTP level exactly.


#8

Ok , Jenkins listens on 192.168.2.67:8080. Once you hit the URL , it shows 192.168.2.67:8080/login in the browser , which then loads the login page . You enter the credentials and after successful authentication Jenkins will take you to 192.168.2.67:8080 , which means you have logged in . All good now

I have HAPRoxy installed on another server , and need to give my users /jenkins , so that it redirects to the above URL .
The first request to the same through HAProxy happens , perfectly , as it finds the prefix and redirects to backend , that loads the login page , with the URL showing /login in the address bar. You see the Jenkins application as prefixed it with /login , with the HAPROXYIP .
Once the login page is shown , and enter the credentials , Jenkins get 401 as it does not find the actual ACL now for /login.