First of all, we use nginx as a proxy server to interact with users, so that users only need to interact on port 80, which avoids cross-domain problems, because we are all on port 80 Interactive;
Let’s take a look at the specific configuration of using nginx as a reverse proxy:
server { listen 80; #Listen to port 80, can be changed to other ports server_name localhost; # The domain name of the current service #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass http://localhost:81; proxy_redirect default; } location /apis { #Add proxy configuration with access directory as /apis rewrite ^/apis/(.*)$ /$1 break; proxy_pass http://localhost:82; } #The following configuration is omitted
1. When the user sends localhost:80/, it will be forwarded to the http://localhost:81 service by nginx;
2. When the interface requests interface data , as long as it starts with /apis, it will be forwarded to the back-end interface server by nginx;
Summary: The principle of nginx to realize cross-domain is actually to put the web project and the back-end interface project into one domain In this way, there is no cross-domain problem, and then request different servers (servers that really work) according to the request address;
Recommended tutorial: “JS Tutorial”
The above is what is a cross-domain problem And how to solve the details, please pay attention to other related articles on 1024programmer.com!