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

December 18, 2023

Nginx for Newbie: Setting up real_ip_header 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 many websites and web applications to handle heavy traffic loads efficiently. One of the essential features of Nginx is the ability to handle client IP addresses correctly, especially when it is behind a load balancer or proxy server.

The Importance of real_ip_header Directive

When Nginx is used behind a load balancer or proxy server, the client IP address received by Nginx is typically the IP address of the load balancer or proxy server itself. This can cause issues when you need to log or track the actual client IP address for various purposes, such as security, analytics, or troubleshooting.

To solve this problem, Nginx provides the real_ip_header directive, which allows you to specify the HTTP header that contains the real client IP address. By configuring this directive correctly, Nginx can extract the client IP address from the specified header and use it for various purposes.

Configuring real_ip_header Directive

To configure the real_ip_header directive in Nginx, you need to follow these steps:

  1. Identify the HTTP header that contains the real client IP address. This header is typically set by your load balancer or proxy server. Common headers used for this purpose are X-Forwarded-For and X-Real-IP.
  2. Edit your Nginx configuration file, usually located at /etc/nginx/nginx.conf or /etc/nginx/conf.d/default.conf.
  3. Inside the http block, add the following line to specify the HTTP header:
http {
  # ...
  real_ip_header X-Forwarded-For; # Replace with your actual header
  # ...
}

Make sure to replace X-Forwarded-For with the actual header used by your load balancer or proxy server.

After making the changes, save the configuration file and restart Nginx for the changes to take effect. You can use the following command to restart Nginx:

sudo systemctl restart nginx

Verifying the Configuration

To verify that the real_ip_header directive is configured correctly, you can check the Nginx access logs or use a simple PHP script to display the client IP address. Here’s an example PHP script:

<?php
echo $_SERVER['REMOTE_ADDR'];
?>

Upload this script to your web server and access it using a web browser. If the real_ip_header directive is configured correctly, you should see your actual client IP address instead of the load balancer or proxy server IP address.

Conclusion

Configuring the real_ip_header directive in Nginx is crucial for correctly handling client IP addresses when Nginx is behind a load balancer or proxy server. By following the steps outlined in this article, you can ensure that Nginx extracts the real client IP address from the specified header and uses it for various purposes, such as logging and tracking.

For more information about Nginx and its features, consider exploring Hong Kong VPS Hosting. With their reliable and high-performance VPS solutions, you can take full advantage of Nginx and optimize your web server for better performance and scalability.

Recent Posts

  • 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)
  • 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)

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