How to activate CORS in Apache and Nginx
Marc Wagner
August 26, 2021
The CORS header is used to restrict cross-origin HTTP requests via scripts. In some cases, however, it makes sense to adapt this restriction.
This can be useful for your WordPress website, for example, if you use WPML. You can then use the CORS header to allow resources to be loaded from other domains so that they do not have to be provided twice (e.g. fonts, CSS & JS files, etc.).
How to activate CORS with Apache #
To activate CORS for Apache, you must either change httpd.conf or extend your HTACCESS file. However, the HTACCESS variant only works if you have also activated mod_headers for Apache.
To activate CORS directly via httpd.conf, you must add the following:
Header set Access-Control-Allow-Origin "*"
Alternatively, insert the following line in HTACCESS:
<IfModule mod_headers.c> Header set Access-Control-Allow-Origin "*" </IfModule>
This removes all restrictions, allowing other domains to retrieve data. Alternatively, you can also exclude individual domains from the CORS header restriction by inserting the following line:
Header set Access-Control-Allow-Origin "https://meinedomain.de"
Again, the HTACCESS variant:
<IfModule mod_headers.c> Header set Access-Control-Allow-Origin "https://meinedomain.de" </IfModule>
However, if you want to allow CORS for several domains, it gets a little more complicated, then you have to store the whole thing in httpd.conf as follows:
SetEnvIf Origin "http(s)?://(www\.)?(meinedomain.de|meineanderedomain.de)$" AccessControlAllowOrigin=$0$1 Header set Access-Control-Allow-Origin "%{AccessControlAllowOrigin}e" env=AccessControlAllowOrigin
This method can also be mapped in HTACCESS, see here:
<IfModule mod_headers.c> SetEnvIf Origin "http(s)?://(www\.)?(meinedomain.de|meineandereodomain.example)$" AccessControlAllowOrigin=$0$1 Header add Access-Control-Allow-Origin %{AccessControlAllowOrigin}e env=AccessControlAllowOrigin Header set Access-Control-Allow-Credentials true </IfModule>
Now you just need to save the changes and restart your Apache service.
How to activate CORS with Nginx #
CORS can also be activated and changed with Nginx — only the syntax is different compared to Apache. To do this, you must add the following to the configuration file (e.g.: /etc/nginx/conf.d/default.conf).
add_header Access-Control-Allow-Origin "*";
If you want to activate it for all domains.
Alternatively, you can deactivate the CORS header restrictions for specific domains only. To do this, you must explicitly specify the domain. The whole thing then looks something like this:
add_header Access-Control-Allow-Origin "https://meinedomain.de";
However, if you want to exclude several domains from the restriction, you must include a query for this. This is because browsers only allow an “Access-Conrol-Allow-Origin” header.
To integrate the whole thing dynamically, you can use this code and adapt it to your specifications:
if ($http_origin ~* ^https?://(.+\.)?(meinedomain1|meinedomain2|meinedomain3)\.(de|fr|com)$) { add_header "Access-Control-Allow-Origin" "$http_origin"; add_header "Vary" "Origin"; }
That’s about it. Remember to restart your Nginx service after saving the file to apply the changes.
Conclusion #
You should now be able to change the CORS header for Apache and Nginx to fix possible errors. Although it is possible to create a wildcard for all domains, you should only activate the CORS header for individual domains for security reasons.
Do you have any comments or questions? Then please leave us a comment.

Artikel von:
Marc Wagner
Hi Marc here. I’m the founder of Forge12 Interactive and have been passionate about building websites, online stores, applications and SaaS solutions for businesses for over 20 years. Before founding the company, I already worked in publicly listed companies and acquired all kinds of knowledge. Now I want to pass this knowledge on to my customers.
Then nginx multiple origin example is wrong, it probably should be
“ ‘
if ( $http_origin ~* (https?://(.+\.)? ) {(meinedomain1|meinedomain2|meinedomain3)\.(?:de|fr|com)$) }
add_header “Access-Control-Allow-Origin” “$http_origin”;
add_header Vary Origin;
}
“ ‘
Thanks — we’ve updated the code above.