NGINX Multisite Rewrite rules

时间:2011-05-21 作者:Nick Hamze

我对我的新NGINX 安装

如果我像下面这样手动访问图像:

incredible.devpreview.co/wp-content/blogs.dir/2/files/2011/05/stockvault-colorful-illustration108811.jpg

它出现了,但WordPress正在这里寻找它:

incredible.devpreview.co/files/2011/05/stockvault-colorful-illustration108811.jpg

以下是我的NGINX配置文件中的内容:

server {

listen   80;
server_name  www.devpreview.co;
rewrite ^/(.*) http://devpreview.co/$1 permanent;

}


server {

listen   80;
server_name devpreview.co *.devpreview.co;

access_log /var/www/devpreview.co/logs/access.log;
error_log /var/www/devpreview.co/logs/error.log;

location / {
 root   /var/www/devpreview.co/public/;
 index  index.html index.htm index.php;
 if (!-e $request_filename) {
        rewrite ^.+/?(/wp-.*) $1 last;
        rewrite ^.+/?(/.*\\.php)$ $1 last;
        rewrite ^(.+)$ /index.php?q=$1 last;
        }
 }

 location ~* ^.+\\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js)$
        {
        root  /var/www/devpreview.co/public/;
        rewrite ^/.*(/wp-.*/.*\\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js))$ $1 last;

        rewrite ^.*/files/(.*(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js))$/wp-includes/ms-files.php?file=$1 last;
        expires 30d;
        break;
        }

1 个回复
最合适的回答,由SO网友:Chris_O 整理而成

下面概述的Nginx多站点方法已经过测试,可以用于子域多站点安装,也可以用于域映射插件。

为了简化将来进行更改的过程,服务器文件被组织为3个单独的文件。第一个是nginx。conf,它保存基本配置并告诉服务器在哪里找到vhost。

nginx公司。形态

user www-data;
worker_processes 4;
pid /var/run/nginx.pid;
 
events {
    worker_connections 1024;
    # multi_accept on;
}
 
http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    server_tokens off;
    include mime.types;
    default_type  application/octet-stream;
    index index.php index.htm index.html redirect.php;
 
    #Gzip
    gzip  on;
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_disable "MSIE [1-6].(?!.*SV1)";
    gzip_types text/plain text/css application/json application/x-javascript text/xml
                        application/xml application/xml+rss text/javascript;
 
    #FastCGI
    fastcgi_intercept_errors on;
    fastcgi_ignore_client_abort on;
    fastcgi_buffers 8 16k;
    fastcgi_buffer_size 32k;
    fastcgi_read_timeout 120;
    fastcgi_index  index.php;
 
    limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
 
    ##
    # Virtual Host Configs
    ##
    include /etc/nginx/sites-enabled/*;  
}
下一个文件将是全球WordPress include,并将保存我们的所有重写规则。

wp multi。形态

error_page 404 = @wordpress;
log_not_found off;
 
location / {
    try_files $uri $uri/ /index.php?$args;
}
 
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
 
location ~* \\.(js|css|png|jpg|jpeg|gif|ico)$ {
    expires max;
    log_not_found off;
}
 
rewrite ^/files/(.+) /wp-includes/ms-files.php?file=$1 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 php;
        fastcgi_param SCRIPT_FILENAME $document_root/index.php;
        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  SCRIPT_NAME        $fastcgi_script_name;
        fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
        fastcgi_param  REQUEST_URI        $request_uri;
        fastcgi_param  DOCUMENT_URI       $document_uri;
        fastcgi_param  DOCUMENT_ROOT      $document_root;
        fastcgi_param  SERVER_PROTOCOL    $server_protocol;
 
        fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
        fastcgi_param  SERVER_SOFTWARE    nginx;
 
        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;
 
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_NAME /index.php;
}
 
location ~ \\.php$ {
    try_files $uri @wordpress;
    fastcgi_index index.php;
    fastcgi_pass php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    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  SCRIPT_NAME        $fastcgi_script_name;
        fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
        fastcgi_param  REQUEST_URI        $request_uri;
        fastcgi_param  DOCUMENT_URI       $document_uri;
        fastcgi_param  DOCUMENT_ROOT      $document_root;
        fastcgi_param  SERVER_PROTOCOL    $server_protocol;
 
        fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
        fastcgi_param  SERVER_SOFTWARE    nginx;
 
        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;
 
        fastcgi_pass 127.0.0.1:9000;
}
第三个文件是我们的vhosts。如果您使用域映射插件,则可以为每个域映射站点包含一行。如果需要,还可以向该文件添加其他静态站点。

server {
     listen 80;
    #Add a server_name entry for each mapped domain

    server_name star.main-multisite-domain.com *.main-multisite.com;
    server_name mapped-domain.com www.mapped-domain.com;
    server_name mapped-domain-2.com www.mapped-domain-2.com;
    server_name mapped-domain-3.com www.mapped-domain-3.com;
 
    root /srv/www/wordpress/public;
    access_log /srv/www/wordpress/logs/access.log;
    error_log /srv/www/wordpress/logs/error.log;
    index index.php index.html index.htm;
 
        #The Multisite rules are in the include below
    include global-wp-multi.conf;
}
 
#Begin Static Non WP Sites
#Repeat the vhosts below for each additional static site.  Make sure you make each one a root directory
server {
 
    server_name static-non-wp-domain.com www.static-non-wp-domain.com;
    root /srv/www/static-non-wp-domain/public;
 
        error_log /srv/www/static-non-wp-domain/logs/error.log;
    index index.php index.html index.htm;
 
    }
有关将WordPress与Nginx结合使用的更多信息,请参阅:WordPress Performance Server - Nginx - Debian "squeeze"

结束

相关推荐

当安装在子文件夹中时,您可以安装/激活MultiSite吗?

我有初级域名。com和一个附加域。com公司附加域。com指向primarydomain。com/附加域我已经在primarydomain上分别安装了2次wordpress。com和primarydomain。com/附加域我可以激活primarydomain上的多站点吗。com/附加域作为附加域。com?我这样做的原因是这是两个独立的站点,有两个独立的功能,所以我想使用独立的数据库。这可能吗?