dtz63853 2016-06-21 13:31
浏览 49
已采纳

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

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条回答 默认 最新

  • doukoumi3389 2016-06-22 07:09
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵
  • ¥15 cfx离心泵非稳态计算