Detailed solution to the cross domain access problem of nginx and apache static resources

  • 2020-12-10 01:05:14
  • OfStack

1. apache static resource cross-domain access

Locate the apache configuration file httpd.conf

Find the line

#LoadModule headers_module modules/mod_headers.so

Get rid of the # comment

LoadModule headers_module modules/mod_headers.so

The goal is to open the apache header information customization module

Add header to the standalone host profile

Header set Access-Control-Allow-Origin *

Such as:


<VirtualHost *:88>
 ServerAdmin admin@example.com
 DocumentRoot "****************"
 ServerName www.jb51.com
 Header set Access-Control-Allow-Origin *

 ErrorLog "***********"
 CustomLog "****************************" common
<Directory "**************">
 SetOutputFilter DEFLATE
 Options FollowSymLinks ExecCGI
 Require all granted
 AllowOverride All
 Order allow,deny
 Allow from all
 DirectoryIndex index.html index.php
</Directory>
</VirtualHost>
ApacheCopy

Add 1 header information when accessing a resource for this domain name

Restart apache

service httpd restart

2. nginx static resources allow cross-domain access

Similarly, find the domain name configuration file

Add configuration to the server module:

add_header ‘Access-Control-Allow-Origin' ‘*';

Ex. :


server {
    listen    80;
    add_header 'Access-Control-Allow-Origin' '*';
    location /Roboto/ {
      root  /home/images;
      autoindex on;
    }
  }

nginx overloading

./nginx -s reload

Once configured in the above way, there is no problem accessing static resources across domains again

The above is both nginx/apache static resources that allow cross-domain access to the solution


Related articles: