2014-10-01 133 views
0

呃,一直试图让NGINX/php-fpm与SF 1.4搭配好几天,现在看起来并不能确定正确的配置。我跟着nginx symfony guide以及本SO post,但也无可奈何。我怀疑这可能是因为他们被配置的老版本NGINX的(我有1.6.2工作)NGINX + php-fpm + Symfony 1.4 = :(

这里是我的配置:

server { 

    listen 51000; 

    server_name example.mpurcell.dev.example.com; 
    access_log /tmp/access.log; 
    error_log /tmp/error.log notice; 
    root /home/mpurcell/projects/j1n/app/example/current/code/web/; 

    index index.php; 

    location ~ ^/(app|app_dev)(/|$) { 
     rewrite ^(.*)$ $1.php last; 
    } 

    location ~ ^/(app|app_dev).php(/|$) { 

     try_files $uri =404; 

     include /etc/nginx/fastcgi_params; 

     fastcgi_split_path_info ^(.+?\.php)(/.*)$; 

     if (!-f $document_root$fastcgi_script_name) { 
       return 404; 
     } 

     fastcgi_param SERVICE_ENV 'dev'; 
     fastcgi_param HTTPS off; 

     # http://wiki.nginx.org/Symfony 
     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
     fastcgi_param SCRIPT_NAME $fastcgi_script_name; 
     fastcgi_param PATH_INFO $fastcgi_path_info; 

     fastcgi_pass unix:/var/run/php-fpm.sock; 
    } 
} 

而且各方面的反应:

$ -> curl -v 10.0.0.7:51000 

# Expected 
< HTTP/1.1 301 Moved Permanently 
< Server: nginx/1.6.2 
< Date: Wed, 01 Oct 2014 23:34:10 GMT 
< Content-Type: text/html 
< Transfer-Encoding: chunked 
< Connection: keep-alive 
< Location: /app 

$ -> curl -v 10.0.0.7:51000/app.php 

# Expected 
< HTTP/1.1 200 OK 
< Server: nginx/1.6.2 
< Date: Wed, 01 Oct 2014 23:37:48 GMT 
< Content-Type: text/html; charset=utf-8 
< Transfer-Encoding: chunked 
< Connection: keep-alive 
< Cache-Control: private 

$ -> curl -v 10.0.0.7:51000/app 

# Not expected, the script executes but SF throws a 404 with the following error 
# Empty module and/or action after parsing the URL "/app" (/). 
< HTTP/1.1 404 Not Found 
< Server: nginx/1.6.2 
< Date: Wed, 01 Oct 2014 23:39:09 GMT 
< Content-Type: text/html; charset=utf-8 
< Transfer-Encoding: chunked 
< Connection: keep-alive 
< Cache-Control: private 

,它肯定看起来像从虚拟主机配置重写规则工作:

2014/10/01 23:40:30 [notice] 9668#0: *13 "^(.*)$" matches "/app", client: 10.0.0.3, server: example.mpurcell.dev.example.com, request: "GET /app HTTP/1.1", host: "dev-a-2:51000" 
2014/10/01 23:40:30 [notice] 9668#0: *13 rewritten data: "/app.php", args: "", client: 10.0.0.3, server: example.mpurcell.dev.example.com, request: "GET /app HTTP/1.1", host: "dev-a-2:51000" 

而对于completness起见,该cgi.fix_pathinfo是默认值(= 1),我真的不希望设置这到0.

另外,我应该注意到relative_url_root为应用程序控制器设置为空字符串,因为它位于根网站目录。

堆栈:

nginx 1.6.2 
php-fpm 5.4.33 
php 5.4.33 
+0

检查您的配置是否适合该框架的正确版本:如果您使用的是sf 1.4,则发布sf 2.x的配置(app * .php是sf2.x和前端* .php的后缀.phpbackendé.php是sf1.4的典型代表)。希望这个帮助。 – Matteo 2014-10-02 04:33:41

+0

我发布的链接是SF2,但是在同一个链接中引用了1.4 NGINX配置,我试图使用它,但是也没有工作。对于我的SF1项目,我使用的是应用程序,管理员,api控制器而不是跛脚fe,并且是控制器。 – 2014-10-02 05:51:24

回答

0

所以我终于Symfony和PHP-FPM与对方发挥很好,和一个大的一块拼图是交换拿出nginx的apache。 IMO,nginx> apache的重写语法。因此,这里是我的当前应用程序服务器配置的一个例子:

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

location /admin { 
    rewrite ^/admin/(.*)$ /admin/index.php/$1 last; 
} 

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

location ~ index\.php { 
    ... 
} 

我也必须在Web目录每个控制器创建子迪尔斯,像这样:

/web 
    index.php 
    app.php 
    admin.php 

/web 
    /app/index.php 
    /admin/index.php 

我有这个配置现在推出约2个月0期,所以希望这可以帮助其他老学校的symfonians。

0

我觉得你的问题是,你还没有告诉nginx的地方的Symfony是。我已经把目前使用的nginx配置的一个例子。

server { 
    listen 80; 

    server_name example.com; 
    chunked_transfer_encoding on; 
    proxy_buffering off; 

    charset utf-8; 

    root /home/mpurcell/projects/j1n/app/example/current/code/web; 
    access_log /tmp/access_log; 
    error_log /tmp/error_log; 

    location /sf/ { 
    expires max; 
    root /home/mpurcell/projects/j1n/app/example/current/code/lib/vendor/symfony/data/web/; 
    } 

    location ~ ^/(index|frontend_dev|backend_dev)\.php($|/) { 
    set $script $uri; 
    set $path_info ""; 

    if ($uri ~ "^(.+\.php)(/.*)") { 
     set $script  $1; 
     set $path_info $2; 
    } 

    include fastcgi_params; 
    fastcgi_param PATH_INFO   $path_info; 
    fastcgi_param SCRIPT_FILENAME $document_root$script; 
    fastcgi_param SCRIPT_NAME  $script; 
    fastcgi_param HTTPS off; 
    fastcgi_pass 127.0.0.1:9000; 
    fastcgi_keep_conn on; 
    fastcgi_intercept_errors on; 
} 

location/{ 
    if (-f $request_filename) { 
    expires max; 
    add_header Pragma public; 
    add_header Cache-Control "public, must-revalidate, proxy-revalidate"; 
    break; 
    } 
    if ($request_filename ~ ".(js|htc|ico|gif|jpg|png|css)$") { 
    expires max; 
    add_header Pragma public; 
    add_header Cache-Control "public, must-revalidate, proxy-revalidate"; 
    break; 
    } 
    index index.php; 
    try_files $uri /index.php?$args; 
    } 
} 

对于你的问题,我觉得关键是这样的:

location /sf/ { 
    expires max; 
    root /home/mpurcell/projects/j1n/app/example/current/code/lib/vendor/symfony/data/web/; 
    }