如何使资产在 Nginx 的子目录中与 Symfony3 一起工作
How to make assets work with Symfony3 in subdirectory on Nginx
基于这个问题How to install symfony2 app in a subdirectory in nginx
我创建了在名为 bcms4 的子目录中运行的 symfony3 应用程序。我设法使 php 与 PHP-FPM 一起工作,但我对资产有疑问。当我想获取资产时,它将请求定向到 app_dev 并显示 404,因为显然该路径不存在。
我的问题是如何使资产不被 app_dev 处理但按预期下载?
所以当我输入
test.localhost/s/asdfad -> 它运行 symfony
test.localhost/asdf -> 它运行主目录中的其他应用程序
test.localhost/s/assets/css/test.css -> 它将显示目录 /var/www/test.localhost/bcms4/web/assets/css/test.css
中的文件
我的 nginx 配置:
server {
listen 80;
root /var/www/test.localhost;
index index.html index.htm index.php;
# Make site accessible from http://localhost/
server_name test.localhost;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
location ~ ^/s(/.*)$ {
try_files /s/web /web @sf2dev =404;
}
location @sf2dev {
expires off;
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/www/test.localhost/bcms4/web/app_dev.php;
fastcgi_param SCRIPT_NAME /s/app_dev.php;
fastcgi_param REQUEST_URI /s;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
# With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
fastcgi_intercept_errors on;
}
}
经过几个小时的尝试,我设法通过一些小技巧解决了这个问题。
这是我添加到配置文件中的内容
location ~ ^/s(/.*).\w{1,5}$ {
rewrite ^/s(/.*) /bcms4/web break;
return 404;
}
它将重写具有前缀 /s 和扩展名的文件到它们实际所在的目录。
也许它会对某人有所帮助。我会暂时保留问题,也许有人有更好的解决方案,因为它对我来说似乎很老套。
基于这个问题How to install symfony2 app in a subdirectory in nginx
我创建了在名为 bcms4 的子目录中运行的 symfony3 应用程序。我设法使 php 与 PHP-FPM 一起工作,但我对资产有疑问。当我想获取资产时,它将请求定向到 app_dev 并显示 404,因为显然该路径不存在。
我的问题是如何使资产不被 app_dev 处理但按预期下载?
所以当我输入
test.localhost/s/asdfad -> 它运行 symfony test.localhost/asdf -> 它运行主目录中的其他应用程序 test.localhost/s/assets/css/test.css -> 它将显示目录 /var/www/test.localhost/bcms4/web/assets/css/test.css
中的文件我的 nginx 配置:
server {
listen 80;
root /var/www/test.localhost;
index index.html index.htm index.php;
# Make site accessible from http://localhost/
server_name test.localhost;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
location ~ ^/s(/.*)$ {
try_files /s/web /web @sf2dev =404;
}
location @sf2dev {
expires off;
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/www/test.localhost/bcms4/web/app_dev.php;
fastcgi_param SCRIPT_NAME /s/app_dev.php;
fastcgi_param REQUEST_URI /s;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
# With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
fastcgi_intercept_errors on;
}
}
经过几个小时的尝试,我设法通过一些小技巧解决了这个问题。
这是我添加到配置文件中的内容
location ~ ^/s(/.*).\w{1,5}$ {
rewrite ^/s(/.*) /bcms4/web break;
return 404;
}
它将重写具有前缀 /s 和扩展名的文件到它们实际所在的目录。
也许它会对某人有所帮助。我会暂时保留问题,也许有人有更好的解决方案,因为它对我来说似乎很老套。