nginx config for Drupal&Boost&ImageCache
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".
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;
}
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
精彩评论