nginx解决前后端分离项目部署到云服务器时的跨域问题

前言

在做的前后端分离项目(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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值