• 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 HTTP Strict Transport Security (HSTS)

December 18, 2023

Nginx for Newbie: Enable HTTP Strict Transport Security (HSTS)

When it comes to web server software, Nginx is a popular choice among developers and system administrators. Known for its high performance, scalability, and flexibility, Nginx is widely used to serve static content, reverse proxy, and load balance web applications. In this article, we will explore how to enable HTTP Strict Transport Security (HSTS) on Nginx, a crucial security feature that helps protect websites from certain types of attacks.

What is HTTP Strict Transport Security (HSTS)?

HTTP Strict Transport Security (HSTS) is a security policy mechanism that allows websites to instruct web browsers to only communicate with them over secure HTTPS connections. It helps prevent downgrade attacks, where an attacker tries to intercept or manipulate the communication between a user’s browser and a website by downgrading the connection from HTTPS to HTTP.

By enabling HSTS, websites can ensure that all subsequent requests from the user’s browser are automatically redirected to HTTPS, even if the user manually enters an HTTP URL or clicks on an HTTP link. This helps protect sensitive information, such as login credentials or personal data, from being transmitted over insecure connections.

Enabling HSTS on Nginx

To enable HSTS on Nginx, you need to add the appropriate HTTP response header to your server configuration. Here’s an example of how to do it:

server {
    listen 80;
    server_name example.com;

    location / {
        return 301 https://$host$request_uri;
    }

    # Enable HSTS for one year
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
}

In the above configuration, we first redirect all HTTP traffic to HTTPS using a 301 redirect. This ensures that all requests are automatically redirected to the secure version of the website. Then, we add the “Strict-Transport-Security” header with the “max-age” directive set to one year (31536000 seconds) and the “includeSubDomains” directive to include all subdomains of the website.

After making the necessary changes to your Nginx configuration, don’t forget to reload or restart the Nginx service for the changes to take effect.

Testing HSTS

Once you have enabled HSTS on your Nginx server, you can test if it is working correctly by visiting your website using different browsers. Here’s how you can check:

  • Open your website in a browser.
  • Open the browser’s developer tools (usually by right-clicking and selecting “Inspect” or pressing F12).
  • Navigate to the “Network” tab.
  • Reload the page.
  • Look for the “Strict-Transport-Security” header in the response headers. It should include the “max-age” directive and other specified options.

If the “Strict-Transport-Security” header is present and correctly configured, your website is successfully using HSTS.

Summary

In conclusion, enabling HTTP Strict Transport Security (HSTS) on your Nginx server is an important step in securing your website and protecting your users’ data. By instructing web browsers to only communicate over secure HTTPS connections, HSTS helps prevent downgrade attacks and ensures that sensitive information is transmitted securely. To enable HSTS on Nginx, add the appropriate HTTP response header to your server configuration and test it to ensure it is working correctly.

For reliable and secure VPS hosting solutions, consider Server.HK. With top-notch performance, scalability, and support, Server.HK offers a range of hosting options to meet your needs.

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