前后端分离Nginx转发

前后端分离中Nginx作为web前端容器,需要访问后端接口通常需要通过路径转发,直接访问后端API会造成跨域问题,配置文件如下

server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
         }

         location ^~ /app/ {
          
            proxy_pass  http://localhost:8081/;
         }

其中端口80,访问根路径 http://localhost/ 则为 nginx容器本身内容,如访问  http://localhost/app/ 将会跨域转发至http://localhost:8081/  目录下 ,即访问

http://localhost/app/api/test 实为 http://localhost:8081/api/test 。

posted @ 2020-03-16 08:06  芋头猪爸爸  阅读(960)  评论(0编辑  收藏  举报
我的主页