• 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 port_in_redirect directive

December 18, 2023

Nginx for Newbie: Setting up the port_in_redirect Directive

Nginx is a popular web server and reverse proxy server that is known for its high performance, scalability, and flexibility. It is widely used by web developers and system administrators to serve static and dynamic content, handle load balancing, and improve website performance. In this article, we will explore the port_in_redirect directive in Nginx and how it can be set up to enhance your website’s functionality.

Understanding the port_in_redirect Directive

The port_in_redirect directive in Nginx allows you to control whether the server should include the port number in the “Location” header of HTTP redirect responses. By default, Nginx includes the port number in the “Location” header, which can sometimes cause issues when the server is behind a reverse proxy or load balancer.

When Nginx is used as a reverse proxy or load balancer, it listens on a specific port (e.g., 80 or 443) and forwards requests to backend servers. In such cases, including the port number in the “Location” header can lead to incorrect redirects, as the backend servers might be listening on different ports.

By configuring the port_in_redirect directive, you can ensure that the correct port is used in the “Location” header, regardless of the port Nginx is listening on.

Setting up the port_in_redirect Directive

To set up the port_in_redirect directive in Nginx, you need to modify your Nginx configuration file. The configuration file is usually located at /etc/nginx/nginx.conf or /etc/nginx/conf.d/default.conf.

Open the configuration file in a text editor and locate the http block. Inside the http block, add the following line:

port_in_redirect off;

This line tells Nginx to exclude the port number in the “Location” header of redirect responses.

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

sudo service nginx restart

Once Nginx has restarted, the port_in_redirect directive will be active, and the port number will no longer be included in the “Location” header of redirect responses.

Conclusion

The port_in_redirect directive in Nginx is a useful tool for controlling the inclusion of the port number in the “Location” header of redirect responses. By setting up this directive, you can ensure that your website functions correctly, even when Nginx is behind a reverse proxy or load balancer.

For more information about Nginx and its features, you can visit the Server.HK website. Server.HK offers reliable and high-performance VPS hosting solutions that are optimized for Nginx and other web servers.

Recent Posts

  • US VPS for Asia-Pacific Businesses: When to Choose a US Server Over Hong Kong (2026)
  • How to Configure Nginx Load Balancing on Hong Kong VPS for High-Traffic Sites (2026)
  • How to Migrate Your Website to a Hong Kong VPS: Zero-Downtime Transfer Guide (2026)
  • How to Set Up Redis on Hong Kong VPS: Caching, Queues, and Session Storage (2026)
  • How to Host a Python Flask or Django Application on Hong Kong VPS (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