首页 文章

适用于Drupal&Boost和ImageCache的nginx配置

提问于
浏览
0

UPD!!! :此主题完全解决了此处讨论的问题:

http://groups.drupal.org/node/155564

并通过详细探索:

由Nginx和Drupal大师组成的

Nginx配置 - AntónioP . P. Almeida(Perusio) .

https://github.com/perusio/drupal-with-nginx


无法解决以下问题:我有FreeBSD,Apache 2.2,PHP(没有FastCGI!)作为apache模块,nginx 0.8.5.4 .

我正在尝试将启用了boost和image_cache的Drupal门户移动到个人VPS服务器上 .

我的目标是在nginx中重写clean_url并更正boost&image_cache规则 .

请帮忙!我知道我当前的nginx配置有些问题 . 整整一天都被切断了 .

这是nginx.conf(现在只有/ route工作):

user www www; 
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx-access.log  main;

    reset_timedout_connection on;
    sendfile        on;
    aio sendfile;
    tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    gzip  on;

    upstream backend {
        # Apache server
        server 77.72.19.19:81;
    }

    server {
        listen       77.72.19.19:80 default accept_filter=httpready;
        server_name  77.72.19.19;

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;

        gzip  on;
        gzip_static on;
        gzip_proxied any;

        gzip_types text/plain text/html text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

        set $myroot /usr/local/www/apache22/data/alfa;
        root $myroot;

        location ~ ^\. {
            deny all;
        }

        set $boost "";
        set $boost_query "_";

        if ( $request_method = GET ) {
            set $boost G;
        }

        if ($http_cookie !~ "DRUPAL_UID") {
            set $boost "${boost}D";
        }

        if ($query_string = "") {
            set $boost "${boost}Q";
        }

        if ( -f $myroot/cache/normal/$http_host$request_uri$boost_query$query_string.html ) {
            set $boost "${boost}F";
        }

        if ($boost = GDQF){
            rewrite ^.*$ /cache/normal/$http_host/$request_uri$boost_query$query_string.html break;
        }

        if ( -f $myroot/cache/perm/$http_host$request_uri$boost_query$query_string.css ) {
            set $boost "${boost}F";
        }

        if ($boost = GDQF){
            rewrite ^.*$ /cache/perm/$http_host/$request_uri$boost_query$query_string.css break;
        }

        if ( -f $myroot/cache/perm/$http_host$request_uri$boost_query$query_string.js ) {
            set $boost "${boost}F";
        }

        if ($boost = GDQF){
            rewrite ^.*$ /cache/perm/$http_host/$request_uri$boost_query$query_string.js break;
        }

        location ~ ^/sites/.*/files/imagecache/ {
            #try_files $uri @rewrite;
            error_page 404 = /;
        }

        location ~* \.(txt|jpg|jpeg|css|js|gif|png|bmp|flv|pdf|ps|doc|mp3|wmv|wma|wav|ogg|mpg|mpeg|mpg4|htm|zip|bz2|rar|xls|docx|avi|djvu|mp4|rtf|ico)$ 
        {
            expires max;
            add_header Vary Accept-Encoding;
            if (-f $request_filename) {
                break;
            }
            if (!-f $request_filename) {
                proxy_pass "http://backend";
                break;
            }
        }

        location ~* \.(html(.gz)?|xml)$ {
            add_header Cache-Control no-cache,no-store,must-validate;
            root $myroot;
            if (-f $request_filename) {
                break;
            }
            if (!-f $request_filename) {
                proxy_pass "http://backend";
                break;
            }
        }        

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

        location / {
            proxy_pass http://backend;
        } 

     }
}

UPD:有了这个nginx.conf,我有工作/ . 而任何其他页面都给了我:“页面没有正确重定向” . 谁可以向我解释评估位置规则的顺序?如果它是“休息” - 当nginx遇到这一行时,接下来会做什么 . 我真的尝试了大约20个nginx配置示例 . 我不想要一个更多的链接,我更喜欢某人的答案,他们真正了解nginx.conf中发生了什么 .

UPD2:如果我更换

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

有:

try_files $uri $uri/ @drupal;
        location @drupal {
            rewrite ^ /index.php?q=$uri last; # for Drupal 6
        }

然后所有非根页面都给我404“在此服务器上找不到请求的URL” .

2 回答

  • 0

    以这种方式配置nginx:https://github.com/stanislaw/config_files/blob/master/nginx.conf led Drupal以下行为:如果站点处于维护模式,则一切正常 . 浏览所有非根网址(以管理员身份登录)即可 . 但是,如果我将网站置于在线模式,那么我再次开始全力以赴"Page isn't redirecting properly"(第一个UPD) .

    然后我在新的drupal安装上测试了相同的配置 - 它可以在离线和在线模式下工作 . 导航到所有clean_urls工作 .

    我认为配置确实有效!但是我正在尝试部署的网站包含许多模块,这可能导致这种不正确的重定向 .

    我最终在vhosts部分为我的网站提供了干净的网址,为将来留下了这个问题 . 我现在的配置非常类似于github上的配置(参见链接),但它有一个评论的clean_urls部分(我现在使用'location /'而不是'location~.php') .

    无论如何,我会非常感谢我对当前配置的任何建议和评论(请参阅github上的nginx.conf - 上面的链接) .

    UPD:这里's related post on nginx' s drupal组:http://groups.drupal.org/node/155564

  • 0

    答案2:

    替换以下行的conf

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

    有了这个:

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

    这是有用的链接:

    http://hostingfu.com/article/running-drupal-with-clean-url-on-nginx-or-lighttpd#toc-nginx

    如需快速信息:

    如果drupal安装在根目录中,

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

    如果drupal在子目录中,

    if ($request_uri ~* ^.*/.*$) {
    rewrite ^/(\w*)/(.*)$  /$1/index.php?q=$2 last;
    break;
    }
    

相关问题