Detailed explanation of solving the problem of cross-domain access of nginx/apache static resources

Detailed explanation of solving the problem of cross-domain access of nginx/apache static resources

1. Apache static resource cross-domain access

Find the Apache configuration file httpd.conf

Find this line

#LoadModule headers_module modules/mod_headers.so

Remove the # comment character

LoadModule headers_module modules/mod_headers.so

The purpose is to enable the Apache header information custom module

Add a new header to the standalone host configuration file

Header set Access-Control-Allow-Origin *

For example:

<VirtualHost *:88>
 ServerAdmin [email protected]
 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

This means adding a header when accessing resources on this domain name.

Restart apache

service httpd restart

2. nginx static resources allow cross-domain access

Similarly, find the corresponding domain name configuration file

Add configuration in the server module:

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

example:

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

nginx reload

./nginx -s reload

After configuring through the above method, there is no problem in accessing static resources across domains again

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

You may also be interested in:
  • How to use nginx to access local static resources on Linux server
  • Detailed explanation of Nginx + Tomcat to separate requests for dynamic data and static resources
  • Detailed explanation of simple configuration of nginx static resource server
  • Nginx implements reverse proxy example of static resources
  • Use nginx-http-concat module to merge static resource files in nginx
  • How to publish static resources in nginx

<<:  Summary of Binlog usage of MySQL database (must read)

>>:  JavaScript uses setTimeout to achieve countdown effect

Recommend

How to reset the root password in CentOS7

There are various environmental and configuration...

js implements random roll call

This article shares the specific code of js to im...

Detailed use of Echarts in vue2 vue3

Table of contents 1. Installation 2. Use Echarts ...

Trash-Cli: Command-line Recycle Bin Tool on Linux

I believe everyone is familiar with the trashcan,...

Detailed explanation of for loop and double for loop in JavaScript

for loop The for loop loops through the elements ...

Example of making a butterfly flapping its wings with pure CSS3

Pure CSS3 makes a butterfly flapping its wings, s...

Detailed explanation of MySQL persistent statistics

1. The significance of persistent statistical inf...

js to achieve 3D carousel effect

This article shares the specific code for impleme...

A brief discussion on the $notify points of element

My original intention was to encapsulate the $not...

Introduction to the usage of common XHTML tags

There are many tags in XHTML, but only a few are ...

Implementing a simple age calculator based on HTML+JS

Table of contents Preface Demonstration effect HT...