【问题标题】:How to get rid of app.php in Symfony2 URIs with Nginx如何使用 Nginx 摆脱 Symfony2 URI 中的 app.php
【发布时间】:2014-11-09 08:15:10
【问题描述】:

我试图清楚地了解我的 Symfony2 的 nginx 配置文件发生了什么,这里是:

    server {
        listen 80;
        autoindex on;
        server_name example.com;
        root /var/www/example.com/web;

        rewrite ^/app\.php/?(.*)$ /$1 permanent;

        location / {
            index app.php;
            try_files $uri $uri/ @symfony;
        }

        location @symfony {
            rewrite ^(.*)$ /app.php/$1 last;
        }

        location ~ ^/app\.php(/|$) {
            include fastcgi_params;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_split_path_info ^(.+\.php)(/.*)$;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param SCRIPT_NAME $fastcgi_script_name;
        }
}

简而言之,我试图在用户可以设置的每个可能的 URI 中删除 app.php,例如example.com/app.php/demo 或只是 example.com/app.php。

此配置实际上适用于上述 URI,但在尝试访问“根”URI example.com 时会导致重定向循环。

如果我从 try_files 中删除 $uri/ 并只留下 $uri 和一个备用 @symfony ,那么一切正常,除了我无法访问任何目录,因为它们将由 SF 处理。

我没有想法,对 nginx 和重写如何实际工作进行了大量研究,但就目前而言,这对我来说是一条死胡同。如果你能找到一个解决方案,在 try_files 中保留 $uri/ 并摆脱前面的循环,请告诉我。

【问题讨论】:

    标签: symfony nginx rewrite


    【解决方案1】:

    Nginx Tips 的这个解决方案对我有用。它和你的差不多,但不完全一样。

    server {
    server_name domain.tld www.domain.tld;
    root /var/www/project/web;
    
    location / {
    # try to serve file directly, fallback to rewrite
    try_files $uri @rewriteapp;
    }
    
    location @rewriteapp {
    # rewrite all to app.php
    rewrite ^(.*)$ /app.php/$1 last;
    }
    
    location ~ ^/(app|app_dev|config).php(/|$) {
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_split_path_info ^(.+.php)(/.*)$;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param HTTPS off;
    }
    
    error_log /var/log/nginx/project_error.log;
    }
    

    【讨论】:

    • 谢谢,但是这个配置的问题是它在 location / {} 块中没有 $uri/,我需要 $uri/ 因为我可能还有一些用户特定的文件夹未知名称,应该可以访问(例如,它们可能包含其他应用程序)。此外,它并没有真正从用户可以明确设置的任何 URI 中删除 app.php ;)
    猜你喜欢
    • 1970-01-01
    • 2019-09-13
    • 2013-07-03
    • 1970-01-01
    • 2016-01-11
    • 1970-01-01
    • 2020-05-09
    • 1970-01-01
    • 2014-01-28
    相关资源
    最近更新 更多