前端项目通过nginx服务器发布时,如果存在一些跨域的问题。是可以通过nginx服务器配置代理来解决跨域的。nginx的常见的两种配置代理方式。
创建一个路径代理目标路径(常用)
server {
listen 8000;
server_name localhost;
# 代理配置(后端服务地址:http://192.168.10.231:8000)
location /api {
proxy_pass http://192.168.10.231:8000;
rewrite ^/api/(.*)$ /$1 break;
}
}
请求:/api/xxx
使用目标路径中开头的路径(常用)
server {
listen 8000;
server_name localhost;
# 代理配置(后端服务地址:http://192.168.10.231:8000/apis)
location /apis {
proxy_pass http://192.168.10.231:8000;
}
# 代理配置(后端服务地址:http://192.168.10.232:8000/collection)
location /collection {
proxy_pass http://192.168.10.232:8000;
}
}
请求:/apis/xxx、/collection/xxx