如何使资产在Nginx上的子目录中与Symfony3一起使用

时间:2016-06-21 13:31:34

标签: php nginx symfony nginx-location

基于此问题How to install symfony2 app in a subdirectory in nginx

我创建了在名为bcms4的子目录中工作的symfony3应用程序。我已经设法让PHP与PHP-FPM一起工作,但我有资产问题。当我想要GET资产时,它会将请求定向到app_dev并显示404,因为显然路径不存在。

我的问题是如何让资产不被app_dev处理,而是按照假设下载?

所以当我进入

test.localhost / s / asdfad - >它运行symfony test.localhost / asdf - >它运行在主dir的其他应用程序 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$1 /web$1 @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$1;     
}


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;
}

}

1 个答案:

答案 0 :(得分:0)

经过几个小时的尝试,我已经成功地解决了这个问题。

这是我添加到配置文件中的内容

location ~ ^/s(/.*).\w{1,5}$ {
    rewrite ^/s(/.*) /bcms4/web$1  break;
    return 404;
}

它会将具有前缀/ s和扩展名的文件重写到实际的目录。

也许它会帮助某人。我会暂时提出问题,或许某人有更好的解决办法,因为这对我来说似乎很苛刻。