问题
swagger-editor服务所在服务器192.168.2.206
要访问的接口地址是192.168.2.95,tomcat中web项目名是rdms-pollution
虽然在java后端已经在header中加入允许跨域访问,但是cookie还是无法保存,代码如下
web.xml
<filter>
<filter-name>cors</filter-name>
<filter-class>CORSFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>cors</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
CORSFilter
import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class CORSFilter implements Filter {
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST, GET,PUT, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.addHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
chain.doFilter(req, res);
}
public void init(FilterConfig filterConfig) {}
public void destroy() {}
}
解决方案
使用nginx反向代理解决
启动nginx服务
我拉去了网易镜像的docker
docker run --name nginx --privileged=true -v /home/nginx.conf:/etc/nginx/nginx.conf -p 80:80 -d hub.c.163.com/library/nginx:latest
/home/nginx.conf是本地的配置文件内容如下
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
server {
#侦听的80端口
listen 80;
#不要用localhost,nginx提示冲突
server_name 192.168.2.206;
#本地web服务接口访问
location /rdms-pollution/{
proxy_pass http://192.168.2.95:8080/rdms-pollution/;
}
#访问swagger-editor
location /ui/{
proxy_pass http://192.168.2.206:3001/;
}
}
}
页面访问http://192.168.2.206/ui/即可正确保存cookie
如果有多个rdms-pollution的web接口,不同服务器
我开始的写法是
location /wmf/rdms-pollution/{
proxy_pass http://192.168.2.126:8080/rdms-pollution/;
}
想通过wmf写在前面做区分,这样的结果是虽然能正确访问,但是cookie不能生效,正确的做法是改成/rdms-pollution/wmf/,放在web项目名后面
FAQ
location注意事项
proxy_pass 末尾加上“/”与不加是有区别的
location ^~ /outer/ {
#case A: url最后以/结尾
proxy_pass http://tomcat:8080/
#case B: url最后没有/
#proxy_pass http://tomcat:8080
}
关键在于最后的/,访问localhost/outer/in.html,其中case A会转发到tomcat:8080/in.html, 而case B会转发到tomcat:8080/outer/in.html,所以务必注意了。
匹配模式及顺序
location = /uri =开头表示精确匹配,只有完全匹配上才能生效。
location ^~ /uri ^~ 开头对URL路径进行前缀匹配,并且在正则之前。
location ~ pattern ~开头表示区分大小写的正则匹配。
location ~* pattern ~*开头表示不区分大小写的正则匹配。
location /uri 不带任何修饰符,也表示前缀匹配,但是在正则匹配之后。
location / 通用匹配,任何未匹配到其它location的请求都会匹配到,相当于switch中的default。
什么是跨域以及产生原因
跨域是指a页面想获取b页面资源,如果a、b页面的协议、域名、端口、子域名不同,或是a页面为ip地址,b页面为域名地址,所进行的访问行动都是跨域的,而浏览器为了安全问题一般都限制了跨域访问,也就是不允许跨域请求资源。
nginx操作
验证配置文件是否配置正确nginx -t
[root@node06 home]# docker exec -it nginx nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
nginx重新加载配置文件nginx -s reload
docker exec -it nginx nginx -s reload
此次nginx版本为1.13,操作系统centos7
参考
官网http://nginx.org/en/docs/beginners_guide.html