php - NginX 友好的 PHP 框架

标签 php nginx fastcgi

我正在寻找一个 PHP 框架,如果幸运的话,它可以在 FastCGI 下的 nginx 中运行,否则,不需要太多调整。

最佳答案

带有 nginx 的 Symfony 1.4 非常棒。我已经完成了调整,这是我的生产配置的概括,我可以保证它适合生产使用。

server {
  listen 80;

  server_name mysite.com;

  root /var/www/mysite.com/web;
  access_log /var/log/nginx/mysite.com.access.log;
  error_log /var/log/nginx/mysite.com.error.log;

  location ~ ^/(index|frontend|frontend_dev|backend|backend_dev)\.php$ {
    include fastcgi_params;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
    fastcgi_param HTTPS off;
    fastcgi_pass   127.0.0.1:9000;
  }

  location / {
    index index.php;
    try_files $uri /index.php?$args;
  }
}

server {
  listen 443;

  ssl on;
  ssl_certificate      /etc/ssl/certs/mysite.com.crt;
  ssl_certificate_key  /etc/ssl/private/mysite.com.key;

  server_name mysite.com;

  root /var/www/mysite.com/web;
  access_log /var/log/nginx/mysite.com.access.log;
  error_log /var/log/nginx/mysite.com.error.log;
  location ~ ^/(index|frontend|frontend_dev|backend|backend_dev)\.php$ {
    include fastcgi_params;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
    fastcgi_param HTTPS on;
    fastcgi_pass   127.0.0.1:9000;
  }

  location / {
    index index.php;
    try_files $uri /index.php?$args;
  }
}

PHP 5.4 注意事项

dotdeb 附带的 php5-fpm 5.4 现在默认使用套接字而不是环回。如果您使用的是 PHP 5.4,并且在上述配置中遇到错误的网关错误,请尝试将 127.0.0.1:9000 的所有实例替换为 unix:/var/run/php5-fpm .sock.

php-fpm 5.4 还新限制了可以被解析为 PHP 的文件扩展名到 security.limit_extensions 中指定的文件扩展名。如果您修改了位置正则表达式以包含 .php 以外的其他文件扩展名,这可能会引起您的兴趣。以下安全说明仍然适用。

安全说明

此配置仅使用 PHP 解析文件 index.php、frontend.php、frontend_dev.php、backend.php 和 backend_dev.php。

一般使用 php 和 nginx,而不仅仅是使用 symfony,使用

location \.php$ {
  ...
}

导致与使用路径信息的 URL 相关的安全漏洞,例如:/index.php/foo/bar。

常见的解决方法是在 php.ini 中设置 fix_pathinfo=0。这会破坏 pathinfo URL,而 symfony 依赖于它们。这里使用的解决方案是显式指定解析为 php 的文件。

有关详细信息,请参阅 nginx+php-cgi security alert

平台

这在使用 dotdeb for nginx 和 php-fpm 包的 Debian Squeeze 系统以及 Ubuntu 10.04 Lucid Lynx 上有效且安全> 使用 ppa/brianmercer for php-fpm 的系统。它可能会或可能不会工作,并且在其他系统上是安全的。

使用说明

要添加另一个 PHP 文件 additionalfile.php 进行解析,请在两个位置 block 中使用此语法:

位置 ~ ^(index|frontend|frontend_dev|backend|backend_dev|additionalfile).php$ { ...

编辑: Symfony 2.0 发布了!这是配置,改编自上面的 1.4 配置:

server {
  listen 80;

  server_name symfony2;
  root /var/www/symfony2/web;

  error_log /var/log/nginx/symfony2.error.log;
  access_log /var/log/nginx/symfony2.access.log;

  location / {
    index app.php;
    if (-f $request_filename) {
      break;
    }
    rewrite ^(.*)$ /app.php last;
  }

  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  #
  location ~ (app|app_dev).php {
    include fastcgi_params;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
    fastcgi_param HTTPS off;
    fastcgi_pass   127.0.0.1:9000;
  }
}

server {
  listen 443;

  server_name symfony2;
  root /var/www/symfony2/web;

  ssl on;
  ssl_certificate      /etc/ssl/certs/symfony2.crt;
  ssl_certificate_key  /etc/ssl/private/symfony2.key;

  error_log /var/log/nginx/symfony2.error.log;
  access_log /var/log/nginx/symfony2.access.log;

  location / {
    index app.php;
    if (-f $request_filename) {
      break;
    }
    rewrite ^(.*)$ /app.php last;
  }

  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  #
  location ~ (app|app_dev).php {
    include fastcgi_params;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
    fastcgi_param HTTPS off;
    fastcgi_pass   127.0.0.1:9000;
  }
}

关于php - NginX 友好的 PHP 框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3998856/

相关文章:

python - 相对路径没有这样的文件或目录

javascript - 应该得到 3 张图像的网格

php - 使用xpath获取文本内容

php - 如果另一个值是空的/不存在,如何让 SQL 查询显示一个值

nginx - 如何在 NGINX 配置中的两个位置使用相同的规则?

docker - Nginx和Ingress与Kubernetes无法路由我的请求

c++ - 对 FCGI 概念感到困惑

php - 如何在 PHP 中使用 FDPF 为文本添加下划线?

nginx server_name 通配符或包罗万象

python - 如何运行 nginx + python(没有 django)