# Strip port in host header

**URL:** <https://discourse.haproxy.org/t/strip-port-in-host-header/4414>\
**Category:** Help!\
**Created:** [October 23, 2019, 11:09am UTC](https://discourse.haproxy.org/t/strip-port-in-host-header/4414 "2019-10-23T11:09:23Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![jazzl0ver](https://avatars.discourse-cdn.com/v4/letter/j/c77e96/32.png) [@jazzl0ver](https://discourse.haproxy.org/u/jazzl0ver)\
**Post date:** [October 23, 2019, 11:09am UTC](https://discourse.haproxy.org/t/strip-port-in-host-header/4414/1 "2019-10-23T11:09:23Z")

</div>

Hi. What is the correct way to strip port number in host header? I have an acl:  
acl srv\_all hdr\_end(host) -i [mydomain.com](http://mydomain.com)

It obviously fails when the host header contains a port number. Thanks in advance!

---

<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:** [October 23, 2019, 1:17pm UTC](https://discourse.haproxy.org/t/strip-port-in-host-header/4414/2 "2019-10-23T13:17:05Z")

</div>

The correct way is to include the port in the match:

`acl srv_all hdr_end(host) -i mydomain.com:8080`

If you have multiple, use multiple matches:

```
acl srv_all hdr_end(host) -i mydomain.com
acl srv_all hdr_end(host) -i mydomain.com:8080
acl srv_all hdr_end(host) -i mydomain.com:1080

```

You probably don’t have a lot of different ports, right?

---

<div class="post-metadata">

**Author:** ![jazzl0ver](https://avatars.discourse-cdn.com/v4/letter/j/c77e96/32.png) [@jazzl0ver](https://discourse.haproxy.org/u/jazzl0ver)\
**Post date:** [October 23, 2019, 1:38pm UTC](https://discourse.haproxy.org/t/strip-port-in-host-header/4414/3 "2019-10-23T13:38:58Z")

</div>

Thanks for the prompt reply!

Well… The ports amount is 2 (80 and 443), but there’re a lot of acls for host name matching:

```auto
# grep " hdr" 02_front.cfg | wc -l
43

```

And adding ports will double the rules number.

Is there another way to go?

---

<div class="post-metadata">

**Author:** ![jazzl0ver](https://avatars.discourse-cdn.com/v4/letter/j/c77e96/32.png) [@jazzl0ver](https://discourse.haproxy.org/u/jazzl0ver)\
**Post date:** [October 23, 2019, 1:56pm UTC](https://discourse.haproxy.org/t/strip-port-in-host-header/4414/4 "2019-10-23T13:56:45Z")

</div>

Looks like I’ve figured it out:

```auto
http-request replace-header Host ([^:]+) \1

```

Any arguments against using this in the frontend section?

---

<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:** [October 23, 2019, 2:19pm UTC](https://discourse.haproxy.org/t/strip-port-in-host-header/4414/5 "2019-10-23T14:19:31Z")

</div>

If your ports are 80 and 443 (for HTTPS) you should not seem them in the Host header at all, because those are standard ports, and browsers will not send them at all.

To match non-standard ports, my proposal is the “correct” way. If instead of the “correct” way you mean howto achieve this in the with the shortest amount of configuration lines, that is a different story of course.

> [@jazzl0ver](#):
>
> http-request replace-header Host ([^:]+) \1

It will certainly rewrite that header, remove the port. However the ACL matching probably comes first, so I’m not sure whether this fixes the issue.

I suggest you clarify why you are seeing this ports in the first place.

---

<div class="post-metadata">

**Author:** ![jazzl0ver](https://avatars.discourse-cdn.com/v4/letter/j/c77e96/32.png) [@jazzl0ver](https://discourse.haproxy.org/u/jazzl0ver)\
**Post date:** [October 23, 2019, 2:57pm UTC](https://discourse.haproxy.org/t/strip-port-in-host-header/4414/6 "2019-10-23T14:57:32Z")

</div>

It appears some services (for example, [https://branch.io/resources/aasa-validator/](https://branch.io/resources/aasa-validator/)) send requests with a port inside.

Thanks for the clarification, I’ll definitely check it.

---

<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:** [October 23, 2019, 3:56pm UTC](https://discourse.haproxy.org/t/strip-port-in-host-header/4414/7 "2019-10-23T15:56:52Z")

</div>

You could use a regex in the ACL instead for every domain:

```
acl srv_all hdr_reg(host) -i mydomain.com[^:]*
```

---

<div class="post-metadata">

**Author:** ![jerome](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.haproxy.org/jerome/32/984_2.png) [@jerome](https://discourse.haproxy.org/u/jerome)\
**Post date:** [October 23, 2019, 4:35pm UTC](https://discourse.haproxy.org/t/strip-port-in-host-header/4414/8 "2019-10-23T16:35:38Z")

</div>

you can use the field converter to match against the name/ip part of the host header.

`acl foo hdr(host),field(1,:) -m end bar`

if you have a lot of domains to match against you can store them in a file, one per line, and use

`acl foo hdr(host),field(1,:) -m end -f /path/to/file`

---

<div class="post-metadata">

**Author:** ![jazzl0ver](https://avatars.discourse-cdn.com/v4/letter/j/c77e96/32.png) [@jazzl0ver](https://discourse.haproxy.org/u/jazzl0ver)\
**Post date:** [October 23, 2019, 4:56pm UTC](https://discourse.haproxy.org/t/strip-port-in-host-header/4414/9 "2019-10-23T16:56:27Z")

</div>

Thanks @jerome, the solution looks very nice! Are you aware which one performs better: a single **http-request replace-header** in the beginning of the frontend section or **hdr(),field()** in each acl?

---

<div class="post-metadata">

**Author:** ![jerome](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.haproxy.org/jerome/32/984_2.png) [@jerome](https://discourse.haproxy.org/u/jerome)\
**Post date:** [October 23, 2019, 5:24pm UTC](https://discourse.haproxy.org/t/strip-port-in-host-header/4414/10 "2019-10-23T17:24:40Z")

</div>

I would prefer not involving regular expressions when not needed.

You can also strip the port with field, ie:  
`http-request set-header host %[hdr(host),field(1,:)]`

which way is best would depend on your config and taste.

---

<div class="post-metadata">

**Author:** ![jazzl0ver](https://avatars.discourse-cdn.com/v4/letter/j/c77e96/32.png) [@jazzl0ver](https://discourse.haproxy.org/u/jazzl0ver)\
**Post date:** [October 23, 2019, 5:44pm UTC](https://discourse.haproxy.org/t/strip-port-in-host-header/4414/11 "2019-10-23T17:44:09Z")

</div>

I completely agree. Thanks a bunch!

---

<div class="post-metadata">

**Author:** ![praveenkumar](https://avatars.discourse-cdn.com/v4/letter/p/ccd318/32.png) [@praveenkumar](https://discourse.haproxy.org/u/praveenkumar)\
**Post date:** [April 24, 2022, 2:25am UTC](https://discourse.haproxy.org/t/strip-port-in-host-header/4414/12 "2022-04-24T02:25:18Z")

</div>

sir can help me how to ignore the hdrhost using field converter

i having the acl like  
acl demo hdr(host) -i [praveen.com](http://praveen.com):www.praveen.com  
from this i need to access [praveen.com](http://praveen.com) only

i tried with acl demo hdr(host),field(1,🙂 -i [praveen.com](http://praveen.com):www.praveen.com  
but this is not working

---

<div class="post-metadata">

**Author:** ![zino](https://avatars.discourse-cdn.com/v4/letter/z/d07c76/32.png) [@zino](https://discourse.haproxy.org/u/zino)\
**Post date:** [June 29, 2022, 3:21am UTC](https://discourse.haproxy.org/t/strip-port-in-host-header/4414/13 "2022-06-29T03:21:08Z")

</div>

replace ‘:’ → ’ ’ between [praveen.com](http://praveen.com):www.praveen.com
