perusio / drupal-with-nginx

Running Drupal using nginx: an idiosyncratically crafted bleeding edge configuration.
855 stars 246 forks source link

How to correctly add webmail config to Nginx #245

Open d0dge opened 8 years ago

d0dge commented 8 years ago

This is more of a request for assistance than an issue so apologies if it shouldn't have been posted here. Hopefully someone can help though!

I'm trying to get Rainloop webmail up and running on a server that also hosts a Drupal site. I'm using Perusio's Nginx config.

I basically just want to add a location to handle the path to Rainloop so that the webmail will be accessible via https://example.com/rainloop.

I've added the following just outside the default location block in drupal.conf:

## Webmail handling
# serve static files
location ~ ^/rainloop/(images|javascript|js|css|flash|media|static)/  {
    rewrite        ^ https://$server_name$request_uri? permanent;
    root /usr/share/nginx/;
    expires 30d;
}

location /rainloop {
    root /usr/share/nginx/;
    index index.html index.htm index.php;
    autoindex on;
    autoindex_exact_size off;
    autoindex_localtime on;
}

location ^~ /rainloop/data {
    deny all;
}

It's returning 404 even though the paths are correct. I suspect it's to do with one or both of these locations at the end of the config:

location ^~ /index.php {
    if ( $args ~* "^q=(?<query_value>.*)" ) {
        rewrite ^/index.php$ $scheme://$host/?q=$query_value? last;
    }
    return 404;
}

## Any other attempt to access PHP files returns a 404.
location ~* ^.+\.php$ {
    return 404;
}

How do make an exception to these 404 rules for the rainloop directory?

emesch commented 8 years ago

The last regex location will catch any uri that ends in .php, even if it starts with "rainloop". That's because nginx first parses the location prefixes, and then moves on to the location regexes. If you want nginx to skip the regex matching if a certain prefix match is made, use the "^~" modifier. Try:

location ^~ /rainloop {
    ...
}

The nginx location documentation has more details.

Also, don't you need to pass to your php handler?

d0dge commented 8 years ago

Perfect thank you emesch, that did the trick.

Yes you're right, I did need to add my php handler in there too. I've amended the rainloop location to this and it seems to work OK:

 location ^~ /rainloop {
        root /usr/share/nginx/;
        index index.html index.htm index.php;
        autoindex on;
        autoindex_exact_size off;
        autoindex_localtime on;
        fastcgi_pass 127.0.0.1:9000;
        include /etc/nginx/fastcgi.conf;
    }

Is there any reason why I should put the PHP handling in a separate block? Something like:

location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        include fastcgi.conf;
    }