需要将http://ip:8080/api/api/userLogin代理到http://ip:8099/api/userLogin
nginx中的location 需要如何配置?
我们需要修改nginx的配置文件,我的nginx为默认的。服务器用的系统是centos的,nginx.conf文件在这个目录下 /usr/local/nginx/conf/nginx.conf,用vim 修改,具体服务器系统是什么的按照相应操作就可,具体如下:
vim /usr/local/nginx/conf/nginx.conf
nginx.conf内容:
server{
listen 80; #监听端口
server_name localhost; #服务器ip地址
location / { #访问上面服务器ip地址时,出现的项目
root html; #根目录为html
index index.html index.htm;#默认访问的文件
}
location /test{ #访问服务器ip/test时,我们实际访问的是下面代理的url
proxy_pass http://apis.juhe.cn/idioms/query; #代理设置,我们想要请求的真实接口url
}
}
我的项目地址是在http://localhost下,我的后台api接口在http://apis.juhe.cn/idioms/query?请求参数下,显然如果访问就是跨域。这时候,我们通过监听80端口,设置代理当访问/test时,将转发给我的代理去访问。即实现跨域ngnix端口转发实现跨域请求。