• 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: enable hotlink protection

December 18, 2023

Nginx for Newbie: Enable Hotlink Protection

Hotlinking, also known as inline linking or leeching, is the practice of displaying an image or file on a website by linking directly to the source on another website. While this may seem convenient, it can have negative consequences for both the website owner and the server hosting the content. Hotlinking can lead to increased bandwidth usage, slower website loading times, and even theft of intellectual property.

Fortunately, Nginx, a popular web server software, provides a simple and effective way to enable hotlink protection. By implementing hotlink protection, you can prevent other websites from directly linking to your images and files, ensuring that your server resources are used only by legitimate visitors.

Step 1: Install Nginx

If you haven’t already, the first step is to install Nginx on your server. You can follow the official Nginx documentation for detailed instructions on how to install it on your specific operating system.

Step 2: Configure Nginx

Once Nginx is installed, you need to configure it to enable hotlink protection. Open the Nginx configuration file, usually located at /etc/nginx/nginx.conf, using a text editor.

Within the http block, add the following code:

location ~ .(jpe?g|png|gif)$ {
    valid_referers none blocked ~.google. ~.bing. ~.yahoo. yourdomain.com;
    if ($invalid_referer) {
        return 403;
    }
}

This code specifies that hotlink protection should be applied to JPEG, PNG, and GIF files. You can modify the file extensions as needed. The valid_referers directive specifies the domains that are allowed to hotlink to your files. In this example, we allow hotlinking from yourdomain.com, as well as search engines like Google, Bing, and Yahoo. You can add or remove domains as necessary.

The if ($invalid_referer) condition checks if the referring domain is not in the list of valid referers. If it is not, a 403 Forbidden error is returned.

Save the configuration file and restart Nginx for the changes to take effect.

Step 3: Test Hotlink Protection

To test if hotlink protection is working correctly, try accessing one of your protected images directly from another website. You should receive a 403 Forbidden error, indicating that hotlinking is blocked.

If you want to customize the error page shown to blocked hotlinkers, you can create a custom HTML page and specify it using the error_page directive in the Nginx configuration file.

Summary

Enabling hotlink protection with Nginx is a straightforward process that can help protect your server resources and prevent unauthorized use of your content. By following the steps outlined in this article, you can easily implement hotlink protection on your website.

For reliable and secure VPS hosting solutions, consider Server.HK. Our Hong Kong VPS Hosting plans offer high-performance servers and excellent customer support. Protect your website and enjoy fast loading times with our top-notch VPS solutions.

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