需求场景:
一、前端资源,部署于服务器 p1
二、后台请求资源,部署于服务器p2
那么问题来了,服务器域名、端口都不通,自然就存在跨域问题(主要是请求头的问题),有多种方式,这里列举一个说明一下,前端资源所在的服务添加代理请求
nginx部署
config
server {
listen 8080;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
#post前缀的所有请求
location /post {
proxy_pass http://your.serve.com;
}
}
启动nginx后,访问 localhost:8080 即可访问 index.html ,而在页面上发起的所有/post请求,如 localhost:8080/post/… 都会被代理到 http://your.serve.com,
这样发起请求的就可以设置header,不存在跨域问题。
相关链接
nginx命令
详解Nginx 利用代理转发请求示例
Windows下Nginx的配置及配置文件部分介绍
nginx实现请求转发
跨域问题