php nginx 上的 Zend 框架

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/376732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:32:11  来源:igfitidea点击:

Zend Framework on nginx

phpzend-frameworkmod-rewritenginx

提问by rg88

The Zend Framework based site I have been working on is now being migrated to its production server. This server turns out to be nginx (surprise!). Naturally the site does not work correctly as it was developed on Apache and relies on an htaccess file.

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

My question is... anyone have any experience with this? Any ideas on how to translate what the htaccess file does to an nginx.conf file? I'm researching this but am hoping someone already has experience with this. Thanks!

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

EDIT: This is the current htaccess:

编辑:这是当前的 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]

回答by another one

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

It's recommended to use try_files when ever possible.

建议尽可能使用 try_files。

回答by stunti

I know it's a pretty old thread but it might help some people anyway.

我知道这是一个很旧的线程,但无论如何它可能会帮助一些人。

Basically it redirects any 404 error to index.php, but if the file exists (type file) it will set the right root.

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

I did it from the top of my head. It might not be working right away, and you have to put the right path and fastcgi config. I also put everything back to index.php as it should work like that with Zend_Framework

我是从头顶做的。它可能无法立即工作,您必须放置正确的路径和 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;
}

回答by mooware

I don't know of any automatic/systematic way to convert the htaccess-file, you'll probably have to do it manually. The Nginx wikiis the best resource for nginx documentation.

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

Edit:I'm running Zend Framework on Nginx myself now and the config looks like this:

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

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

As you can see, the rewrite rule itself is very simple.

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

回答by ?ukasz Muchlado

This is "official", simple and works nice:

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

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

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

回答by nerkn

For staging server that could help ;)

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

            fastcgi_param APPLICATION_ENV staging;

回答by Hymantrade

Actually i run a nginx with a drupal site that work like zend framework: one index.php as bootstrap

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

this is the rule (not tested on zend framework, just on drupal, but should be similar)

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

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

error_page  404              /index.php;

回答by dz0ny

If you use a subdirectory for your project like http://some.url/myproject/controller/, then you also need to add setBaseUrl to your bootstrap file.

如果您为您的项目使用子目录,例如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!
    }
}

The nginx rewrite would look like this:

nginx 重写将如下所示:

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

PS The question mark is not typo!

PS问号不是错字!

回答by Jordan S. Jones

If it were at all possible, I would recommend that they setup Apache on a nonstandard port accessible only from the Nginx box, and have Nginx proxy to Apache.

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

Nginx Proxy documentation

Nginx 代理文档