How to enable CORS (Cross-Origin Resource Sharing) in Apache Httpd

Solution Verified - Updated -

Environment

  • Red Hat Enterprise Linux (RHEL) 7, 8, 9
    • Apache Httpd

Issue

  • We need to enable cors in Apache Httpd.

Resolution

To enable CORS the following header directive can be used:

Header set Access-Control-Allow-Origin "*"
  • Note: Access-Control-Allow-Origin header describes whether all (*), or a specific origin is allowed to access resources.

To allow requests only from a specific origin.

Header set Access-Control-Allow-Origin "http://mysite.com"

Example: Let’s assume a simple VirtualHost-based configuration that proxies request to an internal service.

<VirtualHost *:443>
    ServerName example.com
    # ... SSL configuration ...

    # ... CORS configuration, discussed in this article ...

    # ... proxy configuration ... ProxyPass, ProxyPassReverse, etc
</VirtualHost>

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.

Comments