nginx url rewrite
在服务器端安装
nginx
服务,将
H5
前端的访问路径和
java
后端的访问路径放在一个域名下,由
nginx
根据
url
请求路径进行转发
:
http://aaa.afubx.com/afbx-wap
==
》
afbx-wap
http://aaa.afubx.com/afbx-rest
==> afbx-rest
Nginx
配置:
server {
listen 80;
server_name aaa.afubx.com;
index index.html index.htm;
location /afbx-rest {
#proxy_redirect off;
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_pass http://192.168.0.202:18081;
}
location /afbx-wap/afbx-wap {
index index.php index.html index.htm;
rewrite “/afbx-wap/(.*)” /$1;
}
location /afbx-wap {
#proxy_redirect off;
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_pass http://192.168.0.202:18083;
}
}
优点:通过同一域访问,不存在跨域问题。
缺点:需要安装配置
nginx
,并且存在
nginx
单点压力的隐患。
跨域资源共享
CROS
服务端根据
http
请求头的
Origin
字段进行判断,设置允许访问的域名访问。
解决方法:
服务端拦截请求,通过
HttpServletRequest.getHeader(
“Origin”)
进行判断,如果是白名单就设置
HttpServletResponse.setHeader(
“Access-Control-Allow-Origin”,”{
来源域名
}
”);
Java
代码:
List whiteList = new ArrayList();
whiteList.add(“”);
String filterDrive = “wap”;
HttpServletResponse res = (HttpServletResponse) message.get(AbstractHTTPDestination.HTTP_RESPONSE);
HttpServletRequest req = (HttpServletRequest) message.get(AbstractHTTPDestination.HTTP_REQUEST);
If(filterDrive.equas(req.getParameter(“drive”))){
String origin = req.getHeader(“Origin”);
If(whiteList.contains(origin)){
res.setHeader(“Access-Control-Allow-Origin”, origin);
}
}
优点:使用
ajax
直接跨域访问,服务端添加支持,对以前的代码无影响。
缺点: