0

UPD!!!: Issue discussed here is completely solved by this topic:

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

And by detailed exploration of:

Nginx configuration by Nginx&Drupal guru - António P. P. Almeida (Perusio).

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


can't solve the following problem: I have FreeBSD, Apache 2.2, PHP (no FastCGI!) as apache module, nginx 0.8.5.4.

I'm trying to move Drupal portal having boost and image_cache enabled on it to personal VPS server.

My goal is to have clean_url rewrites in nginx and correct boost & image_cache rules.

Please help! I know that something's very wrong with my current nginx config. The whole day has been cut on it.

Here is nginx.conf (Only / route works now):

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: With this nginx.conf I have working /. And any other page gives me: "The page isn't redirecting properly". Who can explain me the order in which location rules are evaluated? And if it is "break" - when nginx meets this line, what it does next. I really tryed about 20 nginx config samples. I don't want one more link, I'd prefer answers of somebody, who has real understanding of what's going on in nginx.conf.

UPD2: If I replace

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

with:

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

Then all non-root pages give me 404 "The requested URL was not found on this server".

Stanislav Pankevich
  • 11,044
  • 8
  • 69
  • 129

2 Answers2

0

Answer 2:

Replace the following lines of conf

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

    location / {
        proxy_pass http://backend;
    }

with this:

location / {
    root   /path/to/drupal;
    index  index.php index.html;

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

}

Here is the useful link:

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

For quick info:

If drupal install in the root directory,

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

If drupal in a subdirectory,

if ($request_uri ~* ^.*/.*$) {
rewrite ^/(\w*)/(.*)$  /$1/index.php?q=$2 last;
break;
}
Rakesh Sankar
  • 9,337
  • 4
  • 41
  • 66
  • Yeah, thanks. Clean Urls seem to be working. Interesting that, I've already tryed exactly this link, but it didn't work for me then. But now imagecache images are not displayed. Would be interesting to see the whole config. – Stanislav Pankevich Jun 13 '11 at 09:50
  • Which one of them have you used? Since, both will try to give you the CLEAN URLs. What happens when you try to open the image URL in a new window? – Rakesh Sankar Jun 13 '11 at 09:53
  • I used first rule. Nothing happens - no file exists. It doesn't generates images now. I cleared sized images some time before setting up nginx. Without nginx image_cache generated images. Looks like my nginx.conf lacks some proper rule so that addressing the non-presenting image results to it's generation by image_cache – Stanislav Pankevich Jun 13 '11 at 10:08
  • Check error-log see if you find an error? Please paste if you have any. – Rakesh Sankar Jun 13 '11 at 10:13
  • This is strange, but I don't see imagecache related errors in 5 log files: /var/log/messages, /var/log/httpd-access and httpd-errors logs, and access and errors logs for nginx. – Stanislav Pankevich Jun 13 '11 at 10:20
  • It could be a problem with the code that creates images, may be revert the changes in the .conf and see does it work? – Rakesh Sankar Jun 13 '11 at 10:29
  • Strange things. After I restarted FreeBSD (maybe 100 tries of nginx&apache various configs before that), pictures begin to generate. – Stanislav Pankevich Jun 14 '11 at 00:42
  • I don't know how, but clean urls seem to work only if I log as admin in maintenance mode. If i put site to online - I have improper redirects (see UPD). – Stanislav Pankevich Jun 14 '11 at 00:52
  • Wondering why do you need a PROXY? Are you doing it? – Rakesh Sankar Jun 14 '11 at 03:44
  • You mean proxy_pass? I don't insist on using it. what alternatives? – Stanislav Pankevich Jun 14 '11 at 04:02
  • Okay. Please take a look at the updated answer above and let me know if that solves your problem. – Rakesh Sankar Jun 14 '11 at 04:27
  • Sorry, how nginx should be aware then, that it should pass request to 81 port to Apache? – Stanislav Pankevich Jun 14 '11 at 05:00
  • In such case, I would advice you to have the rewrite done in Apache instead of NGINX. Remove all the rewrite rules and have a RewriteRule in Apache conf. file. – Rakesh Sankar Jun 14 '11 at 05:19
  • This is where the story begins. I want to have them in nginx ). If rules are written in .htaccess - all works. – Stanislav Pankevich Jun 14 '11 at 05:33
  • Is there any particular reason to have them in NGINX?? Sorry I am being naive here, trying to solve/understand your problem. – Rakesh Sankar Jun 14 '11 at 05:39
  • It is practice recommended by community. For now I have my rules working only for maintenance mode turned on. Navigating all clean urls for Admin user works properly. I substituted rule you recommended, by similar (community recommendations) try_files directive. I will try my configs on fresh drupal installiation. I suspect some modules are not working. – Stanislav Pankevich Jun 14 '11 at 06:24
0

Having nginx configured this way: https://github.com/stanislaw/config_files/blob/master/nginx.conf led Drupal to following behaviour: All worked okay, if the site was in maintenance mode. Navigating all non-root urls (signed in as admin) was ok. But if I put site to online mode, then I again began to get all there "Page isn't redirecting properly" (first UPD).

Then I tested the same configuration on fresh drupal installiation - it worked in both off- and online modes. Navigation to all clean_urls worked.

I think that config is indeed working!, but the site I'm trying to deploy contains of many modules, that can cause such improper redirects.

I ended up having clean urls in vhosts section for my site leaving this problem for the future. My present config is very similar to the one on github (see the link) but it has clean_urls section commented (I use 'location /' instead of 'location ~ .php' now).

Anyway, I would be very thankful really for any advices & comments on my current config (see nginx.conf on github - link above).

UPD: here's related post on nginx's drupal group: http://groups.drupal.org/node/155564

Stanislav Pankevich
  • 11,044
  • 8
  • 69
  • 129