我正在尝试在mydomain/后端下为我的PHP项目提供服务,因为我想为Node/Vue项目保留我的基本网址。我将问题归结为这样一个事实,即由于try+文件和~.php块之间的交互,我无法在/后端下运行PHP应用程序。
关于如何实现这一点的任何建议?如果我简单地删除/后端并通过我的根位置访问应用程序,一切正常/
server {
listen 80;
listen [::]:80;
root /var/www/backend/public;
index index.php index.html index.htm;
server_name your-domain.com;
#location ^~ /backend/ {
location /backend {
# This is the folder that index.php is in
try_files $uri $uri/ /index.php?_url=$uri&$args;
}
location ~ [^/].php(/|$) {
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
fastcgi_index /index.php;
include fastcgi_params;
fastcgi_split_path_info ^(.+?.php)(/.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
奇怪的是,PHP 项目的相对路由实际上是在导航到/后端时触发的,但缺少项目的实际内容。我希望项目就像项目的根目录是/domain/后端一样,项目中的所有 URL 都以/backend 为前缀
我也试图在 ~.php 块中添加/后端,但无济于事。
更新:
感谢您的回复。我已经更接近解决方案,但还没有完全到位。
按照您的提示和以下链接,我学到了以下内容: 如何在nginx中正确配置别名指令?
- 如果在某个位置内使用别名,则不应使用try_files 由于长期存在的错误而阻止
- 应使用 fastcgi_param SCRIPT_FILENAME $request_文件名 而不是fastcgi_param SCRIPT_FILENAME $document_根$fastcgi_script_name;如果使用别名
- 如果你执行第 2 点,你应该使用 index index.php;而不是 fastcgi_index/索引.php;
但是,使用 update.conf 文件,我的 PHP 应用程序仍然重新路由到原始 URL。
重写行有什么问题?为什么/后端仍然路由回去,就好像项目在/中一样?它是由PHP应用程序获取的,因为Node应用程序为所有其他随机不存在的子URI提供404。
做台词
fastcgi_split_path_info ^(.+?.php((/.*($;
和
fastcgi_param PATH_INFO $fastcgi_路径_信息;
由于将它们嵌套在别名块中而受到任何副作用?
如何正确try_files $uri $uri//index.php?_url=$uri&$args; 在别名块中工作?
server {
listen 80;
listen [::]:80;
server_name your-domain.com;
location ^~ /backend {
alias /var/www/back/public;
index index.php index.html index.htm;
if (!-e $request_filename) { rewrite ^ /backend/index.php?_url=$uri&$args last; }
location ~ .php$ {
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
index index.php;
include fastcgi_params;
fastcgi_split_path_info ^(.+?.php)(/.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
location / {
# node/Vue project
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
尝试先设置根位置,然后使用PHP项目的别名设置位置/后端。
像这样:
root /var/www;
location / {
# Directives for your Vue project
}
location /backend {
alias /var/www/backend/public;
try_files $uri $uri/ /index.php?_url=$uri&$args;
}