Home  >  Article  >  Operation and Maintenance  >  Analysis of the principle of nginx solving cross-domain problems

Analysis of the principle of nginx solving cross-domain problems

王林
王林forward
2020-06-13 16:30:337646browse

Analysis of the principle of nginx solving cross-domain problems

Let’s first talk about what cross-domain is:

The same-origin policy restricts how documents or scripts loaded from the same source can interact with Interact with a resource from another source. This is an important security mechanism for isolating potentially malicious files. Read operations between different sources are generally not allowed.

Then let’s talk about what is the same origin:

If the protocol, port (if specified) and domain name of the two pages are the same, then the two pages have the same origin. Same source.

Analysis of the principle of nginx solving cross-domain issues:

For example:

The domain name of the front-end server is: fe.server.com

The domain name of the back-end service is: dev.server.com

Now when I make a request to dev.server.com from fe.server.com, it will definitely appear cross-domain.

Now we only need to start an nginx server, set server_name to fe.server.com, and then set the corresponding location to intercept front-end cross-domain requests, and finally proxy the request back to dev.server.com. As shown in the following configuration:

server {
        listen       80;
        server_name  fe.server.com;
        location / {
                proxy_pass dev.server.com;
        }
}

This can perfectly bypass the browser's same-origin policy.

fe.server.com's access to nginx's fe.server.com is a same-origin access, and the request forwarded by nginx to the server will not trigger the browser's same-origin policy.

Recommended tutorial: nginx tutorial

The above is the detailed content of Analysis of the principle of nginx solving cross-domain problems. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.im. If there is any infringement, please contact admin@php.cn delete