Apache can be used to inject a Request Header in the incoming request that can be either consumed by Apache or forwarded further to another underlying service, in this case Apache works essentially as a reverse proxy.
In a test setup where Apache works as a reverse proxy in front of tomcat, the below Apache configuration is used to implement the reverse proxy functionality and add a Request Header:
<VirtualHost *>
<Location "/sherif">
ProxyPass http://127.0.0.1:8080/sherif
ProxyPassReverse http://127.0.0.1:8080/sherif
RequestHeader set myh "valueofarequestheader"
</Location>
</VirtualHost>
In this setup, tomcat is using default port 8080, it has a defined context path for a dummy application defined in tomcat under its context.xml:
<Context>
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<WatchedResource>WEB-INF/tomcat-web.xml</WatchedResource>
<WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
<Manager pathname="/sherif" />
</Context>
Under tomcat webapps, we have folder created as sherif and has a simple index.html file to support the test:
[root@localhost conf]# ls -lt ../webapps/sherif/
total 4
-rw-r--r-- 1 root root 14 Mar 19 16:27 index.html
[root@localhost conf]#
To verify the header being added, we configure tomcat to log the head myh.
This is done on tomcat server.xml accesslog value as below:
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b %{myh}i" />
Once a request is sent to http://localhost/sherif, tomcat logs the below log showing the request header being added by Apache and reaching tomcat:
127.0.0.1 - - [19/Mar/2019:16:46:47 -0400] "GET /sherif/ HTTP/1.1" 200 17 valueofarequestheader
This configuration is useful in passing headers to backend services in case those are not already sent by the source user agent.
In a test setup where Apache works as a reverse proxy in front of tomcat, the below Apache configuration is used to implement the reverse proxy functionality and add a Request Header:
<VirtualHost *>
<Location "/sherif">
ProxyPass http://127.0.0.1:8080/sherif
ProxyPassReverse http://127.0.0.1:8080/sherif
RequestHeader set myh "valueofarequestheader"
</Location>
</VirtualHost>
In this setup, tomcat is using default port 8080, it has a defined context path for a dummy application defined in tomcat under its context.xml:
<Context>
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<WatchedResource>WEB-INF/tomcat-web.xml</WatchedResource>
<WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
<Manager pathname="/sherif" />
</Context>
Under tomcat webapps, we have folder created as sherif and has a simple index.html file to support the test:
[root@localhost conf]# ls -lt ../webapps/sherif/
total 4
-rw-r--r-- 1 root root 14 Mar 19 16:27 index.html
[root@localhost conf]#
To verify the header being added, we configure tomcat to log the head myh.
This is done on tomcat server.xml accesslog value as below:
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b %{myh}i" />
Once a request is sent to http://localhost/sherif, tomcat logs the below log showing the request header being added by Apache and reaching tomcat:
127.0.0.1 - - [19/Mar/2019:16:46:47 -0400] "GET /sherif/ HTTP/1.1" 200 17 valueofarequestheader
This configuration is useful in passing headers to backend services in case those are not already sent by the source user agent.
Another snippet to set the Authorization request header based on remote host IP address:
ReplyDeleteSetEnvIf Remote_Addr "10.0.0.12" buildtriggerjira
RequestHeader set Authorization "Basic c2hlcmlmOnNoZXJpZg==" env=buildtriggerjira
This helps in case we need to workaround an authenticating service from an application that doesn't support basic authentication.