After the geoserver release service is completed, a very important application scenario is the front-end service call to display the service data, then it is likely to encounter a cross-domain problem, today we will share a variety of solutions across the problem to apply to different needs of the business Scenes.
1. nginx service balancing strategy
If your project is using nginx, it is very simple to add a routing configuration in the conf/nginx.conf file
1. Add routing
location /geoserver{
proxy_pass http://127.0.0.1:8080/geoserver;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
fastcgi_buffer_size 4k;
fastcgi_buffers 8 4k;
#proxy_pass http://mape.shanghai-map.net/arcgis/;
#index index.html index.htm;
# 指定允许跨域的方法,*代表所有
add_header Access-Control-Allow-Methods *;
# 预检命令的缓存,如果不缓存每次会发送两次请求
add_header Access-Control-Max-Age 3600;
# 带cookie请求需要加上这个字段,并设置为true
add_header Access-Control-Allow-Credentials true;
# 表示允许这个域跨域调用(客户端发送请求的域名和端口)
# $http_origin动态获取请求客户端请求的域 不用*的原因是带cookie的请求不支持*号
add_header Access-Control-Allow-Origin $http_origin;
# 表示请求头的字段 动态获取
add_header Access-Control-Allow-Headers
$http_access_control_request_headers;
}
There are configurations about spanning in the configuration, you can take a closer look.
2. Restart the nginx service, and the front end can call
Two, configure tomcat
If the geoserver service is deployed in the form of a war package, then the tomcat container is a good choice.
1. Configure web.xml
Enter tomcat's conf/web.xml
<filter>
<filter-name>CorsFilter</filter-name>
<filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
<init-param>
<param-name>cors.allowed.origins</param-name>
<param-value>*</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CorsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3. Modify the configuration geoserver service
1. Remove the corresponding comment
Remove the corresponding CORS comment in the web.xml file under geoserver
apache-tomcat-7.0.72\webapps\geoserver\WEB-INF\web.xml path
Remove this comment.
2. Import the jar package
In the geoserver installation directory, open the lib folder:
apache-tomcat-7.0.72\webapps\geoserver\WEB-INF\lib
Put the following three jars:
Then restart the tomcat service and it will be OK.
4. Front-end configuration
I am not very professional in front-end configuration spanning, you can refer to the following blogs:
Front-end processing spanning
Cross Problem Solving References
At this point, the sharing of multiple solutions to geoserver cross-domain problems is completed. In the next article, we will share multiple styles of layer data configuration methods, so stay tuned!