前言
在做的前后端分离项目(React+Spring)已经完成了一部分功能,打算部署到服务器上测试一下,但是由于浏览器的同源策略导致跨域,在此提供我的解决办法。
前端部署地址:172.79.63.152:8080
后端部署地址:172.79.63.152:9090
首先要在服务器上安装配置好nginx,具体流程可以另行百度。
nginx就绪后,打开nginx文件夹,我这里是安装在/usr/local
文件夹下
[root@dc6-80-283 ~]# cd /usr/local/nginx
[root@dc6-80-283 nginx]# ls
client_body_temp html nginx-1.20.1.tar.gz scgi_temp
conf logs proxy_temp uwsgi_temp
fastcgi_temp nginx-1.20.1 sbin
[root@dc6-80-283 nginx]#
打开conf
文件夹,对nginx.conf
文件进行编辑
[root@dc6-80-283 nginx]# cd conf
[root@dc6-80-283 conf]# vim nginx.conf
找到文件内容的如下部位进行编辑
server {
listen 8080;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /usr/local/build;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location /api/v2 {
proxy_pass http://172.79.63.152:9090;
}
listen 8080
是前端的部署端口号
location /api/v2 { proxy_pass http://172.79.63.152:9090; }
是配置的转发后。端地址,即本项目的后端地址
/api/v2
是前端发送请求的前缀,例如:
前端项目中发送 get(‘/api/v2/login’).then()...
后端的接口地址为http://172.79.63.152:9090/api/v2/login