前端对外暴露端口:http://localhost:8080
后端在前端中的暴露端口:http://localhost:8082/api
后端的具体端口:http://localhost:88/api(由网关再转发到具体的服务,进行过滤)
在nginx里的config文件夹里进行配置,找到nginx.conf
前端端口监听
后端端口暴露
用具体的if来判断跨域
location / {
if ($request_method = 'OPTIONS') {
add_header Access-Control-Allow-Origin 'http://localhost:8080';
add_header Access-Control-Allow-Headers '*';
add_header Access-Control-Allow-Methods '*';
add_header Access-Control-Allow-Credentials 'true';
return 204;
}
if ($request_method != 'OPTIONS') {
add_header Access-Control-Allow-Origin 'http://localhost:8080' always;
add_header Access-Control-Allow-Credentials 'true';
}
最后的proxy_pass 就是所要转发的网关的端口,而之后再由网关转发到具体的服务