DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Build a Java Backend That Connects With Salesforce
  • Build Even Faster Quarkus Applications With fast-jar
  • Retrieval Augmented Generation With Spring AI 2.0, Claude, and PGvector
  • How To Build a Simple GitHub Action To Deploy a Django Application to the Cloud

Trending

  • How to Migrate From C/C++ to Rust: A Practical Guide for Embedded IoT Systems
  • Using Middleware in LangChain: Recovering From Tool Failures
  • Part 4: Multi-Agent Orchestration — Pairing Goose With Quarkus Flow State Machines
  • How to Hide Sensitive Information Before You Share a File

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Data Engineering
  3. Data
  4. Replacing Text in NGINX with sub_filter

Replacing Text in NGINX with sub_filter

Weird situations arise sometimes with web applications. Come have a look at how you might overcome these situations when you need to replace text like IP addresses or files with NGINX!

By 
James Carr
·
Apr. 28, 16 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
26.9K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Sometimes you find yourself in a weird predicament. A third party application that you’ve slapped NGINX in front of insists on using internal IP addresses or ports despite your reverse proxy passing all the correct headers and other pieces required. Or maybe you’ve found yourself in the situation I found myself in last week where you have a third party internal application wanting to reference a file on a CDN. As luck would have it, that CDN has been deprecated and changing it requires rebuilding a jar where the script path is hardcoded and then repackaging the internal application that uses it. Long story short, we’ll soon be doing some yak shaving that could possibly take all day so how about we just use sub_filter instead and take a nap?

Use It

Thankfully most stock builds of NGINX include ngx_http_sub_module by default. Using it is actually simple enough, just slap the following directive under your location or server directive in nginx.conf.

And that’s it! You can also use regular expression here as well. sub_filter_once indicates whether NGINX should apply the replacement once or repeatedly. One gotcha you might have is that by default this only works with mime types of text/html. If you want to modify the mime-types that subfilter executes on set `sub_filter_types` to the desired type.

Give It a Try

I’ve put together a very simple demonstration of using subfilter for text replacement on Github.

application Content delivery network Directive (programming) YAK (cryptography) Build (game engine) GitHub JAR (file format) Data Types

Published at DZone with permission of James Carr. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Build a Java Backend That Connects With Salesforce
  • Build Even Faster Quarkus Applications With fast-jar
  • Retrieval Augmented Generation With Spring AI 2.0, Claude, and PGvector
  • How To Build a Simple GitHub Action To Deploy a Django Application to the Cloud

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook