Thursday, March 18, 2010

F5 BigIP LTM iRules

1) To redirect http requests to https
-------------------------------------------------------------------------
when HTTP_REQUEST {
HTTP::redirect https://[HTTP::host][HTTP::uri]
}
-------------------------------------------------------------------------
Please check Redirect Rewrite to Matching in HTTP Profile used
Associate the iRule with Virtual Server Profile

2) To modify cookie domain for JSESSIONID.
-------------------------------------------------------------------------
when HTTP_RESPONSE {

   if {[HTTP::cookie count] > 0 } {
      foreach aCookie [HTTP::cookie names] {
         log local0. "Cookie name: $aCookie"
         if { $aCookie == "JSESSIONID" } {
            HTTP::cookie domain $aCookie yourwiderdomain.com
         }
      }
   }
}
-------------------------------------------------------------------------

This iRule will help to issue the same JSESSIONID when switching between different subdomains.
In JBOSS by default, you will get two different JSession IDs when you make requests to www.domain.com and secure.domain.com. If you examine the JSESSIONID cookie, you will see the domain name is set to complete www.domain.com and secure.domain.com respectively.

To avoid this issue and get a single JSESSIONID , create an iRule as above and set the cookie domain at wider level, ex: domain.com. Associate the iRule to virtual server in the resources section. Hit the URLs again, the domain name in JSESSIONID will be set to domain.com. 

3) Issue 301 redirect and resolve to desired URI
-------------------------------------------------------------------------
when HTTP_REQUEST {
if { not ([HTTP::host] starts_with "www") }
{
   HTTP::respond 301 Location "http://www.[HTTP::host]/urigoeshere"
}
elseif { [HTTP::uri] equals "/" }{
   HTTP::respond 301 Location "http://[HTTP::host]/urigoeshere"
}
}

Another flavor with or condition

when HTTP_REQUEST {
   if { not ([HTTP::host] starts_with "www"
         or [HTTP::host] starts_with "somename"
       )
     }{
           HTTP::respond 301 Location "http://www.[HTTP::host]/web/cs?a=5"
   }
   elseif { [HTTP::uri] equals "/" }{
     HTTP::respond 301 Location "http://[HTTP::host]/web/cs?a=5"
   }
 }

-------------------------------------------------------------------------
This iRule will avoid multiple redirects.

4) Insert X-Forwarded-Proto
-------------------------------------------------------------------------
iRule for port 80
when HTTP_REQUEST {
   HTTP::header remove X-Forwarded-Proto
   HTTP::header insert X-Forwarded-Proto http
}

iRule for port 443
when HTTP_REQUEST {
   HTTP::header remove X-Forwarded-Proto
   HTTP::header insert X-Forwarded-Proto https
}
-------------------------------------------------------------------------
This iRule is useful to identify the client protocol is either http or https. This iRule helps the when the SSL gets decrypted in load balancer or web server and backed requests are sent to application server as http. If the application uses http servlet isSecure method, setting the iRule on port 443 will return isSecure as true. Please note the XForwardProto filter will also have to be applied on application server to get the correct value for isSecure or getScheme.


4) Logging 
-------------------------------------------------------------------------
log local0. "value1= $somevariable and value2= $somevariableothervariable "
log local0. " IP: [IP::client_addr] "
log local0. " uri= [HTTP::uri] path= [HTTP::path] query= [HTTP::query] 
-------------------------------------------------------------------------
The log command can be inserted to iRule to provide useful debug information.

Wednesday, March 17, 2010

Recommended LTM Configuration for Apache

1) Login into LTM
2) Create monitor: Local Traffic -> Monitors -> create apache-http-monitor
Type: http
Import Settings: http
Interval: 30 seconds
Timeout: 91 seconds
Send String: GET /ltmreply.html 
Finish


Note:
a) Recommended calculation for Timeout based on Interval value => (1:3)+1 => (30 * 3 + 1 = 90 secs
b) Send String is optional. The default is GET /. If you want to modify as above, create a page called ltmreply.html with simple content like 'web server alive' and place it in the document root folder in web server.

3) Create Pool
Local Traffic - Virtual Servers -> Pools -> Create
Name: your choice
Health Monitor: apache-http-monitor (created as above)
Slow Ramp Time: 30 secs
Finish

4) Create Pool Members
Local Traffic - Virtual Servers -> Pools -> Members
Specify web server IP address and port as 9080.
(Assume that web server listens on port 9080)

