如何在子文件夹中为WordPress REST API配置nginx?

时间:2019-07-08 07:21:36

标签: wordpress wordpress-rest-api nginx-config

我试图在我们域的子文件夹中(即不是多站点)设置多个Wordpress站点,但是我很难配置REST API端点。例如,此端点可以正常工作:

https://example.com/site1/?rest_route=/wp/v2/posts

但是此端点给出了404:

https://example.com/site1/wp-json/wp/v2/posts

我已经尝试在我的nginx配置中使用以下规则将失败的网址重写为工作网址:

location /site1/wp-json {
    rewrite ^/site1/wp-json(.*)$ /site1/?rest_route=$1;
}

location /site1/ {
   try_files $uri $uri/ /site1/index.php$is_args$args;
}

我在WordPress docsnginx wiki中看不到wp-json的任何特殊处理。我在这里想念什么?如果该站点的永久链接可能起作用,则将其设置为Numerichttps://example.com/site1/archives/123)。

更新

已编辑full config file的要旨和配置语法都可以:

nginx -c /etc/nginx/nginx.conf -t
  

nginx:配置文件/etc/nginx/nginx.conf语法没问题
  nginx:配置文件/etc/nginx/nginx.conf测试成功

3 个答案:

答案 0 :(得分:0)

我认为重写指令的编写应如下所示:

server {
    location /site1/wp-json
        {
            rewrite ^(/site1/wp-json.*)$ /site1/?rest_route=$1 last;
        }
}

答案 1 :(得分:0)

我能够这样解决它:

const ExcelSheet = new ExcelSheet() // excel.js library

const usersWithPlayedGames  = await findAllUsers({ include: GameTable });

const excelLoop = (index) => {

  for (let j = 0; j < usersWithPlayedGames[index].length; j++) {
      // Write some user's game on Excel
      ...
      ...
      for (let k = 0; k < usersWithPlayedGames[index][j].length; k++) {
          // Write some users's game's company data on Excel
          ...
          ...
      }
  }

  if (index < usersWithPlayedGames.length) {
    setImmediate(() => excelLoop(index + 1))
  }
  else {
    res.send(ExcelSheet.toFile());
  }
};

excelLoop(0);

答案 2 :(得分:0)

一种简单的方法如果子文件夹中的网站页面已经正常运行,只需将 index.php 添加到网址中:

https://site1.com/site2/index.php/wp-json/

如果您的网站页面仍在子文件夹中不起作用,请将此代码也添加到nginx / sites-available / website.conf文件中:

location /site2 {
        rewrite ^(/[^/]+)?(/wp-.*) /site2/$2 break;
        rewrite ^/site2/(.*)$ /site2/index.php?q=$1 last;
}
相关问题