前后端分离中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 。

相关文章:

  • 2021-10-06
  • 2022-12-23
  • 2021-07-11
  • 2022-12-23
  • 2021-09-30
  • 2022-12-23
  • 2022-12-23
  • 2021-05-22
猜你喜欢
  • 2022-12-23
  • 2022-02-15
  • 2021-04-19
  • 2022-12-23
  • 2022-01-24
  • 2022-12-23
  • 2021-09-05
相关资源
相似解决方案