What kind of cross-domain solution is used in this scenario?
question: the current project is like this, for example, the local address is 192.168.1.101, the backend address is 192.168.1.105 9006, and then the address of the front-end project is 192.168.1.102virtual 8008. The project uses axios,baseUrl = 192.168.1.105 9006
, so each request is cross-domain. I don"t know how to solve the cross-domain problem.
in fact, there are two most commonly used cross-domain solutions
1. CORS backend settings header
Access-Control-Allow-Origin: *
which domain names are allowed (* all)
Access-Control-Allow-Headers: X-Requested-With
which request headers are allowed
Access-Control-Allow-Methods: PUT,POST,GET,DELETE, OPTIONS
which methods are allowed (non-simple requests will have option)
after background configuration axios
baseUrl = backend server address
(development mode)
baseUrl = online server address
(production mode)
2. Proxy
backend direct requests whose cross-domain is browser restrictions do not limit
so you can browser-> proxy server-> backend-> proxy server-> browser
react
vue
scaffolding webpack-dev-server
its interior is http-proxy-middleware
you can also use node
simple proxy -proxy-middleware
http-proxy
can
or use nginx
after configuring the agent axios >
baseUrl = proxy server address
(development mode)
baseUrl = online server address
(production mode)
1 if a request is always requested from 192.168.1.105virtual 9006, then directly set 192.168.1.105 to allow cross-domain access.
2 if the background 192.168.1.105vir 9006 is only used for development, the entire project ends up running in 192.168.1.102vir 8008. Then set the cross-domain proxy of the front-end project to 192.168.1.105virtual 9006.
data transfer between several servers you can use JWT or jsonp
to achieve cross-domain data transmission. It is recommended to use jwt or jsonp for data transfer between several servers. At the same time, servers should also set cross-domain url addresses. Cross-domain
if it is your own server, it is best to put a reverse proxy in front of it. Anyway, it is an intranet, the speed can keep up, and there is no excessive burden.