16

我一直在开发的基于 Zend 框架的站点现在正在迁移到其生产服务器。这个服务器原来是 nginx(惊喜!)。由于它是在 Apache 上开发的并且依赖于 htaccess 文件,因此该站点自然无法正常工作。

我的问题是……有人有这方面的经验吗?关于如何将 htaccess 文件的功能转换为 nginx.conf 文件的任何想法?我正在研究这个,但希望有人已经有这方面的经验。谢谢!

编辑:这是当前的 htaccess:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ /index.php [NC,L]
4

8 回答 8

29
server {

 listen   80; ## listen for ipv4
 listen   [::]:80 default ipv6only=on; ## listen for ipv6

 server_name  localhost;

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

 root   /var/www/localhost/public;

 try_files $uri @php_index;

 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
 #
 location @php_index {
  fastcgi_pass   127.0.0.1:9000;
  fastcgi_param  SCRIPT_FILENAME /var/www/localhost/index.php;
  include fastcgi_params;
 }
}

建议尽可能使用 try_files。

于 2010-12-17T12:32:24.173 回答
17

我知道这是一个非常古老的线程,但无论如何它可能会对某些人有所帮助。

基本上它将任何 404 错误重定向到 index.php,但如果文件存在(类型文件),它将设置正确的根。

我是从头顶做的。它可能无法立即工作,您必须放置正确的路径和 fastcgi 配置。我还将所有内容都放回 index.php,因为它应该像 Zend_Framework 那样工作

error_page  404 = /index.php;

location / {
    if (-f $request_filename) {
        root   /var/www;
    }
}

location ~ \.php$ {
        fastcgi_pass   unix:/tmp/php.sock;
        fastcgi_index  index.php;

        fastcgi_param  SCRIPT_FILENAME     /var/www/index.php;
        include /etc/nginx/fastcgi_params;
}
于 2009-01-20T09:38:43.287 回答
13

我不知道转换 htaccess 文件的任何自动/系统方法,您可能必须手动进行。Nginx wiki是 nginx 文档的最佳资源。

编辑: 我现在自己在 Nginx 上运行 Zend Framework,配置如下所示:

server {
  listen 80;
  server_name servername.com;

  root /var/www/zendapp/public;

  location / {
    index index.php;
  }

  # Deny access to sensitive files.
  location ~ (\.inc\.php|\.tpl|\.sql|\.tpl\.php|\.db)$ {
    deny all;
  }
  location ~ \.htaccess {
    deny all;
  }

  # Rewrite rule adapted from zendapp/public/.htaccess
  if (!-e $request_filename) {
    rewrite ^.*$ /index.php last;
  }

  # PHP scripts will be forwarded to fastcgi processess.
  # Remember that the `fastcgi_pass` directive must specify the same
  # port on which `spawn-fcgi` runs.
  location ~ \.php$ {
    include /etc/nginx/fastcgi_params;

    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
  }

  location = /50x.html {
      root   /var/www/default;
  }
}

如您所见,重写规则本身非常简单。

于 2008-12-18T02:37:08.950 回答
8

这是“官方的”,简单且效果很好:

http://wiki.nginx.org/Zend_Framework#Time_for_nginx

于 2012-04-17T12:20:50.267 回答
7

对于可以提供帮助的登台服务器;)

            fastcgi_param APPLICATION_ENV staging;
于 2010-10-26T15:24:10.503 回答
4

实际上我用一个像zend框架一样工作的drupal站点运行一个nginx:一个index.php作为引导程序

这是规则(未在 zend 框架上测试,仅在 drupal 上测试,但应该类似)

location / {
            if (!-e $request_filename) {
                    rewrite  ^/(.*)$  /index.php?q=$1  last;
                    break;
        }
    }

error_page  404              /index.php;
于 2009-03-23T00:35:51.940 回答
2

如果您为您的项目使用一个子目录,例如http://some.url/myproject/controller/,那么您还需要将 setBaseUrl 添加到您的引导文件中。

<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected function _initSomeFancyName()
    {
        $this->bootstrap('frontController');
        $frontController = Zend_Controller_Front::getInstance();
        $frontController->setBaseUrl('/myproject'); // set the base url!
    }
}

nginx 重写看起来像这样:

location /myproject/ {
  if (!-e $request_filename) {
    rewrite ^/myproject/(.*)$ /index.php?$1? last;
  }
}

PS问号不是错字!

于 2009-12-06T21:31:17.713 回答
0

如果可能的话,我建议他们在只能从 Nginx 机器访问的非标准端口上设置 Apache,并让 Nginx 代理到 Apache。

Nginx 代理文档

于 2009-03-23T02:20:24.047 回答