前言:
最近在搭建vue后台,后端接口是PHP寫的,線上構建好之后,需要請求其他域名下的接口,開發環境已經使用proxytable解決了接口問題,為了開發和生成的代碼一致,
編譯后的代碼,放在nginx下運行,配置了路由重寫。
項目說明:
前端頁面域名 front.me,后端接口backend.me,前端訪問后端接口都是請求front.me/api/controller/action,nginx配置了重定向,當檢測到路由里面/api/是會重定向到 backend.me/controller/action,下面是完整配置,其中重定向配置,我加了說明
server { listen 9929 default_server; server_name _; index index.html index.htm index.php; root /data/web/eyeSkyAdmin/dist; include enable-php-pathinfo.conf; location /nginx_status { stub_status on; access_log off; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } location ~ .*\.(js|css)?$ { expires 12h; } location ~ /.well-known { allow all; } location ~ /\. { deny all; } #這段是指定路由重定向配置start location /api/{ rewrite ^/proxy/html/(.*)$ /$1 break; proxy_pass http://backend.me/; } #這段是指定路由重定向配置end access_log /home/wwwlogs/access.log; }