• 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 Security Tip: Implement cross-origin resource sharing (CORS) correctly

December 18, 2023

Nginx Security Tip: Implement Cross-Origin Resource Sharing (CORS) Correctly

When it comes to web development, security is a top priority. One crucial aspect of web security is Cross-Origin Resource Sharing (CORS). In this article, we will explore what CORS is, why it is important, and how to implement it correctly with Nginx.

What is Cross-Origin Resource Sharing (CORS)?

Cross-Origin Resource Sharing is a mechanism that allows web servers to specify which origins are allowed to access their resources. An origin is a combination of the protocol, domain, and port from which a request is made. By default, web browsers restrict cross-origin requests to ensure the security of user data.

For example, suppose you have a website hosted on “www.example.com” that wants to make an AJAX request to an API hosted on “api.example.com.” The browser will block this request by default because it is a cross-origin request. CORS allows the server hosting the API to specify that requests from “www.example.com” are allowed, bypassing the browser’s security restrictions.

Why is CORS Important?

CORS is essential for modern web applications that rely on cross-origin requests to fetch data from APIs or interact with different domains. Without CORS, browsers would block these requests, rendering many web applications useless.

By implementing CORS correctly, web developers can ensure that only trusted origins can access their resources, preventing unauthorized access and protecting sensitive user data.

Implementing CORS with Nginx

Nginx is a popular web server that can be used to implement CORS. To enable CORS for your Nginx server, follow these steps:

Step 1: Edit Nginx Configuration

Open your Nginx configuration file, usually located at “/etc/nginx/nginx.conf” or “/etc/nginx/conf.d/default.conf,” and add the following lines inside the “server” block:

location / {
    if ($request_method = 'OPTIONS') {
        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-Max-Age' 1728000;
        add_header 'Content-Type' 'text/plain; charset=utf-8';
        add_header 'Content-Length' 0;
        return 204;
    }
    if ($request_method = 'POST') {
        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';
    }
    if ($request_method = 'GET') {
        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';
    }
}

Step 2: Restart Nginx

After making the changes, save the configuration file and restart Nginx to apply the new settings. The command to restart Nginx depends on your operating system. For example, on Ubuntu, you can use:

sudo service nginx restart

Summary

Implementing Cross-Origin Resource Sharing (CORS) correctly is crucial for web application security. By allowing trusted origins to access resources, developers can ensure the integrity and confidentiality of user data. With Nginx, implementing CORS is straightforward and can be done by editing the server configuration file. To learn more about VPS hosting and how it can benefit your web applications, visit Server.HK.

Recent Posts

  • Hong Kong VPS vs Singapore VPS: Which Is Better for Your Asia Business in 2026?
  • How to Migrate from CentOS 8 to AlmaLinux or Rocky Linux Safely
  • CentOS Server Performance Tuning: Optimization Techniques for 2026
  • How to Configure SELinux in CentOS Without Breaking Your System (CentOS Stream 9/10 – 2026)
  • Managing Users and Permissions in CentOS Stream: Best Practices (CentOS Stream 9/10 – 2026)

Recent Comments

No comments to show.

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