更新时间:2023-11-22 18:45:22
在以下 vhost 中,我所做的最重要的更改是:
In the following vhost, the most important changes I made are :
index
指令:它直接在 location 块中处理location/api/
之后添加了一个斜杠,并在同一个 api location 块中删除了不必要的 $uri/
index.php
位置块:您希望所有请求都传递到 Symfony 应用程序中的前端控制器index
directive in server block : it is handled directly in locations blockslocation /api/
and remove unnecessary $uri/
in the same api location block
index.php
location block : you want all requests to be passed to front controller in Symfony appserver {
listen 80 default_server;
access_log /var/log/nginx/my-site.com.log;
error_log /var/log/nginx/my-site.com-error.log error;
charset utf-8;
location /api/ {
root /var/www/my-site.com/backend;
try_files $uri /index.php$is_args$args;
}
location / {
root /var/www/my-site.com/frontend;
try_files $uri /index.html;
}
location ~ ^/index\.php(/|$) {
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
fastcgi_param HTTPS off;
internal;
}
# return 404 for all other php files not matching the front controller
# this prevents access to other php files you don't want to be accessible.
location ~ \.php$ {
return 404;
}
}
最后,我敢打赌你必须将 symfony 公共文件夹添加到 api location block
根指令中.
Last, I bet you'll have to add symfony public folder into api location block
root directive.
这个虚拟主机在我的本地主机上通过以下树进行了良好的测试.
This vhost was tested fine on my localhost with following tree.
api_test
- backend
- index.php
- frontend
- index.html
我可以成功访问
backend/index.php
来自 api_test.dv/apifrontend/index.html
来自 api_test.dv/backend/index.php
from api_test.dv/apifrontend/index.html
from api_test.dv/