先后端分离Nginx转发

先后端分离中Nginx做为web前端容器,须要访问后端接口一般须要经过路径转发,直接访问后端API会形成跨域问题,配置文件以下html

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

相关文章
相关标签/搜索