Drupal 7网站是否有通用的nginx conf?


15

我看过Perusio的drupal-with-nginx存储库,虽然它的扩展范围令人印象深刻,但目前对我来说可能有点太先进了,此外,我在服务器上还有几个基于Symfony2的站点,并且在完全理解配置之前,我不会开始进行重大更改。

因此,我在博客上找到了它,并认为它可以完成此工作。在nginx上提供drupal 7是否有任何常见的陷阱?此外,如果同一Drupal安装要为多个站点供电,那么配置会有所不同吗?

server {
    server_name example.org;
    root /home/me/sites/example.org;

    index index.html index.htm index.php;

    access_log /var/log/nginx/example.org.access.log;
    error_log /var/log/nginx/example.org.error.log;

    location = /favicon.ico {
            log_not_found off;
            access_log off;
    }

    location = /robots.txt {
            allow all;
            log_not_found off;
            access_log off;
    }

    # For drush
    location = /backup {
            deny all;
    }

    # Prevent user from accessing settings.php directly
    location ~ ^/sites/[^/]+/settings.php$ {
            deny all;
    }

    ## Replicate the Apache <FilesMatch> directive of Drupal standard
    ## .htaccess. Disable access to any code files. Return a 404 to curtail
    ## information disclosure. Hide also the text files.
    location ~* ^(?:.+\.(?:htaccess|make|txt|log|engine|inc|info|install|module|profile|po|sh|.*sql|theme|tpl(?:\.php)?|xtmpl)|code-style\.pl|/Entries.*|/Repository|/Root|/Tag|/Template)$ {
            return 404;
    }

    location ~ \..*/.*\.php$ {
            return 403;
    }

    location / {
            # This is cool because no php is touched for static content
            try_files $uri @rewrite;
    }

    location @rewrite {
            # Some modules enforce no slash (/) at the end of the URL
            # Else this rewrite block wouldn't be needed (GlobalRedirect)
            #rewrite ^/(.*)$ /index.php?q=$1&$args;
            rewrite ^ /index.php last;
    }

    # Use an SSH tunnel to access those pages. They shouldn't be visible to
    # external peeping eyes.
    location = /install.php {
            allow 127.0.0.1;
            deny all;
    }

    location = /update.php {
            allow 127.0.0.1;
            deny all;
    }

    location ~ \.php$ {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_intercept_errors on;
            fastcgi_pass unix:/var/run/php5-cgi/php5.sock;
    }

    ## Drupal 7 generated image handling, i.e., imagecache in core. See:
    ## https://drupal.org/node/371374
    location ~* /sites/.*/files/styles/ {
            access_log off;
            expires 30d;
            try_files $uri @rewrite;
    }

    # Fighting with ImageCache? This little gem is amazing.
    location ~ ^/sites/.*/files/imagecache/ {
            try_files $uri @rewrite;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
            expires max;
            log_not_found off;
    }
}

1
没有我知道的陷阱。该nginx配置已经在离散地对待每个/ sites / * /多站点目录...
tenken

@tenken不错。我一定会尝试的。我在网上发现的大多数配置都是假设未安装nginx或尚未配置任何站点,这就是为什么我有些谨慎的原因。谢谢
Adam-E

Answers:


7

Drupal 7与Nginx的主要问题在于Drupal是为Apache设计的,因此许多模块都假定已安装Apache(并且您的“状态报告”中始终会有一个蓝色的小提示,告诉您不能使用上载进度,因为未安装mod_php-很烦)。

话虽这么说,感谢perusio和其他人,已经创建了许多模块,这些模块更多地处理了nginx并充分利用了其功能。到目前为止,我还没有遇到过Apache可以解决的nginx问题,并且nginx更快,占用空间更小。许多基准测试都表明了这一点,但这也是我的经验。它还具有与php5-fpm更好的集成,其性能也优于mod_php。

随着Drupal的发展,它变得越来越与后端无关。您可以通过7的数据库抽象层看到这一点,该层允许更多的数据库后端,因此我认为将来的发行版将考虑其他Web服务器。