5) Create HTTP Profile
Local Traffic -> Virtual Servers -> Profiles -> Services -> HTTP -> Create New
Name: apache-http-opt
Parent Profile: http-wan-optimized-compression-caching (If not using WebAccelerator)
Redirect Rewrite: Matching (to support http to https switching)
Leave the rest to default values
Finish

6) Create TCP Profiles
a) Local Traffic -> Virtual Servers -> Profiles -> Protocol -> TCP -> Create New
Name: apache-tcp-lan
Parent Profile: tcp-lan-optimized (if Apache connect to devices on local network - server side TCP connections)

b) Local Traffic -> Virtual Servers -> Profiles -> Protocol -> TCP -> Create New
Name: apache-tcp-wan
Parent Profile: tcp-wan-optimized (if users connect to Apache from remote places - client side TCP connections)

7) Create Persistence Profiles
a) Local Traffic -> Virtual Servers -> Profiles -> Persistence -> Create New
Name: apache-cookie
Type: Cookie

b) Local Traffic -> Virtual Servers -> Profiles -> Persistence -> Create New
Name: apache-source
Type: Source Addresses Affinity
Finish

8) Create OneConnect profile (client requests can utilize existing, server-side connections - recommended to provide significant performance improvements)
Local Traffic -> Virtual Servers -> Profiles -> Other Profiles -> Create New
Name: apache-oneconnect
Type: oneconnect

9) Create Virtual Servers for HTTP traffic
Local Traffic -> Virtual Servers -> Create New
Name: your choice
Destination: Host - Provide, IP address
Service Port: 80 HTTP
Configuration: Advanced:
Type: Standard
Protocol: TCP
Protocol Profile (Client): apache-tcp-wan
Protocol Profile (Server): apache-tcp-lan
OneConnect Profile: apache-oneconnect
HTTP Profile: apache-http-opt
Finish

Navigate to created virtual server -> Resources
Default Pool: Select the pool created above
Default Persistence Profile: apache-cookie
Fallback Persistence Profile: apache-source

10) Import CA Root Certs if different than recognized CA (Your organization can even act as CA for self signed certs)
Local Traffic -> SSL Certificates -> Import ->
Import Type: Certificate
Certificate Name: Create New -> Your choice
Certificate Source : Upload File -> Upload root ca cert for your organization

Local Traffic -> SSL Certificates -> Import ->
Import Type: Certificate
Certificate Name: Create New -> Your choice
Certificate Source : Upload File -> Upload Intermediary ca cert for your organization

10) Create CSR
Local Traffic -> SSL Certificates -> Create
Provide Name, Issuer: Certificate Authority and other details
Download the CSR
Finish
You will see a key record created under SSL certificates

11) Certificate Authorization

Login to the Microsoft Certificate Server
Request a certificate -> Advanced Certificate Request -> Submit a certificate request by using a base-64-encoded CMC or PKCS #10 file, or submit a renewal request by using a base-64-encoded PKCS #7 file.

Certificate Request: Browse & insert the CSR file created above
Certificate Template: Web Server
Attributes: san:dns=www.mydomain.com&dns=secure.mydomain.com
(Attributes parameter is like an alias so one cert can be used for multiple domains)
Submit

Download and save the certificate.
Open the certificate -> Go to Details tab -> Subject Alternative Name
Check for Alias

12) Import SSL certificate into LTM
Local Traffic -> SSL Certificates -> Navigate to the CSR/Key created in the above.
Upload the Cert and Import

13) Create an Client SSL Profile
Local Traffic -> Virtual Servers -> Profiles -> SSL -> Create New
Name: your choice
Certificate: As created or imported above
Key: As created or imported above
Chain: Your company's Chain cert imported above
Trusted CA: Your company's CA cert imported above
Finish

14) Create Virtual Servers for HTTPS traffic
Local Traffic -> Virtual Servers -> Create New
Name: your choice
Destination: Host - Provide, IP address
Service Port: 443 HTTPS
Configuration: Advanced:
Type: Standard
Protocol: TCP
Protocol Profile (Client): apache-tcp-wan
Protocol Profile (Server): apache-tcp-lan
OneConnect Profile: apache-oneconnect
HTTP Profile: apache-http-opt
SSL Profile (Client): Select the clienssl profile created above
Finish

Navigate to created virtual server -> Resources
Default Pool: Select the http pool created above
Default Persistence Profile: apache-cookie
Fallback Persistence Profile: apache-source



Reference: F5 deployment guide: Deploying F5 with Apache Web Servers

Newer›