Introduction

Sometimes it is required to rewrite the content returned by the application which is not possible or desired by using a new location redirects HTTP:301/302. NGINX allows to change the return traffic’s content dynamically.

Code

server {

  listen 802;

  location / {

    proxy_pass http://127.0.0.1:81/;

    sub_filter '<a href="/wps-portal/news'  '<a href="/news';

    rewrite ^/news$ /wps-portal/news;

  }

}

Explanation

server {

  listen 802; <- Listen on incoming traffic on TCP port 802

  location / {

    proxy_pass http://127.0.0.1:81/; <- Forward the traffic to the localhost on TCP port 81

    sub_filter '<a href="/wps-portal/news'  '<a href="/news'; <- Change the  '<a href="/wps-portal/news' to  '<a href="/news'

    rewrite ^/news$ /wps-portal/news; <- rewrite it back to the correct path so the application knows where to route the traffic.

  }

}