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

December 18, 2023

Nginx for Newbie: Setting Up Expires Directive

Nginx is a powerful web server and reverse proxy server that is known for its high performance, stability, and scalability. One of the key features of Nginx is its ability to handle static content efficiently, which can greatly improve the performance of your website. In this article, we will explore how to set up the expires directive in Nginx to leverage browser caching and optimize the delivery of static files.

What is the expires directive?

The expires directive in Nginx allows you to specify how long a browser should cache a particular file. By setting an appropriate expiration time for static files such as images, CSS, and JavaScript files, you can reduce the number of requests made to the server and improve the overall loading speed of your website.

Configuring the expires directive

To configure the expires directive in Nginx, you need to modify the server block in your 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 server block that corresponds to your website.

Within the server block, you can add the following code to set the expiration time for different types of files:

location ~* .(jpg|jpeg|png|gif|ico|css|js)$ {
    expires 7d;
}

In the above example, we have set the expiration time for image files, CSS files, and JavaScript files to 7 days. This means that once a browser downloads these files, it will cache them for 7 days before making another request to the server.

You can customize the expiration time according to your needs. For example, if you have static files that rarely change, you can set a longer expiration time to further improve caching efficiency. On the other hand, if you frequently update your static files, you can set a shorter expiration time to ensure that the latest version is always served.

Verifying the configuration

After making the necessary changes to your Nginx configuration file, you need to reload the Nginx service to apply the new configuration. You can do this by running the following command:

sudo service nginx reload

To verify if the expires directive is working correctly, you can use browser developer tools to check the response headers of your static files. Look for the “Cache-Control” or “Expires” header, which should indicate the expiration time you have set.

Conclusion

The expires directive in Nginx is a powerful tool for optimizing the delivery of static files. By setting an appropriate expiration time, you can leverage browser caching and reduce the load on your server. Remember to customize the expiration time based on the frequency of file updates and the caching requirements of your website. With Nginx and the expires directive, you can significantly improve the performance and user experience of your website.

For more information on VPS hosting and how it can benefit your website, check out Server.HK. They offer top-notch VPS solutions that are reliable, scalable, and affordable.

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