Nginx 将目录重写为 Symfony App 但删除主机目录

Nginx 将目录重写为 Symfony App 但删除主机目录

我有一台虚拟机,上面运行着几个开发应用程序。基本 Web 目录如下:

/var/web/subdoms/:
    index.php
    MyApp/
    SomeOtherApp/

我的 Nginx 配置文件将根指定为 /var/web/subdoms,例如:

http://myserver/  -> /var/web/subdoms/index.php.

在 MyApp 子目录中,是一个 Symfony2 项目。在 SomeOtherApp 目录中,是一个通用的 perl-cgi 应用程序

http://myserver/SomeOtherApp  -> /var/web/subdoms/SomeOtherApp/index.pl

对 MyApp 的请求需要重定向到子目录,以便:

http://myserver/MyApp/    ->  /var/web/subdoms/MyApp/symfony/web/

从那里开始,需要重写请求,以便将请求传递到“app.php”ala:

http://myserver/MyApp/          -> /var/web/subdoms/MyApp/symfony/web/app.php
http://myserver/MyApp/hello/you -> /var/web/subdoms/MyApp/symfony/web/app.php/hello/you

当我设置根目录:/var/web/subdoms/MyApp/symfony/web/ 并按照 symfony 文档中的示例进行请求而不使用“MyApp”时,我可以实现此目的。但是,当我尝试将应用程序嵌套在一个目录中时(以便我的服务器可以在同一个域上为多个应用程序提供服务,而不是由 symfony 应用程序控制),“MyApp”部分会添加到 URL ala:

http://myserver.com/MyApp/hello/you  -> /var/web/subdoms/MyApp/symfony/web/app.php/MyApp/hello/you

然后 symfony 抱怨说它找不到路由“MyApp/hello/you”。我可以通过在所有 symfony 路由前面添加“MyApp”来解决这个问题,但这感觉很笨拙。我宁愿通过 Nginx 找到一种方法将 url sans 目录转发到 Symfony。

如果有帮助的话,这是我当前的 nginx 配置(已删除其他应用程序):

server {
    listen          80;
    server_name myserver;

    root /var/web/subdoms;
    error_log /var/log/nginx/rr_error.log;
    access_log /var/log/nginx/rr_access.log;

    location /MyApp/ {
        rewrite ^/MyApp/(.*)$ /MyApp/symfony/web/app_dev.php/$1 last;
    }

    #was the way to redirect requests to app_dev.php when MyApp/Symfony/web was root
    location @rewriteRR {
        rewrite ^(.*)$ /app_dev.php/$1 last;
    }

    location ~ \.php(/|$) {
        include fastcgi_params;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
        fastcgi_param HTTPS on;
        fastcgi_pass 127.0.0.1:9000;
    }

    location ~ /\.ht {
        deny all;
    }

}

最后,我意识到最好使用子域来分隔应用程序(或多个域),但如前所述,这是一个开发服务器。将其拆分到子域、不同主机等中成本太高。出于成本原因(SSL 证书/托管费),我需要将其全部保留在同一个主机/域上,同时避免自我签名证书(因为在开发过程中,其他人会在非正式环境中使用这些应用程序,而自我签名的错误会让他们恼火)。

相关内容