WordPress子域名多站点Nginx媒体库

时间:2011-06-05 23:01:44

标签: wordpress nginx rewrite subdomain

我在Nginx上运行带有子域和域映射的WordPress多站点。除媒体库外,一切似乎都正常工作。文件上传到wp-content / blogs.dir / BLOGID / files并正确显示,但WordPress重写规则无法正常工作。它尝试访问的网址是http://mydomain.com/files/2011/06/image.jpg,但它一直收到404错误

nginx conf文件位于下方。

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;
}

1 个答案:

答案 0 :(得分:2)

如果你仍然没有这个工作,请删除这些行...

 # 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 files/(.+) /wp-includes/ms-files.php?file=$1 last;

希望这有助于遇到同样问题的人!