前後端分離專案,前後端共用一個域名。通過域名後的 url 字首來區別前後端專案。
# demo.conf
server
{
listen 80;
server_name demo.com; # 配置專案域名
index index.html index.htm index.php;
# 預設訪問前端專案
location / {
# 前端打包後的靜態目錄
alias /path/dist/;
#解決頁面重新整理404問題
try_files $uri $uri/ /index.html;
}
# 後端專案
location ~* ^/(api|admin) {
try_files $uri $uri/ /index.php?$query_string;
}
# PHP-INFO-START PHP引用配置,可以註釋或修改,寫法 1 和寫法 2 任意一種都可以
# 1.寶塔寫法 include enable-php-80.conf;
location ~ \.php(.*)$
{
root /path/public/;
try_files $uri =404;
fastcgi_pass unix:/tmp/php-cgi-80.sock;
fastcgi_index index.php;
include fastcgi.conf;
include pathinfo.conf;
}
# 2.一般寫法,使用 nginx 預設提供的配置,加上 `root` 相關配置即可
location ~ \.php(.*)$ {
root /path/public/;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
#PHP-INFO-END
# 前端靜態資源處理
location ^~ /images/ {
alias /path/dist/images/;
}
# 後端靜態資源處理
location ^~ /vendor/ {
alias /path/public/vendor/;
}
location ^~ /storage/ {
alias /path/public/storage/;
}
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結