NGINX配置子目录中多个wordpress站点的自动漂亮链接

时间:2016-12-26 16:53:59

标签: php wordpress nginx configuration permalinks

我有一个开发服务器,我上传客户端网站,每个WordPress网站都有自己的数据库和目录(每个网站都是独立的)。我有一个服务器块。我的问题在于非常永久链接,这对我有用:

    server_name dev.example.tld;

    location = /favicon.ico { log_not_found off; access_log off; }
    location = /robots.txt { log_not_found off; access_log off; allow all; }

    location / {
            try_files $uri $uri/ =404;
            add_header 'Cache-Control' 'no-store, no-cache, must-revalidate, proxy-revalidate, max-$
            expires off;
    }

    location /site_1/ {
            index index.php;
            try_files $uri $uri/ /site_1/index.php?$args;
    }

    location /site_2/ {
            index index.php;
            try_files $uri $uri/ /site_2/index.php?$args;
    }

我一直在寻找一种方法,而不是为每个网站添加位置块,但没有运气。

1 个答案:

答案 0 :(得分:1)

您可以将location更改为正则表达式并捕获网站名称:

location ~ ^(?<site>/[^/]+) {
    try_files $uri $uri/ $site/index.php?$args;
}

或者,通过使用重写来实现相同的效果:

location / {
    try_files $uri $uri/ @rewrite;
}
location @rewrite {
    rewrite ^(/[^/]+) $1/index.php last;
    return 404;
}