#二、满足所有访问不跨域
##Nginx相关配置
http {
upstream tomcatServer{
server 172.16.100.130:8082;
server 172.16.100.136:8082;
server 172.16.100.137:8082;
}
server{
listen 8080;
add_header backendIP $upstream_addr;
add_header backendCode $upstream_status;
location / {
if ($request_method = 'OPTIONS') {
#########解决跨域问题##########################
add_header Access-Control-Allow-Origin $http_origin;
add_header Access-Control-Allow-Credentials true;
add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
add_header Access-Control-Allow-Headers X-Requested-With,access_token,x-session,Accept,Origin,Content-Type,Last-Modified,x-access-token,withcredentials;
return 204;
}
if ($request_method = 'GET') {
add_header Access-Control-Allow-Origin $http_origin;
add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
add_header Access-Control-Allow-Headers X-Requested-With,access_token,x-session,Accept,Origin,Content-Type,Last-Modified,x-access-token,withcredentials;
}
proxy_pass http://tomcatServer;
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_connect_timeout 180;
proxy_send_timeout 180;
proxy_read_timeout 180;
proxy_buffer_size 128k;
proxy_buffers 4 128k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
}
}
##Tomcat相关配置:
census/WEB-INF/web.xml
<init-param>
<param-name>cors.allowOrigin</param-name>
<!-- 设置需要跨域的地址 -->
<param-value>*</param-value>
</init-param>
<init-param>
<param-name>cors.supportedMethods</param-name>
<param-value>GET, POST,OPTIONS</param-value>
</init-param>
<init-param>
<param-name>cors.supportedHeaders</param-name>
<param-value>Accept, Origin, X-Requested-With, Content-Type, Last-Modified,x-access-token,access_token</param-value>
</init-param>
<init-param>
<param-name>cors.supportsCredentials</param-name>
<param-value>true</param-value>
</init-param>