近期项目需求,有一个百度上ip获取的接口在打包上线后出现了跨域问题,一番犹豫仍是选择用Nginx反向代理 毕竟你们都说是很成熟的解决方案。html
关于正向代理和反向代理之类的就不说了,资料不少,能够参考:blog.csdn.net/zt157326258…nginx
仍是说怎么用nginx解决跨域吧apache
1.下载nginx 地址:nginx.org/en/download…api
2.解压打开配置文件:nginx-1.14.2 ➡conf➡nginx.conf跨域
listen 的端口能够随便填 可是要注意的是端口不能被占用,这个坑踩一脚浪费我半天时间。因为当时不知道本机的80端口被apache占用,而且nginx启动出错不会主动报错(须要本身查日志,位置:log文件夹的error.conf),致使启动nginx失败可是半天找不到缘由。socket
若是日志中显示:[emerg] 9012#2384: bind() to 0.0.0.0:80 failed (10013: An attempt was made to access a socket in a way forbidden by its access permissions)即表明端口占用工具
须要打开命令行工具输入: netstat -ano 查看端口 而后打开管理器根据PID关掉该占用端口的程序就好了ui
3.启动nginx 启动文件里的nginx.exe便可,也能够命令行输入 'start nginx' 启动,启动之后会有一个终端闪一下,是正常现象, 而后打开 http://localhost:8065/ 有正常显示起始页就OK了, 跨域请求数据已经能够被使用啦!spa
nginx经常使用命令:.net
nginx -s reload 优雅重启,并从新载入配置文件nginx.conf
nginx -s quit 优雅中止nginx,有链接时会等链接请求完成再杀死worker进程
nginx -s reopen 从新打开日志文件,通常用于切割日志
nginx -v 查看版本
nginx -t 检查nginx的配置文件
nginx -h 查看帮助信息
nginx -c filename 指定配置文件
复制代码
伸手党:
listen 8065;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root E:\\nginx ;
index demo.html demo.htm;
}
location /baiduIp{
rewrite ^/baiduIp/(.*)$ /$1 break;
proxy_pass http://api.map.baidu.com/location/ip;
}
复制代码
ps:有什么不对请在评论区留言,感谢!