首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >NGINX + php-fpm + Symfony 1.4 = :(

NGINX + php-fpm + Symfony 1.4 = :(
EN

Stack Overflow用户
提问于 2014-10-02 07:45:30
回答 3查看 3K关注 0票数 0

呃,我已经尝试了几天让NGINX/php-fpm能够很好地使用SF 1.4了,但是似乎不能确定正确的配置。我跟踪了nginx symfony guide和这个SO post,但都没有帮助,我怀疑这可能是因为它们是针对旧版本的NGINX进行配置的(我正在使用1.6.2)。

下面是我的配置:

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

而且看起来vhost配置中的重写规则确实有效:

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"

为了完整起见,cgi.fix_pathinfo是默认值(=1),我并不想将它设置为0。

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

堆栈:

nginx 1.6.2
php-fpm 5.4.33
php 5.4.33
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-12-11 06:44:13

所以我最终让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

我在prod中使用这个配置已经两个月了,没有任何问题,所以希望这也能帮助到其他老派的symfonians。

票数 0
EN

Stack Overflow用户

发布于 2014-10-03 15:47:09

我想你的问题是你没有告诉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/;
  }
票数 0
EN

Stack Overflow用户

发布于 2019-04-09 03:43:53

这是我在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;
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26152424

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档