How to make assets work with Symfony3 in subdirect

2019-09-06 14:51发布

问题:

Based on this question How to install symfony2 app in a subdirectory in nginx

I've created symfony3 application that works in subdirectory called bcms4. I've manged to make php work with PHP-FPM but I have probelms with assets. When I want to GET asset it directs the request to app_dev and shows 404 because obviosly the path does not exist.

My question is how to make assets not to be proccesed by app_dev but downloaded as supposed?

So when I enter

test.localhost/s/asdfad -> it runs symfony test.localhost/asdf -> it runs other app living in main dir test.localhost/s/assets/css/test.css -> it will show file in directory /var/www/test.localhost/bcms4/web/assets/css/test.css

My nginx config:

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:

After hours of trying I've managed to figure it out with little hack.

This is what I've added to my config file

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

It'll rewrite files that has prefix /s and extension to directory where they are actually.

Maybe it will help someone. I'll leave question open for a while maybe someone has better solution cause it's seems hacky for me.