CDN-accelerated WordPress triggers CORS and causes cross-domain load failure

In the past two days, I have tossed CDN acceleration to improve the access speed of my blog. I used the Alibaba Cloud CDN acceleration solution. When I used it, I found a problem. The CDN acceleration of some resources failed. The reason was that CORS was triggered, because the CDN acceleration URL was inconsistent with the blog URL. The cross-domain request is unsuccessful;

CDN-accelerated Wordpress triggers CORS and causes cross-domain loading failure

From the error, it is found that the Off and Tff fonts are loaded with errors:

(index):1 Access to Font at 'http://cdn.5yun.org/wp-content/themes/yusi1.0/fonts/fontawesome-webfont.woff?v=4.1.0' from origin 'http://www.5yun.org' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://www.5yun.org' is therefore not allowed access.

Since an error is reported, first understand the principle of CORS:

CORS defines a mechanism for cross-domain access, which enables AJAX to achieve cross-domain access. CORS allows web applications on one domain to submit cross-domain AJAX requests to another domain. Implementing this functionality is as simple as sending a response header by the server.

Solution for cross-domain loading failure caused by WordPress triggering CORS:

At present, I am using the lnmp control panel, and the nginx configuration file of the corresponding site is in the /usr/local/nginx/conf/vhost directory

Edit the current site configuration file of www.5yun.org.conf and add the following code in the appropriate place

        location ~* \.(ttf|woff)$ {
       add_header Access-Control-Allow-Origin *;
        }

After the configuration is saved, you need to restart the nginx server to take effect

/etc/init.d/nginx restart

Then ctrl+f5 is forced to refresh, and the problem of cross-domain loading errors between ttf and woff disappears;

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326058561&siteId=291194637