【发布时间】:2014-11-26 22:42:50
【问题描述】:
呃,几天来一直试图让 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
而且看起来虚拟主机配置中的重写规则确实有效:
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
【问题讨论】:
-
检查您的配置是否适用于正确版本的框架:如果您使用的是 sf 1.4,则发布 sf 2.x 的配置(app*.php 是典型的 sf2.x 和前端* .php backendé.php 是典型的 sf1.4)。希望对您有所帮助。
-
我发布的链接指向 SF2,但在同一个链接中引用了 1.4 NGINX 配置,我尝试使用但也没有工作。对于我的 SF1 项目,我使用的是 app、admin、api 控制器,而不是蹩脚的 fe 和 be 控制器。
标签: nginx rewrite symfony-1.4 php vhosts