Solution to Apache cross-domain resource access error

Solution to Apache cross-domain resource access error

In many cases, large and medium-sized websites will place static resources (such as font files, pictures, etc.) on independent servers or CDNs and use independent resource domain names (such as res.test.com) in order to distribute static resources, speed up access, and reduce the pressure on the main site.

However, in actual deployment, you will find that the browser cannot load resources from these different domain names, and the Firefox console will report an error:

<span role="presentation" class="objectBox objectBox-errorMessage "><span class="errorMessage ">Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://xxxxx. (Reason: CORS header missing 'Access-Control-Allow-Origin'). </span></span>

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://xxxxx. (Reason: CORS request failed).

This is because modern browsers define it as a cross-origin resource and do not allow it to be loaded.

To understand cross-domain, you must first understand the same-origin policy. The same-origin policy is a very important security policy implemented on browsers for security reasons.

What is homology?

A URL consists of a protocol, domain name, port, and path. If two URLs have the same protocol, domain name, and port, they have the same origin.

Same-origin policy:

The browser's same-origin policy restricts "documents" or scripts from different origins from reading or setting certain properties of the current "document". (White hat talks about web security[1])
Scripts loaded from one domain are not allowed to access document properties from another domain.

So the key is how to solve it. In fact, it is very simple. Just add a header information on the static resource server:

Access-Control-Allow-Origin *

This article uses apache for operation, nginx is similar

First edit 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

Then add a line to the virtual host of the independent resource domain name

Header set Access-Control-Allow-Origin *

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

Restart apache

Visit again, OK!

NameVirtualHost 10.0.0.2:80
<VirtualHost 10.0.0.2:80>
  DocumentRoot /var/www/host.example.com
  ServerName host.example.com
  JkMount /webapp/* jkworker
  Header set Access-Control-Allow-Origin "*"
  RewriteEngine on
  RewriteRule ^/otherhost http://otherhost.example.com/webapp [R,L]
</VirtualHost>

And here's an example of the Apache config for the second:

NameVirtualHost 10.0.1.2:80
<VirtualHost 10.0.1.2:80>
  DocumentRoot /var/www/otherhost.example.com
  ServerName otherhost.example.com
  JkMount /webapp/* jkworker
  Header set Access-Control-Allow-Origin "*"
</VirtualHost>

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of the process of enabling cross-domain mode in PHP Apache
  • Detailed explanation of solving the problem of cross-domain access of nginx/apache static resources
  • How to configure Apache server for cross-domain requests
  • Apache configuration supports CORS (cross-origin resource sharing) example
  • Detailed explanation of the pitfalls of Apache domain name configuration
  • Detailed description and usage examples of Apache Commons tool classes in Java's commonly used class library
  • Solve the problem of case sensitivity of Linux+Apache server URL
  • Complete tutorial on installing Apache, MySQL, PHP, LAMP on Ubuntu 18.04

<<:  React.js framework Redux basic case detailed explanation

>>:  MySQL index knowledge summary

Recommend

Detailed explanation of how Nginx works

How Nginx works Nginx consists of a core and modu...

Linux method example to view all information of the process

There is a task process on the server. When we us...

Implementation of CSS text shadow gradually blurring effect

text-shadow Add a shadow to the text. You can add...

Introduction to the deletion process of B-tree

In the previous article https://www.jb51.net/arti...

CSS complete parallax scrolling effect

1. What is Parallax scrolling refers to the movem...

What hidden attributes in the form can be submitted with the form

The form elements with visibility=hidden and displ...

MySQL table return causes index invalidation case explanation

Introduction When the MySQL InnoDB engine queries...

MySQL foreign key setting method example

1. Foreign key setting method 1. In MySQL, in ord...

What are Web Slices?

IE8 new feature Web Slices (Web Slices) Microsoft...

Modify the maximum number of mysql connections and configuration files in docker

1. Find the mysql image docker ps 2. Enter the mi...

MySQL series multi-table join query 92 and 99 syntax examples detailed tutorial

Table of contents 1. Cartesian product phenomenon...