新项目集成公司统一的SSO登陆,登陆域名aaa。前端启动服务localhost:10002,自动跳转到登陆地址,登陆成功后会进行跨域访问。
解决方案:nginx 反向代理
1.安装nginx:
brew install nginx 如果遇到update Homebrew..., control+C终止当前进程,开始下一进程安装。
2.安装成功后,启动sudo nginx或者 brew services start nginx,,访问http://localhost:8080/查看是否启动成功
3.配置nginx:
cd /usr/local/etc/
cd nginx
cat nginx.conf
读取servers 文件夹下的配置文件。
没有servers文件夹我们自己创建:
mkdir servers
cd servers
touch cld.conf 创建cld.conf 配置文件
vim cld.conf 编辑配置
当浏览器访问cld.aaa.com 时,将会指向本地服务
server { listen 80; server_name cld.aaa.com; location ~ ^/login$ { rewrite ^/login$ /api/user/login break; proxy_pass http://max-auth-new.aaa.com; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; } location / { proxy_pass http://localhost:10002/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header x-request-id $request_id; add_header Cache-Control no-cache; add_header Pragma no-cache; add_header Expires 0; if ($request_filename ~* .*.(?:js|css)$) { expires 7d; } } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } }
4.重启nginx:
sudo nginx -s reload
5.使用switchhost修改本地host ip和域名映射
127.0.0.1 localhost 255.255.255.255 broadcasthost ::1 localhost 127.0.0.1 cld.aaa.com