📖 Scenario: You are setting up a simple web server using Nginx. You want to transform incoming requests and outgoing responses to meet specific needs.For example, you want to add a custom header to all responses and rewrite certain request URLs.
🎯 Goal: Build an Nginx configuration that rewrites request URLs starting with /oldpath to /newpath, and adds a custom response header X-Custom-Header: DevOpsProject to all responses.
📋 What You'll Learn
Create a basic Nginx server block listening on port 8080
Rewrite requests from /oldpath to /newpath
Add a custom response header X-Custom-Header: DevOpsProject
Serve static files from /usr/share/nginx/html
💡 Why This Matters
🌍 Real World
Web servers often need to modify requests and responses to support legacy URLs, add security headers, or customize content delivery.
💼 Career
Understanding Nginx request and response transformations is essential for DevOps roles managing web infrastructure and improving application delivery.
Progress0 / 4 steps
1
Create a basic Nginx server block
Write an Nginx server block that listens on port 8080 and serves files from /usr/share/nginx/html. Name the server block server and include the listen and root directives.
Nginx
Hint
Use server { ... } block with listen 8080; and root /usr/share/nginx/html;.
2
Add URL rewrite rule
Inside the existing server block, add a location /oldpath block that rewrites requests to /newpath using the rewrite directive with a permanent redirect.
Nginx
Hint
Use location /oldpath { rewrite ^/oldpath(.*)$ /newpath$1 permanent; } inside the server block.
3
Add custom response header
Inside the server block, add the directive add_header X-Custom-Header "DevOpsProject"; to add a custom header to all responses.
Nginx
Hint
Add add_header X-Custom-Header "DevOpsProject"; inside the server block but outside location blocks.
4
Test and display the final configuration
Print the entire Nginx configuration you wrote so far to verify it includes the server block with listen 8080;, root /usr/share/nginx/html;, the location /oldpath rewrite, and the add_header directive.
Nginx
Hint
Print the full configuration text exactly as written.
Practice
(1/5)
1. What is the main purpose of request/response transformation in nginx?
easy
A. To block all incoming requests
B. To increase the server's hardware capacity
C. To change the server's IP address automatically
D. To modify data passing through nginx for better communication
4. You want to modify the response body in nginx but changes don't appear. What is the likely cause?
medium
A. gzip compression is enabled, preventing body modification
B. proxy_pass directive is missing
C. add_header directive is used incorrectly
D. server block is missing listen directive
Solution
Step 1: Understand response body modification requirements
Modifying response body requires disabling gzip compression because compressed data can't be changed easily.
Step 2: Identify common misconfigurations
If gzip is enabled, changes won't show; proxy_pass or listen missing causes other errors, add_header affects headers only.
Final Answer:
gzip compression is enabled, preventing body modification -> Option A
Quick Check:
Disable gzip to modify response body [OK]
Hint: Disable gzip to allow response body changes [OK]
Common Mistakes:
Ignoring gzip compression effect
Confusing header directives with body changes
Assuming missing proxy_pass causes this issue
5. You want to rewrite incoming URLs from /old-path/ to /new-path/ and add a response header X-Updated: true. Which nginx config snippet correctly does both?
places rewrite after proxy_pass (ignored). Configs using location /new-path/ have the wrong location. Configs with permanent cause redirects, not internal rewrites.