Nginx problem solving cross-domain calls front-end interface to back-end

 1, problems encountered in the project description:

Front-end gateway service calls zuul unified interface to request the status code 200, but no data is returned.

Browser console error message: No Access-Control-the Allow-Origin header Present IS ON requested Resource. 

 2, to solve the problem

Learn https://blog.csdn.net/envon123/article/details/83270277 blog.

Project configuration is as follows: 

location /xxxx-server { #/xxxx-server不能以/结尾
add_header 'Access-Control-Allow-Origin' $http_origin;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,web-token,app-token,Authorization,Accept,Origin,Keep-Alive,User-Agent,X-Mx-ReqToken,X-Data-Type,X-Auth-Token,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 204;
}
#springcloud of zuul components external unified gateway, xxxx-server is springcloud background service component
proxy_pass http://127.0.0.1/xxxx-server; # / xxxx-server can not end with a /
proxy_set_header $ Host Host;
proxy_set_header the X-Real-- $ REMOTE_ADDR the IP;
proxy_set_header the For-X-Forwarded-$ proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto-$ scheme;
}

Guess you like

Origin www.cnblogs.com/wangymd/p/11200746.html