• Home
  • Cloud VPS
    • Hong Kong VPS
    • US VPS
  • Dedicated Servers
    • Hong Kong Servers
    • US Servers
    • Singapore Servers
    • Japan Servers
  • Company
    • Contact Us
    • Blog
logo logo
  • Home
  • Cloud VPS
    • Hong Kong VPS
    • US VPS
  • Dedicated Servers
    • Hong Kong Servers
    • US Servers
    • Singapore Servers
    • Japan Servers
  • Company
    • Contact Us
    • Blog
ENEN
  • 简体简体
  • 繁體繁體
Client Area

Nginx Tip – Implement the ngx_http_sub_filter_module for content substitution

December 19, 2023

Nginx Tip – Implement the ngx_http_sub_filter_module for content substitution

Nginx is a powerful web server and reverse proxy server that is widely used for hosting websites and applications. It offers various modules that enhance its functionality and allow for advanced customization. One such module is the ngx_http_sub_filter_module, which enables content substitution within HTTP responses. In this article, we will explore how to implement this module and leverage its capabilities.

What is the ngx_http_sub_filter_module?

The ngx_http_sub_filter_module is an Nginx module that provides a way to modify the content of HTTP responses before they are sent to the client. It allows for pattern matching and substitution, making it useful for tasks such as rewriting URLs, modifying HTML content, or replacing specific strings within responses.

Enabling the ngx_http_sub_filter_module

To use the ngx_http_sub_filter_module, you need to ensure that it is enabled in your Nginx configuration. By default, this module is included in most Nginx installations, so you may not need to install any additional packages.

To enable the module, open your Nginx configuration file (usually located at /etc/nginx/nginx.conf) and add the following line within the http block:

http {
    ...
    sub_filter_types *;
    sub_filter_once off;
    ...
}

The sub_filter_types * directive ensures that the module is applied to all response types. The sub_filter_once off directive allows multiple substitutions within a single response.

Implementing content substitution

Once the ngx_http_sub_filter_module is enabled, you can start implementing content substitution in your Nginx configuration. To do this, you need to use the sub_filter directive within a specific location block.

Here’s an example of how to use the sub_filter directive:

location / {
    ...
    sub_filter 'old_string' 'new_string';
    sub_filter_once off;
    ...
}

In the above example, any occurrence of ‘old_string’ within the response will be replaced with ‘new_string’. You can specify multiple sub_filter directives to perform multiple substitutions.

Advanced usage

The ngx_http_sub_filter_module provides additional directives and options for more advanced content substitution. Some of these include:

  • sub_filter_last_modified on;: Replaces the Last-Modified header with the current date and time.
  • sub_filter_once off;: Allows multiple substitutions within a single response.
  • sub_filter_types: Specifies the response types to which the module should be applied.
  • sub_filter_match: Allows for more complex pattern matching using regular expressions.

Refer to the Nginx documentation for more information on these directives and their usage.

Summary

The ngx_http_sub_filter_module is a powerful module that allows for content substitution within HTTP responses. By enabling this module and using the sub_filter directive, you can easily modify the content of your responses to meet your specific requirements. Whether it’s rewriting URLs, modifying HTML content, or replacing specific strings, the ngx_http_sub_filter_module provides a flexible solution.

For more information on VPS hosting solutions, consider Server.HK. They offer top-notch VPS hosting services with reliable performance and excellent customer support.

Recent Posts

  • CentOS Server Performance Tuning: Optimization Techniques for 2026
  • How to Configure SELinux in CentOS Without Breaking Your System (CentOS Stream 9/10 – 2026)
  • Managing Users and Permissions in CentOS Stream: Best Practices (CentOS Stream 9/10 – 2026)
  • How to Set Up Nginx on CentOS Stream for High-Performance Web Hosting
  • CentOS Stream Explained: Key Differences from CentOS Linux

Recent Comments

No comments to show.

Knowledge Base

Access detailed guides, tutorials, and resources.

Live Chat

Get instant help 24/7 from our support team.

Send Ticket

Our team typically responds within 10 minutes.

logo
Alipay Cc-paypal Cc-stripe Cc-visa Cc-mastercard Bitcoin
Cloud VPS
  • Hong Kong VPS
  • US VPS
Dedicated Servers
  • Hong Kong Servers
  • US Servers
  • Singapore Servers
  • Japan Servers
More
  • Contact Us
  • Blog
  • Legal
© 2026 Server.HK | Hosting Limited, Hong Kong | Company Registration No. 77008912
Telegram
Telegram @ServerHKBot