Learn F5 Technologies, Get Answers & Share Community Solutions Join DevCentral

Filter by:
  • Solution
  • Technology
code share

Serverside SNI injection iRule

Problem this snippet solves:

Hi Folks,

the iRule below can be used to inject a TLS SNI extension to the server side based on e.g. HOST-Header values. The iRule is usefull if your pool servers depending on valid SNI records and you don't want to configure dedicated Server SSL Profiles for each single web application.

Cheers, Kai

How to use this snippet:
  1. Attach the iRule to the Virtual Server where you need to insert a TLS SNI expension
  2. Tweak the $sni_value variable within the HTTP_REQUEST to meet your requirements or move it to a different event as needed.
  3. Make sure you've cleared the "Server Name" option in your Server_SSL_Profile.
Tested on Version:
Comments on this Snippet
Comment made 15-Mar-2017 by Stanislas Piron 10640

Hi Kai,

Nice irule... I used this irule for APM portal access resource by replacing the HTTP_REQUEST event by ACCESS_ACL_ALLOWED.

and it worked.

Comment made 06-Apr-2017 by Kai Wilke 7294

Hi Stanislas,

I've slightly optimized the iRule. Make sure to update your toolbox... ;-)

Cheers, Kai

Comment made 07-Apr-2017 by Stanislas Piron 10640

4 lines for such need.... with encoding explanations!


Comment made 07-Apr-2017 by Kai Wilke 7294

And its now ~4-times faster than before :-)

Cheers, Kai

Comment made 07-Apr-2017 by Patrik Jonsson 3524

Impressive, five stars! :)

Comment made 27-Apr-2017 by Stanislas Piron 10640

Hi Kai,

with this optimized code, do you still use the "Client side to server side SNI relay iRule" or do you always use this one?

Comment made 26-Jun-2017 by JoeTheFifth 301

I was redirected from this thread (https://devcentral.f5.com/questions/help-with-sni-not-being-passed-to-pool-servers#comment50962) to this one by Stanislas. I was struggling with two certs/two profiles with SNI support. couldn't make it work. I just tested this irule and it works fine. I wanted to use the default config instead of an irule as I already have a complex irule/apm setup but this solves my issue for the moment. I had a question in mind about how to make it work with two certs but I answered my question with a quick test. I created a client profile (no sni setting defined) and added the two certs ( with only one key otherwise you get the error 'Client SSL profile cannot contain more than one set of same certificate/key type'). I created a server profile (no sni no cert just inherited from th default ssl server). Pluged the iRule to the VS and voilà ! :-) Cheers ! But I'd like to know if configuring multiple profiles with sni entries (and a default sni profile) on a single VS work !

Comment made 26-Jun-2017 by Stanislas Piron 10640

you can configure on the virtual server multiple clientssl profile (to manage client servername certificate selection) and with only one serverssl profile with this irule.

Comment made 10-Jul-2017 by angorya 0

I still can't make it work. For some reason I don't know, the F5 is still sending its own hostname in the Client Hello.

Comment made 10-Jul-2017 by Stanislas Piron 10640

@angorya f5 can't know its own host name in irule. I guess you mean the host name of the virtual server!

If you use APM With rewrite mode, read my first comment!

Comment made 11-Jul-2017 by Patrik Jonsson 3524

This might be a bit out of context, but you can actually extract the hostname of the F5 from an iRule:


Check out:



Comment made 11-Jul-2017 by angorya 0

Stanislas, you're correct, I expressed myself incorrectly. It's sending the VS hostname.

I'm using APM with rewrite mode, so I followed your suggestion when I read this thread (I did test Kai's iRule too). I had the iRule log what it does, so I can see that the hostname that it's going to inject is the correct one, but in the packet capture I still see the VS hostname.

Edit: Just in case, this is in 13.0.0.

Comment made 23-Aug-2017 by Dane 1

@angorya, I am seeing the same behavior you are. I have a portal access profile setup to route traffic to an internal app. I used the rule above and the tcpdumps still show the original server name instead of the injected server name.

Comment made 24-Aug-2017 by Kai Wilke 7294

Patrik mentioned: tcl_platform(machine)

