Nginx with Parallel CDN?

Events happening in the community are now at Drupal community events on www.drupal.org.
Terko's picture

Hello!
I've installed successfully Nginx with Drupal and FastCGI, But I am trying to achieve better loading times with parallel downloading of site elements.
So I've installed Parallel and now I have cdn1.example.com, cdn2.example.com and cdn2.example.com
They serve images, CSS and JS.
My question is - how to avoid duplicate content? Now if I visit cdn1.example.com I can see everything from example.com
Is there way to do this in settings.php or in Nginx configuration?
In this comment http://drupal.org/node/579624#comment-2120792 I can see this rewrite example with 301 reddirects, but I can't do this with Nginx - searched a lot of information but without luck.
Thanks in advance!

Comments

Yes, either 301 redirects or

brianmercer's picture

Yes, either 301 redirects or what I do is 404 error for the root of the subdomains. Something like...

server {
  listen 80;
  server_name cdn1.example.com cdn2.example.com cdn3.example.com;

  access_log /var/log/nginx/$host.access.log;
  root /var/www/www.example.com;
  server_name_in_redirect off;

  location / {
    return 404;
    # or
    # rewrite ^ http://www.example.com permanent;
  }

  location ~* ^.+.(jpg|jpeg|gif|png|ico)$ {
    # access_log      off;
    # error_log /dev/null crit;
    expires         45d;

    try_files $uri =404;
  }
}

If you're also serving imagecache files from those subdomains then you'd need another location rewriting back to the main domain on 404s so imagecache can handle them. Might also need something similar for fckeditor js files. I saw omegacc updated her git config for that, but wasn't sure why it was necessary.

Thanks! I will try it now :)

Terko's picture

Thanks! I will try it now :)

It works . Yes, I use

Terko's picture

It works .
Yes, I use ImageCache. It's defined already in the first location. I will try to upload file now to see if works. Thanks again! :)

Ops, it don't works as

Terko's picture

Ops, it don't works as expected. It was cached copy. Images are visible, but CSS not.
There is my nginx.conf if you can help

#----------------------------------------------------------------------
# Events Module
#
#   http://wiki.codemongers.com/NginxEventsModule
#
#----------------------------------------------------------------------
user nginx;
worker_processes  6;
events {
    worker_connections  1024;
}


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

    #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  logs/access.log  main;
error_log  /var/log/nginx_error.log debug;
    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  20;
    tcp_nodelay        on;

    #gzip  on;
# output compression saves bandwidth
  gzip  on;
  gzip_http_version 1.1;
  gzip_vary on;
  gzip_comp_level 6;
  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;

  # make sure gzip does not lose large gzipped js or css files
  # see http://blog.leetsoft.com/2007/7/25/nginx-gzip-ssl
  gzip_buffers 16 8k;

  # Disable gzip for certain browsers.
  gzip_disable “MSIE [1-6].(?!.SV1)”;

client_max_body_size 10m;
 
