• 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 for Newbie: setting up proxy_redirect directive

December 18, 2023

Nginx for Newbie: Setting up proxy_redirect directive

Nginx is a powerful web server and reverse proxy server that is widely used for hosting websites and applications. It offers various features and functionalities that make it a popular choice among developers and system administrators. One of the essential directives in Nginx is the proxy_redirect directive, which allows you to modify the response headers and redirect URLs. In this article, we will explore how to set up the proxy_redirect directive for beginners.

Understanding the proxy_redirect directive

The proxy_redirect directive is used to modify the response headers and redirect URLs in Nginx. It is commonly used when Nginx is acting as a reverse proxy server, forwarding requests to backend servers. The directive allows you to rewrite the location header and modify the URLs in the response body.

The basic syntax of the proxy_redirect directive is as follows:

proxy_redirect default replacement;

Here, default is the original string that needs to be replaced, and replacement is the new string that will replace the original string. The default string can be a regular expression or a plain string.

Setting up the proxy_redirect directive

To set up the proxy_redirect directive, you need to modify the Nginx configuration file. The configuration file is usually located at /etc/nginx/nginx.conf or /etc/nginx/conf.d/default.conf. Open the file using a text editor and locate the location block where you want to apply the proxy_redirect directive.

Within the location block, add the following line:

proxy_redirect default replacement;

Replace default with the original string you want to replace and replacement with the new string. For example, if you want to replace all occurrences of http://backend-server with https://new-backend-server, the directive would look like this:

proxy_redirect http://backend-server https://new-backend-server;

Save the configuration file and restart Nginx for the changes to take effect. You can use the following command to restart Nginx:

sudo service nginx restart

Example usage of proxy_redirect directive

Let’s consider an example where you have an Nginx server acting as a reverse proxy for a backend server. The backend server returns a response with the following headers:

Location: http://backend-server/page

And the response body contains the following URLs:

<a href="http://backend-server/page">Link</a>
<img src="http://backend-server/image.jpg">

If you want to replace all occurrences of http://backend-server with https://new-backend-server, you can use the following proxy_redirect directive:

proxy_redirect http://backend-server https://new-backend-server;

After applying the directive, the modified response headers will be:

Location: https://new-backend-server/page

And the modified response body will be:

<a href="https://new-backend-server/page">Link</a>
<img src="https://new-backend-server/image.jpg">

Summary

The proxy_redirect directive in Nginx is a powerful tool for modifying response headers and redirecting URLs. It allows you to replace specific strings in the response headers and body, making it useful when Nginx is acting as a reverse proxy server. By understanding and utilizing the proxy_redirect directive, you can customize the behavior of your Nginx server to meet your specific requirements.

For more information about VPS hosting and to explore our high-performance VPS solutions, visit Server.HK.

Recent Posts

  • How to Host a Python Flask or Django Application on Hong Kong VPS (2026)
  • How to Set Up WireGuard VPN on a Hong Kong VPS: Step-by-Step Guide 2026
  • Hong Kong VPS vs DigitalOcean: Cost, Performance, and China Routing Compared (2026)
  • VPS Hosting vs Shared Hosting: Why the Upgrade Is Worth It for Asia-Facing Websites
  • Hong Kong VPS vs Google Cloud Asia: Which Delivers Better China Performance in 2026?

Recent Comments

  1. vibramycin injection on How to Choose the Right Hong Kong VPS Plan: A Buyer’s Guide for 2026
  2. allopurinol for gout on CN2 GIA vs BGP vs CN2 GT: What’s the Real Difference for China Connectivity?
  3. antibiotics online purchase on How to Set Up a WordPress Site on a Hong Kong VPS with aaPanel (Step-by-Step 2026)
  4. linezolid cost oral on Top 5 Use Cases for a Hong Kong Dedicated Server in 2026
  5. metoprolol generic on Hong Kong VPS vs Japan VPS: Head-to-Head for Asia-Pacific Deployments in 2026

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