Hi Patrik, instead of using the slightly bugged tcl_platform(machine) global variable, you may try the build-in TCL command [info hostname] to easily access the HOST-Name of your F5.

Cheers, Kai

Comment made 11-Sep-2017 by alex.talmage 1

I am struggling the same as Angorya and Dane to get this working with Portal-Access. Replaced HTTP_Request with ACCESS_ACL_ALLOWED and its still sending the original SNI

Comment made 11-Sep-2017 by angorya 0

F5 has it as a Known Issue for 13.0:

653495-1 : Incorrect SNI hostname attached to serverside connections

Component: Local Traffic Manager

Symptoms: SNI hostname submitted to a virtual server on the client side is sent to server side, even if there is a different hostname specified in the server SSL profile.

Conditions: -- Client side ClientHello contains SNI.

Impact: SNI is sent from client to server without stripping or rewriting the SNI.

Workaround: None.

It seems we have to wait for them to patch it...

Comment made 11-Sep-2017 by Kai Wilke 7294

Hi Angorya,

Workaround: None.

Seems to be an dead end then, since you can not remove an existing SNI extension from the Handshake for now (see my prequisites Step 3.).

The last chance I see for you is by utilizing a VIP-targeting-VIP setup, so that you can still use SNI to flip SSL Profiles for the client side connection (aka. external VS) while making sure to not forward a SNI value to APM (aka. internal VS). At least this approach will allow you to mitigate the listed condition "Client side ClientHello contains SNI." and would then allow you to insert a SNI value of your choice on the connection between the internal VS and backend servers.

Cheers, Kai

Comment made 03-Nov-2017 by Enrique Estudillo 0

Great iRule! Tested on 11.5.4 and works!

Comment made 25-Jan-2018 by Christoph Langer 74

I have two virtual servers with multiple server ssl profiles applied. One is working, the second only work with one domain. Replacing the multiple server ssl profiles with this irule solved the problem. Thanks!

Comment made 25-Jan-2018 by Kai Wilke 7294

You're both very welcome!

Cheers, Kai

Comment made 15-Aug-2018 by youssef 4046

Good JOB!!!

Comment made 4 months ago by kgaigl 110

Hello Kai,

could you please explain, how to tweak the sni_value? I'm to stupid to get this...

we've 2 servernames behind a Portal-Access (different from the FQDN of Client-Side), now only the one works, I insert the servername in the sni-field of the SSL-Profile.

But what would there be the $sni_value ? At the moment I don't get it.



Comment made 4 months ago by Stanislas Piron 10640

Hello kgaigl,

look at the first comment above...

Comment made 4 months ago by Kai Wilke 7294

Hi kgaigl,

the iRule uses the HTTP_REQUEST event to extract the HOST-header value of the client requested ressource. This value is then used as Server_SSL SNI-Value.

If the client requests https://www.domain.com/ the Server_SSL SNI-Value will become www.domain.com If the client requests https://ww2.domain.com/ the Server_SSL SNI-Value will become ww2.domain.com

Note: You may use whatever logic during the HTTP_REQUEST event you like to decide which SNI-Value should be used to negotiate the Server Side SSL connection.

Important: To make this iRule work you have to make sure that you dont specify a SNI-Value in your Server_SSL_Profile. This will allow the iRule to simply inject the SNI-Value without clearing existing values (aka. clearing existing values is not supported). If you set the "Server Name" option in your Server_SSL_Profile, the SSL negotiation will most likely fail...

Cheers, Kai

Comment made 4 months ago by kgaigl 110

Hi Stanislas,

that did it, thanks a lot.

Hi Kai, thanks too for explaining and for the Rule


Comment made 1 month ago by Piotr Lewandowski 1162

Hi Kay,

Actually Server Name can be enabled, it just has to be empty. Wonder why not use something like that in SERVERSSL_CLIENTHELLO_SEND:

clientside {
   set sni_value [getfield [HTTP::host] ":" 1]
   log local0. "SNI should be: \"$sni_value\""

I know that TCL error ("{badEventContext {command is not valid in current event context (SERVER_CONNECTED)} is generated when saving iRule but according to support as long as you are actually processing HTTP at clientside this will work without issue - and indeed it works for me.

Any comments why not to use?