Injection Using Nginx
Related pages:
If you are using Nginx as your web container, or use Nginx as a reverse proxy, you can use a container substitution module to automatically inject the JavaScript Agent into your pages. The module intercepts the response object as it is being returned and makes a string substitution.
Download the Agent
You must first download the JavaScript Agent from the Configuration screen.
- Open the browser application in which you are interested.
- From the left navigation menu, select Configuration.
- Click the Configure and download JavaScript Agent.
- For the JavaScript hosting option, select I will host all the JavaScript agent files.
- Click Download to download the JavaScript Agent.
- Place the file somewhere accessible to the Nginx instance. The name of the saved file should be adrum.js.
Configure Nginx withngx_http_sub_module
The ngx_http_sub_module module is a filter that modifies a response by replacing one specified string by another. You can use this feature to have the server automatically inject the header portion of the JavaScript Agent into a served page. For more information on the process, see the Nginx documentation, Module ngx_http_sub_module.
For example, modify the location context to replace the <head> tag with the <head> tag and the JavaScript Agent scripts.
Sample Nginx Configuration
location / {
sub_filter <head>
'<head><script>window["adrum-app-key"]="<EUM_APP_KEY>";window["adrum-start-time"]=new Date().getTime();</script><script type="text/javascript" src="//cdn.appdynamics.com/adrum/adrum-latest.js"></script>';
sub_filter_once on;
}
In the sample above, note that /adrum-latest.js is the path to a copy of the adrum file that is accessible to the server. Also, note the timer initialization <script>window['adrum-start-time'] = new Date().getTime();</script>
.Keep these as close as possible to the top of the document, preferably right after the <head> tag, ensures the best possible timings.
You may need to escape some characters, depending on your platform. For example, on Mac OS:
location / {
sub_filter <head>
'<head><script>window[\'adrum-start-time\'] = new Date().getTime();</script><script src="/adrum.js"></script>';
sub_filter_once on;
}
Possible variations on the script string can be found in Configure the JavaScript Agent.
Configure Ngnix withoutngx_http_sub_module
You can configure Ngnix without the ngx_http_sub_module module if you prefer. Insert the required script into the conf.d/default.conf location.
Sample Nginx Configuration
location / {
sub_filter <head>
'<head><script>window["adrum-app-key"]="<EUM_APP_KEY>";window["adrum-start-time"]=new Date().getTime();</script><script type="text/javascript" src="//cdn.appdynamics.com/adrum/adrum-latest.js"></script>';
sub_filter_once on;
}