Skip to content
Ercan Ermis
Ercan Ermis

notes for everyone about cloud technology

  • Cloud
    • AWS
    • GCP
  • Container
    • Kubernetes
    • Docker
  • Linux
  • DevOps
  • Privacy Policy
  • Contact
Ercan Ermis

notes for everyone about cloud technology

CORS in Nginx: Configuration Guide for Enhanced Security

Ercan, April 19, 2023April 19, 2023

Cross-Origin Resource Sharing (CORS) is a crucial mechanism for enabling secure communication between web servers and browsers. By providing a way for web applications to request resources from different domains, CORS helps overcome the limitations of the Same-Origin Policy (SOP), which restricts web applications from accessing data on a different domain than the one serving the application.

In this blog post, we’ll explore the importance of CORS, how it works, and how to configure it in Nginx for different scenarios. We’ll also delve into key CORS concepts, such as allow-origin, allow-methods, allow-headers, and expose-headers.

CORS Importance

CORS is essential for modern web applications, as it enables communication between servers and clients from different domains. This ensures that applications can access data and services hosted on various domains securely while maintaining the integrity of the user’s data.

Configuring CORS in Nginx

Nginx is a popular web server that can be easily configured to support CORS. In this section, we’ll provide two different configuration examples for enabling CORS on an Nginx server. The first configuration will be for a single domain, while the second will cover multiple domains and subdomains.

Configuration Example 1: Single Domain (domain.com)

To enable CORS for a single domain, add the following to the server block in your Nginx configuration file:

location / {
    add_header 'Access-Control-Allow-Origin' 'https://domain.com';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
    add_header 'Access-Control-Allow-Headers' 'Content-Type, Authorization';
    add_header 'Access-Control-Expose-Headers' 'Content-Length';
}

Configuration Example 2: Multiple Domains and Subdomains (domain.com, domain1.com, domain2.com, domain3.com, and subdomains)

To enable CORS for multiple domains and subdomains, use the following configuration in your Nginx server block:

location / {
    if ($http_origin ~* (https?://(?:.+\.)?(domain\.com|domain1\.com|domain2\.com|domain3\.com)$)) {
        add_header 'Access-Control-Allow-Origin' '$http_origin';
    }
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
    add_header 'Access-Control-Allow-Headers' 'Content-Type, Authorization';
    add_header 'Access-Control-Expose-Headers' 'Content-Length';
}

CORS Concepts

  1. Access-Control-Allow-Origin: This header specifies which origins are allowed to access the resources on the server. In our examples, we either set a single domain or use a regular expression to match multiple domains and subdomains.
  2. Access-Control-Allow-Methods: This header defines the allowed HTTP methods (e.g., GET, POST, OPTIONS) that the client can use when making requests to the server.
  3. Access-Control-Allow-Headers: This header lists the allowed HTTP headers that the client can include in its requests. Common headers include “Content-Type” and “Authorization.”
  4. Access-Control-Expose-Headers: This header specifies which headers the server exposes to the client. In our examples, we expose the “Content-Length” header.

Conclusion

CORS is a vital security mechanism for modern web applications. By understanding and implementing CORS in Nginx, you can ensure that your web applications can securely access resources across different domains. With this guide, you should now be able to configure CORS in Nginx for single or multiple domains and subdomains while also understanding the importance of key CORS concepts like allow-origin, allow-methods, allow-headers, and expose-headers. Ensuring your Nginx server is CORS-compliant will improve the security and functionality of your web applications, providing a better user experience and increased compatibility across various platforms. By making your content SEO-friendly and easy to understand, you’ll also improve your web application’s discoverability and user engagement.

Share on Social Media
x facebook linkedin reddit
Linux corsnginxnginx configsecurityweb security

Post navigation

Previous post
Next post
  • AWS (45)
    • Serverless (4)
  • Best (9)
  • DevOps (16)
  • Docker (10)
  • GCP (3)
  • Kubernetes (3)
  • Linux (13)
  • Uncategorized (6)

Recent Posts

  • Automating AWS CloudWatch Log Group Tagging with Python and Boto3
  • Automating AWS ECR Tagging with Python and Boto3
  • Automating ECR Image Cleanup with Bash
  • Update ECR Repositories with Bash Script
  • Why Automated Tests Are Essential in Your CI/CD Pipeline and Development Flow
  • Streamline Your AWS ECR Management with This Powerful Bash Script
  • Setting up DKIM for Google Workspace (Gmail) using Terraform and AWS Route 53
  • Automate AWS Site-to-Site VPN Monitoring
  • Optimizing Docker Images: Tips for Reducing Image Size and Build Time
  • Monitoring EC2 Disk Space with a Simple Bash Script and Slack Alerts
  • Securing Docker Containers: Best Practices for Container Security
  • Mastering Dockerfile: Writing Efficient, Scalable Container Builds
  • Migrating a Git Repository from GitLab to GitHub with GPG-Signed Commits
  • Accessing AWS Services in Private Subnets Without 0.0.0.0/0
  • Understanding AWS Regions, Availability Zones, and VPCs: A Comprehensive Guide
©2025 Ercan Ermis | WordPress Theme by SuperbThemes