【问题标题】:nginx with symfony2 in subdirectory AND normal projectsnginx 与 symfony2 在子目录和正常项目
【发布时间】:2013-12-06 08:20:59
【问题描述】:

我的 nginx 配置有问题。

在大多数页面和谷歌搜索条目上,我发现诸如“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 / 未知!?)

当前配置

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;
    }
}

提前感谢您的每一个提示!

【问题讨论】:

    标签: symfony url-rewriting nginx


    【解决方案1】:

    这个配置应该会有所帮助。

    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;
            }
        }
    }
    

    【讨论】:

    • 2013/12/06 14:30:24 [error] 5755#0: *189 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: ::1, server: localhost, request: "GET /symfony2/web/ HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "localhost" ::1 - - [06/Dec/2013:14:30:24 +0100] "GET /symfony2/web/ HTTP/1.1" 404 47 "-" "Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36" ::1 - - [06/Dec/2013:14:30:34 +0100] "-" 400 0 "-" "-"
    • 我稍微改了一下(/symphony2/app.php 替换为正则表达式),请立即尝试
    • 作品: http://localhost/symfony2/app_dev.php/en/test/index 作品: http://localhost/symfony2/app.php/en/test/index 不作品: http://localhost/symfony2/en/test/index 作品: http://localhost 作品: http://localhost/index.php
    • 不工作:localhost/symfony2/en/test/index - 它应该工作吗?如果是这样,应该用什么脚本来处理它?
    • 这是唯一的错误。项目应该完全重写到 web 目录:localhost/symfony2/en/test/index == /srv/www/symfony2/app.php/en/test/index 一切都是完美的。我可以使用普通项目和symfony2项目^^但是“索引”/默认文件是空的...应该是app.php文件:)谢谢!!!
    猜你喜欢
    • 1970-01-01
    • 2013-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多