How to make assets work with Symfony3 in subdirectory on Nginx

故事扮演 提交于 2019-12-13 07: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.



来源:https://stackoverflow.com/questions/37945904/how-to-make-assets-work-with-symfony3-in-subdirectory-on-nginx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!