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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM