• 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 Cross-origin resource sharing (CORS)

December 18, 2023

Nginx for Newbie: Enable Cross-origin Resource Sharing (CORS)

When it comes to web development, one of the most common challenges developers face is dealing with Cross-origin Resource Sharing (CORS) issues. CORS is a security mechanism implemented by web browsers to restrict web pages from making requests to a different domain than the one that served the web page. This security measure is in place to prevent malicious attacks and protect user data.

If you are new to Nginx, a popular web server and reverse proxy server, this article will guide you on how to enable CORS in Nginx and overcome the CORS-related challenges.

Understanding CORS

Cross-origin Resource Sharing (CORS) is a mechanism that allows web servers to specify which origins (domains) are allowed to access their resources. By default, web browsers restrict cross-origin requests, meaning that a web page served from one domain cannot make requests to another domain.

For example, if your website is hosted on “example.com” and you want to make an AJAX request to an API hosted on “api.example.com,” the browser will block the request due to CORS restrictions.

Enabling CORS in Nginx

To enable CORS in Nginx, you need to add the appropriate headers to the server configuration. Here’s an example of how to enable CORS for all requests:

server {
    listen 80;
    server_name yourdomain.com;

    location / {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
        add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
    }
}

In the above configuration, the ‘Access-Control-Allow-Origin’ header is set to ‘*’, allowing requests from any domain. If you want to restrict access to specific domains, you can replace ‘*’ with the desired domain.

The ‘Access-Control-Allow-Methods’ header specifies the allowed HTTP methods for cross-origin requests. In this example, GET, POST, and OPTIONS methods are allowed.

The ‘Access-Control-Allow-Headers’ header specifies the allowed request headers. You can customize this header based on your application’s requirements.

The ‘Access-Control-Expose-Headers’ header specifies the headers that can be exposed to the browser. This header is optional and can be omitted if not needed.

Testing CORS Configuration

Once you have configured CORS in Nginx, it’s essential to test if the configuration is working correctly. You can use browser developer tools or tools like cURL to make cross-origin requests and check if the appropriate headers are present in the response.

For example, using cURL:

curl -I https://yourdomain.com/api/endpoint

The response should include the ‘Access-Control-Allow-Origin’ header with the appropriate value.

Summary

Enabling Cross-origin Resource Sharing (CORS) is crucial for web developers to allow cross-origin requests and overcome the restrictions imposed by web browsers. By following the steps outlined in this article, you can easily configure CORS in Nginx and ensure seamless communication between different domains.

If you are looking for reliable and high-performance VPS hosting solutions, consider Server.HK. With a wide range of hosting plans and excellent customer support, Server.HK is a trusted provider in the industry. Learn more about Hong Kong VPS Hosting and take your website performance to the next level.

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