在子目录和普通项目中使用symfony2的nginx

时间:2013-12-06 08:20:59

标签: symfony url-rewriting nginx

我的nginx配置有问题。

在大多数网页和Google搜索条目中,我发现了“nginx和symfony2”或“子目录中的nginx和symfony2”之类的内容。 但是我没有找到关于这种配置的一些事情:

  1. 普通的php文件项目(路径如localhost / project - > /srv/www/project/index.php /每个索引统治)
  2. Symfony2项目(路径如localhost / symfony2 - > /srv/www/symfony2/web/app.php(默认)和手动app_dev.php / unknown!?)
  3. 当前配置

    server
    {
        listen   80;
        listen   [::]:80 default_server ipv6only=on;
    
        root /srv/www;
        index index.php index.html index.htm app.php app_dev.php;
        try_files $uri $uri/ index.php @rewriteapp /index.php;
    
        server_name localhost;
    
        location ~ ^\/symfony2
        {
            #rewrite ^\/symfony2(.*)$ /symfony2/web$1 last;
            #root /srv/www/symfony2/web;
            alias /srv/www/symfony2/web;
        }
    
        location @rewriteapp
        {
            rewrite ^(.*)$ /app.php/$1 last;
        }
    
        location ~ \.php(/|$)
        {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
        }
    }
    

    提前感谢每个提示!

1 个答案:

答案 0 :(得分:0)

此配置应有帮助。

server {
    listen   80;
    listen   [::]:80 default_server ipv6only=on;

    root /srv/www;
    index index.php index.html index.htm app.php app_dev.php;

    server_name localhost;

    location / {
        try_files $uri $uri/ /app.php /index.php;
        location ~ .*\.php$ {
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
        }
    }

    location /symfony2 {
        alias /srv/www/symfony2/web;

        location ~ .*\.php(/|$) {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
        }

        location ~ /symfony2(?<path>.*)$ {
            root /srv/www/symfony2/web; 
            try_files $path $path/ /app.php$path;
        }
    }
}