【问题标题】:NGINX configuratuion for automatic pretty links for multiple wordpress sites in subdirectoriesNGINX 配置用于子目录中多个 wordpress 站点的自动漂亮链接
【发布时间】:2017-05-11 02:01:49
【问题描述】:

我有一个开发服务器,用于上传客户端网站,每个 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;
    }

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

【问题讨论】:

    标签: php wordpress nginx configuration permalinks


    【解决方案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;
    }
    

    【讨论】:

    • site 变量是 URL,例如dev.example.tld ?当我使用它时显示错误:
    • 对不起,第二个例子中的剪切粘贴错误,当然应该是$1$site$1 是 URI 的第一个路径段 - 例如/site_1/site_2 在你的情况下。如果您需要服务器名称,请使用:$host$server_name$http_host(它们都略有不同 - 请参阅 this link for details
    猜你喜欢
    • 2015-05-21
    • 2018-04-01
    • 1970-01-01
    • 2013-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-09
    相关资源
    最近更新 更多