前后端分离Nginx转发

Posted 芋头猪爸爸

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了前后端分离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 。

以上是关于前后端分离Nginx转发的主要内容,如果未能解决你的问题,请参考以下文章