# Rewrite urls for response with absolute urls

**URL:** <https://discourse.haproxy.org/t/rewrite-urls-for-response-with-absolute-urls/513>\
**Category:** Help!\
**Created:** [July 20, 2016, 1:14pm UTC](https://discourse.haproxy.org/t/rewrite-urls-for-response-with-absolute-urls/513 "2016-07-20T13:14:56Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mowijo](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.haproxy.org/mowijo/32/101_2.png) [@Mowijo](https://discourse.haproxy.org/u/Mowijo)\
**Post date:** [July 20, 2016, 1:14pm UTC](https://discourse.haproxy.org/t/rewrite-urls-for-response-with-absolute-urls/513/1 "2016-07-20T13:14:56Z")

</div>

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`:

```auto
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

```

---

<div class="post-metadata">

**Author:** ![ppraj](https://avatars.discourse-cdn.com/v4/letter/p/85e7bf/32.png) [@ppraj](https://discourse.haproxy.org/u/ppraj)\
**Post date:** [January 3, 2017, 1:54pm UTC](https://discourse.haproxy.org/t/rewrite-urls-for-response-with-absolute-urls/513/2 "2017-01-03T13:54:35Z")

</div>

Hi,

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

---

<div class="post-metadata">

**Author:** ![lukastribus](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@lukastribus](https://discourse.haproxy.org/u/lukastribus)\
**Post date:** [January 3, 2017, 2:13pm UTC](https://discourse.haproxy.org/t/rewrite-urls-for-response-with-absolute-urls/513/3 "2017-01-03T14:13:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ppraj](https://avatars.discourse-cdn.com/v4/letter/p/85e7bf/32.png) [@ppraj](https://discourse.haproxy.org/u/ppraj)\
**Post date:** [January 3, 2017, 2:26pm UTC](https://discourse.haproxy.org/t/rewrite-urls-for-response-with-absolute-urls/513/4 "2017-01-03T14:26:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lukastribus](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@lukastribus](https://discourse.haproxy.org/u/lukastribus)\
**Post date:** [January 3, 2017, 3:06pm UTC](https://discourse.haproxy.org/t/rewrite-urls-for-response-with-absolute-urls/513/5 "2017-01-03T15:06:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ppraj](https://avatars.discourse-cdn.com/v4/letter/p/85e7bf/32.png) [@ppraj](https://discourse.haproxy.org/u/ppraj)\
**Post date:** [January 3, 2017, 3:26pm UTC](https://discourse.haproxy.org/t/rewrite-urls-for-response-with-absolute-urls/513/6 "2017-01-03T15:26:36Z")

</div>

I require proxy-ip/jenkins to be the proxy url for Jenkins host ie [http://192.168.2.67:8080](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

---

<div class="post-metadata">

**Author:** ![lukastribus](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@lukastribus](https://discourse.haproxy.org/u/lukastribus)\
**Post date:** [January 3, 2017, 3:30pm UTC](https://discourse.haproxy.org/t/rewrite-urls-for-response-with-absolute-urls/513/7 "2017-01-03T15:30:44Z")

</div>

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_.

---

<div class="post-metadata">

**Author:** ![ppraj](https://avatars.discourse-cdn.com/v4/letter/p/85e7bf/32.png) [@ppraj](https://discourse.haproxy.org/u/ppraj)\
**Post date:** [January 3, 2017, 4:09pm UTC](https://discourse.haproxy.org/t/rewrite-urls-for-response-with-absolute-urls/513/8 "2017-01-03T16:09:02Z")

</div>

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.
