duanpanbo9476
2014-10-01 23:45
浏览 57
已采纳

NGINX + php-fpm + Symfony 1.4 = :(

Ugh, been trying to get NGINX/php-fpm to play nicely with SF 1.4 for a few days now, and can't quite seem to nail down the proper config. I followed the nginx symfony guide as well as this SO post, but neither helped, and I suspect it may be because they were being configured against older versions of NGINX (I am working with 1.6.2).

Here is my config:

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

And the various responses:

$ -> 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

And it sure looks like the rewrite rule from the vhost config is working:

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"

And for the sake of completness, the cgi.fix_pathinfo is default (=1), and I don't really want to set this to 0.

Also, I should note that relative_url_root for the app controller is set to empty string, as it is located in the root web directory.

Stack:

nginx 1.6.2
php-fpm 5.4.33
php 5.4.33
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • doulin2947 2014-12-10 22:44
    已采纳

    So I finally got Symfony and php-fpm to play nicely with each other, and one big piece of that puzzle was to swap out apache for nginx. IMO, the rewrite syntax for nginx > apache. So here is an example of my current app server config:

    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 {
        ...
    }
    

    I did have to create sub dirs for each controller in the web dir, like this:

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

    I've had this config in prod now for about 2 months with 0 issues, so hopefully this helps other old school symfonians as well.

    点赞 打赏 评论
  • duangonglian6028 2014-10-03 07:47

    I think your issue is that you've not told nginx where Symfony is. I've put an example of nginx configuration that I'm using currently that works.

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

    For you problem I think the key is this:

      location /sf/ {
        expires max;
        root /home/mpurcell/projects/j1n/app/example/current/code/lib/vendor/symfony/data/web/;
      }
    
    点赞 打赏 评论
  • dtwkt46424 2019-04-08 19:43

    This is the config I'm using for symfony 1.4

    location ~ \.php($|/) {
            fastcgi_pass php56:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
            include fastcgi_params;
        }
    
    点赞 打赏 评论

相关推荐 更多相似问题