因此,我完全没有遇到任何陷阱。您只需要更多地注意某些模块的功能,或者至少要注意它们说的功能。如果他们提到.htaccess文件,请确保您在nginx文件中具有执行相同操作的相应条目。实际上,我还没有看到nginx使用正确配置失败的情况。

Perusio的nginx配置绝对是惊人的,但要花很长时间才能完全理解它。您将需要为自己进行自定义,如果对图像缓存或advagg等使用非标准设置,则可能会遇到一些需要修复的问题。它还假定您正在使用多个php-fpm池。因此,您需要仔细检查并取出不需要的内容。但是花点时间完成所有操作是值得的,因为您将学到很多有关nginx如何工作的知识。

我的nginx / drupal网站也遇到了一些错误,因为我倾向于使用php-fpm 5.4或5.5。该错误与nginx无关,但与Drupal函数本身相关,因为Drupal确实完成了对要求php 5.3的过渡。但是,如果您查看问题队列,则会发现一些修补程序和其他解决方案,以修复可与新版本php一起使用的模块。

最后,我建议任何使用新服务器启动的人都使用nginx而不是Apache。好多了。


4

我读过Nginx不能做所有的事情,与Apache相比,它的功能是有限的。“ Apache为每个任务提供了一个模块”。以我的短暂经验,我已经在Drupal中使用Nginx几个月了,并且一切正常。如果您正在为Drupal和Nginx使用多站点安装,则可以在同一服务器配置上设置多个服务器名称,但是每个站点将无法使用不同的日志。我使用此配置没有(几乎)没有任何问题:https : //www.nginx.com/resources/wiki/start/topics/recipes/drupal/


4
Apache就像Microsoft Word,它有上百万个选项,但您只需要六个。Nginx可以完成这六件事,并且其中五件事要比Apache快50倍。— nginx和Wordpress上的Chris Lea
SGhosh

2

我完全同意您的看法,Perusio的Drupal的nginx配置令人印象深刻,但对于nginx的本地实例而言可能是过大的。我发现GitHub上Mulkave的nginx配置文件是在nginx上运行Drupal 7的最实用,轻便的配置。


0
server {

    listen *:80;

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

    root /srv/test;
    index index.html index.htm index.php;

    # Enable compression, this will help if you have for instance advagg‎ module
    # by serving Gzip versions of the files.
    gzip_static on;

    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

    # This matters if you use drush prior to 5.x
    # After 5.x backups are stored outside the Drupal install.
    #location = /backup {
    #        deny all;
    #}

    # Very rarely should these ever be accessed outside of your lan
    location ~* \.(txt|log)$ {
        allow 192.168.0.0/16;
        deny all;
    }

    location ~ \..*/.*\.php$ {
        return 403;
    }

    # No no for private
    location ~ ^/sites/.*/private/ {
        return 403;
    }

    # Block access to "hidden" files and directories whose names begin with a
    # period. This includes directories used by version control systems such
    # as Subversion or Git to store control files.
    location ~ (^|/)\. {
        return 403;
    }

    location / {
        # This is cool because no php is touched for static content
        try_files $uri @rewrite;
    }

    location @rewrite {
        # You have 2 options here
        # For D7 and above:
        # Clean URLs are handled in drupal_environment_initialize().
        rewrite ^ /index.php last;
        # For Drupal 6 and bwlow:
        # Some modules enforce no slash (/) at the end of the URL
        # Else this rewrite block wouldn't be needed (GlobalRedirect)
        #rewrite ^/(.*)$ /index.php?q=$1;
    }

    # Fighting with Styles? This little gem is amazing.
    # This is for D6
    #location ~ ^/sites/.*/files/imagecache/ {
    # This is for D7 and D8
    location ~* files/styles {
        access_log off;
        expires 30d;
        try_files $uri @rewrite;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires max;
        log_not_found off;
    }

    location ~ [^/]\.php(/|$) {
        fastcgi_index index.php;
        include fcgi.conf;
        fastcgi_pass unix:/var/run/ajenti-v-php-fcgi-test-php-fcgi-0.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.