I am running WordPress multisite with subdomains and domain mapping on Nginx. Everything appears to become working properly except the media library. The files get submitted to wordpress-content/blogs.dir/BLOGID/files and appear properly, however the WordPress rewrite rules aren't working. The URL it's attempting to access is http://mydomain.com/files/2011/06/image.jpg however it just get a 404 error

The nginx conf file is below.

server
{
    listen       80;
    server_name *.mydomain.com;
    index index.html index.htm index.php default.html default.htm default.php;
    root  /home/wwwroot/mydomain.com;

    # WordPress multisite sub-domain rules.
    # Designed to be included in any server {} block.

    error_page 404 = @wordpress;
    log_not_found off;

    # This order might seem weird - this is attempted to match last if rules below fail.
    # http://wiki.nginx.org/HttpCoreModule
    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    # Add trailing slash to */wp-admin requests.
    rewrite /wp-admin$ $scheme://$host$uri/ permanent;

    # Directives to send expires headers and turn off 404 error logging.
    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires 24h;
        log_not_found off;
    }

    # Pass uploaded files to wp-includes/ms-files.php.
    rewrite /files/$ /index.php last;

    location ^~ /files/ {
        rewrite ^.*/files/(.+)$ /wp-includes/ms-files.php?file=$1 last;
    }

    # Rewrite multisite '.../wp-.*' and '.../*.php'.
    if (!-e $request_filename) {
        rewrite ^/[_0-9a-zA-Z-]+(/wp-.*) $1 last;
        rewrite ^/[_0-9a-zA-Z-]+(/.*\.php)$ $1 last;
    }

    location @wordpress {
        fastcgi_pass  unix:/tmp/php-cgi.sock;
        fastcgi_param SCRIPT_FILENAME $document_root/index.php;
        include fcgi.conf;
        fastcgi_param SCRIPT_NAME /index.php;
    }

    # Pass all .php files onto a php-fpm/php-fcgi server.
    location ~ \.php$ {
        # Zero-day exploit defense.
        # http://forum.nginx.org/read.php?2,88845,page=3
        # Won't work properly (404 error) if the file is not stored on this server, which is entirely possible with php-fpm/php-fcgi.

        try_files $uri @wordpress;

        #fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_index index.php;
        fastcgi_pass  unix:/tmp/php-cgi.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    #   fastcgi_intercept_errors on;
        include fcgi.conf;
    }

    # if the requested file exists, return it immediately
    if (-f $request_filename) {
        break;
    }

    ## W3 Total CACHE BEGIN
    set $totalcache_file '';
    set $totalcache_uri $request_uri;

    if ($request_method = POST) {
      set $totalcache_uri '';
    }

    # Using pretty permalinks, so bypass the cache for any query string
    if ($query_string) {
      set $totalcache_uri '';
    }

    if ($http_cookie ~* "comment_author_|wordpress|wp-postpass_" ) {
      set $totalcache_uri '';
    }

    # if we haven't bypassed the cache, specify our totalcache file
    if ($totalcache_uri ~ ^(.+)$) {
      set $totalcache_file /wp-content/w3tc-$http_host/pgcache/$1/_default_.html;
    }

    # only rewrite to the totalcache file if it actually exists
    if (-f $document_root$totalcache_file) {
      rewrite ^(.*)$ $totalcache_file break;
    }                 

    ##W3 Total CACHE END

    # all other requests go to WordPress
    if (!-e $request_filename) {
        rewrite . /index.php last;
    }

    log_format  mydomain.com  '$remote_addr - $remote_user [$time_local] $request '
    '$status $body_bytes_sent $http_referer '
    '$http_user_agent $http_x_forwarded_for';
    access_log  /home/wwwlogs/mydomain.com.log  mydomain.com;
}

Should you still don't have this to operate, please remove wrinkles...

 # Pass uploaded files to wp-includes/ms-files.php.
rewrite /files/$ /index.php last;

location ^~ /files/ {
    rewrite ^.*/files/(.+)$ /wp-includes/ms-files.php?file=$1 last;
}

And place the next code within your location / ...

rewrite files/(.+) /wp-includes/ms-files.php?file=$1 last;

Hope this can help somebody who has exactly the same problem!