Skip to content

Instantly share code, notes, and snippets.

@lnxph-devops-sareno
Created September 30, 2020 02:31
Show Gist options
  • Save lnxph-devops-sareno/85495a9c35264f4acbdf08a40f630cfa to your computer and use it in GitHub Desktop.
Save lnxph-devops-sareno/85495a9c35264f4acbdf08a40f630cfa to your computer and use it in GitHub Desktop.
HTTP Content Injection with Nginx

Nginx - Content injection

/etc/nginx/sites-enabled/example.com.conf

server {
    listen 80;
    server_name example.com www.example.com;
    
    root /var/www/example.com;
    index index.html;
    
    # Using Nginx's ngx_http_sub_module module to modify the request content
    # Ref. http://nginx.org/en/docs/http/ngx_http_sub_module.html
    
    # Inject an HTML element
    sub_filter '<body>' '<body><br/><h1>Hello! this is an injected content 💉</h1>';
    
    # Inject a script that will show an alert on window click
    sub_filter '</head>' '<script language="javascript">window.onclick=function(){alert("Hello from injected script!")}</script></head>';
    
    sub_filter_once on;
}

Definition

  • sub_filter will replace the text with another text.
  • sub_filter_once on; set to on to only replace the first occurence.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment