0
0
Nginxdevops~5 mins

Request/response transformation in Nginx - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you need to change the data coming into your server or the data going out before it reaches the user. Request/response transformation lets you modify these messages easily using Nginx.
When you want to add or change headers in incoming requests to control how your backend handles them
When you need to rewrite parts of the URL before the request reaches your application
When you want to modify the response headers to add security or caching rules
When you want to change the response body, like replacing text or injecting content before sending it to the user
When you want to block or redirect certain requests based on their content
Config File - nginx.conf
nginx.conf
events {}

http {
    server {
        listen 8080;

        location / {
            # Change the Host header in the request
            proxy_set_header Host example.com;

            # Add a custom header to the response
            add_header X-Custom-Response "Transformed by Nginx" always;

            # Rewrite URL path from /oldpath to /newpath
            rewrite ^/oldpath/(.*)$ /newpath/$1 break;

            # Proxy pass to backend server
            proxy_pass http://localhost:9000;

            # Modify response body: replace 'foo' with 'bar'
            sub_filter 'foo' 'bar';
            sub_filter_once off;
        }
    }
}

events {}: Required block for Nginx event handling.

http {}: Main block for HTTP server settings.

server {}: Defines a server listening on port 8080.

location / {}: Handles all requests to the root path.

proxy_set_header Host example.com; changes the Host header sent to the backend.

add_header X-Custom-Response "Transformed by Nginx" always; adds a custom header to the response, including for error responses.

rewrite ^/oldpath/(.*)$ /newpath/$1 break; rewrites URLs starting with /oldpath to /newpath.

proxy_pass http://localhost:9000; forwards requests to a backend server running on port 9000.

sub_filter directives replace all occurrences of 'foo' with 'bar' in the response body.

Commands
Check the Nginx configuration file for syntax errors before applying changes.
Terminal
nginx -t
Expected OutputExpected
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
Reload Nginx to apply the new configuration without stopping the server.
Terminal
nginx -s reload
Expected OutputExpected
No output (command runs silently)
Send a request to test the URL rewrite, header changes, and response body transformation.
Terminal
curl -i http://localhost:8080/oldpath/test
Expected OutputExpected
HTTP/1.1 200 OK Server: nginx X-Custom-Response: Transformed by Nginx Content-Type: text/html <html>...bar...</html>
Key Concept

If you remember nothing else from this pattern, remember: Nginx can change both incoming requests and outgoing responses using simple configuration directives.

Common Mistakes
Forgetting to test the configuration with 'nginx -t' before reloading.
This can cause Nginx to fail to reload and stop serving requests properly.
Always run 'nginx -t' to verify syntax before 'nginx -s reload'.
Not enabling 'sub_filter' module or missing 'sub_filter_once off;' when replacing multiple occurrences.
Only the first occurrence of the text will be replaced, leaving others unchanged.
Use 'sub_filter_once off;' to replace all occurrences in the response body.
Using 'rewrite' without 'break' or 'last' flag causing unexpected redirects.
The rewrite may continue processing and cause wrong URL handling.
Add 'break' flag to stop further rewrite processing after the rule.
Summary
Use 'proxy_set_header' to modify request headers before sending to backend.
Use 'add_header' to add or change response headers sent to clients.
Use 'rewrite' with proper flags to change request URLs before processing.
Use 'sub_filter' to modify response body content on the fly.