server {
        listen       80;  # Replace this IP and port with the right ones for your requirements
        server_name  uhaaa.com uhaaa.net joke.bg parfumcheta.com supertune.eu supermama.bg;  # Multiple hostnames seperated by spaces.  Replace these as well.
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

         location = / {
            root   /home/uhaaa22/public_html;  # Again, replace this.
            index  index.php;
        }

        location / {
            root   /home/uhaaa22/public_html;
            index  index.php index.html;

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

        }

        error_page  404              /index.php;

        # serve static files directly
        location ^~ ^.+.(jpg|jpeg|gif|css|png|js|ico)$ {
              access_log        off;
            expires           30d;
        }

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        location ~ .php$ {
          fastcgi_pass   127.0.0.1:8888;  # By all means use a different server for the fcgi processes if you need to
          fastcgi_index  index.php;

          fastcgi_param  SCRIPT_FILENAME  /home/uhaaa22/public_html$fastcgi_script_name;   # !! <--- Another path reference for you.
          fastcgi_param  QUERY_STRING     $query_string;
          fastcgi_param  REQUEST_METHOD   $request_method;
          fastcgi_param  CONTENT_TYPE     $content_type;
          fastcgi_param  CONTENT_LENGTH   $content_length;
  fastcgi_param  SERVER_SOFTWARE  nginx/$nginx_version;
  fastcgi_param  REMOTE_ADDR      $remote_addr;
  fastcgi_param  REMOTE_PORT      $remote_port;
  fastcgi_param  SERVER_ADDR      $server_addr;
  fastcgi_param  SERVER_PORT      $server_port;
  fastcgi_param  SERVER_NAME      $server_name;

        }
# imagecache needs to have php read any files that it's planning to manipulate
    location ^~ /files/imagecache/ {
       index  index.php index.html;
       # assume a clean URL is requested, and rewrite to index.php
        if (!-e $request_filename) {
            rewrite  ^/(.)$  /index.php?q=$1  last;
            break;
        }



    }

   location ^~ /jokefiles/imagecache/ {
       index  index.php index.html;
       # assume a clean URL is requested, and rewrite to index.php
        if (!-e $request_filename) {
            rewrite  ^/(.
)$  /index.php?q=$1  last;
            break;
        }



    }
  location ^~ /car/imagecache/ {
       index  index.php index.html;
       # assume a clean URL is requested, and rewrite to index.php
        if (!-e $request_filename) {
            rewrite  ^/(.)$  /index.php?q=$1  last;
            break;
        }



    }
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /.ht {
        #    deny  all;
        #}

     }
server {
  listen 80;
  server_name cdn1.uhaaa.net cdn2.uhaaa.net cdn3.uhaaa.net;

  access_log /var/log/nginx/$host.access.log;
  root /home/uhaaa22/public_html;
  server_name_in_redirect off;

  location / {
    return 404;
    # or
    # rewrite ^ http://www.example.com permanent;
  }

  location ~
^.+.(jpg|jpeg|gif|png|ico)$ {
    # access_log      off;
    # error_log /dev/null crit;
    expires         45d;

    try_files $uri =404;
  }
}
}

Temporary I've disabled your recommendation because the site is full with people.

Yeah! managed to do

Terko's picture

Yeah! managed to do that

server {
  listen 80;
  server_name cdn1.uhaaa.net cdn2.uhaaa.net cdn3.uhaaa.net;

  access_log /var/log/nginx/$host.access.log;
  root /home/uhaaa22/public_html;
  server_name_in_redirect off;

  location / {
    return 404;
    # or
    # rewrite ^ http://www.example.com permanent;
  }

  location ~ ^.+.(jpg|jpeg|gif|png|ico|js|css)$ {
    # access_log      off;
    # error_log /dev/null crit;
    expires         45d;

    try_files $uri @drupal;
  }

location @drupal {
        ###
        ### now simplified to reduce rewrites
        ###
        rewrite ^/(.*)$  http://uhaaa.net/$1 permanent;    }
}

Thanks Brian!

Sorry, I left out the js and

brianmercer's picture

Sorry, I left out the js and css types. It looks like ya got it all worked out.

Apparently nginx doesn't mind, but the silly markdown filter ate the escape backslash here:

location ~* ^.+\.(jpg|jpeg|gif|png|ico)$ {

With this I receive

Terko's picture

With this I receive error
2010/02/02 22:51:40 [emerg] 24084#0: invalid location modifier "*" in /etc/nginx/nginx.conf:161
2010/02/02 22:51:40 [emerg] 24084#0: the configuration file /etc/nginx/nginx.conf test failed

I think that I managed to do it for now :) And it works :) I still test. Some of the ImageCache images don't show up, but this can be caused from Transliteration settings, so I will wait for while to see if it works without problems :)
Thanks again!

ImageCache will not work when

omega8cc's picture

ImageCache will not work when you have location for static images before location for ImageCache in your config. See how it looks in my example: http://github.com/omega8cc/nginx-for-drupal/blob/master/nginx.conf

There is also simple fix for FCKeditor, so it could be useful also for TinyMCE, I suppose, just add TinyMCE directory name to:

location ~* /(files/imagecache)|(fckeditor)|(ckeditor)/ {

Apache

mikeytown2's picture

How to do it in apache; javascript doesn't work though... so using the .js extension would be how to do it.
http://drupal.org/node/579624#comment-2120792

Mikey, I noticed that JS

Terko's picture

Mikey, I noticed that JS don't work well with Nginx too. So I disabled JS for now. The problem was only with TinyMCE editor when editing HTML code

Nginx

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

Hot content this week