【问题标题】:Proxy NodeJS app to / and Wordpress admin and API on /admin and /wp-json with Nginx使用 Nginx 将 NodeJS 应用程序代理到 / 和 /admin 和 /wp-json 上的 Wordpress 管理员和 API
【发布时间】:2017-04-08 16:37:23
【问题描述】:

我有一个 NodeJS 应用程序,它服务于我的主要前​​端 (ReactJS),它使用 wp-api 从 Wordpress 获取数据。

我希望两个服务器应用程序都响应同一个地址。运行在端口3000 以响应http://example.com/ 并拥有对http://example.com/wp-admin 的所有请求将Wordpress 的管理面板以及对http://example.com/wp-json 的所有请求重定向到WP API。

我将如何在 Nginx 中进行配置?

【问题讨论】:

  • 我不认为你想做什么很清楚。考虑改写问题,并包括额外的信息,说明什么需要在哪里代理,什么有效,什么无效,以及最终需要如何运作(例如,什么代理在哪里)。
  • 不确定如何改写它。我基本上希望有一个 NodeJS 应用程序由 Nginx 代理到 www.domain.com/*,并将一个 Wordpress 站点代理到同一个域但 www.domain.com/admin。还在纠结吗?
  • 是的。你永远不会描述你做什么,什么不成功。
  • 你做了什么,什么没有成功?

标签: node.js wordpress nginx reverse-proxy


【解决方案1】:

扩展您的节点服务器(第二个)的配置并添加另一个位置:

location /admin {
  # fastcgi stuff to run php/wordpress here
}

【讨论】:

  • 为了清楚起见,您介意编辑一下最终代码的样子吗?
  • 这在其他地方有记录。 easyengine 的一个很好的起点是 common/php.conf 和 common/wpcommon.conf 的内容。
  • 我通读了 wpcommon.conf,但我不确定我是否理解这有什么关系...仅仅发布代码看起来是不是很容易?
【解决方案2】:

使用 Nginx 别名。这对我来说可以。我也有同样的情况。

server {
    server_name app.domain.com;
    location / {
        proxy_pass http: //localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
    location /admin {
        alias/path/to/root/admin/interface/here;
        autoindex off;
    }
}

【讨论】:

  • 根据你的例子,这是我一直在尝试的:pastebin.com/3we3XMat 但是 Nginx 无法正常启动,有问题......
  • 谢谢,但 Nginx 仍然不会使用该代码重新启动。有问题,有指点吗?
  • 尝试 sudo nginx -t 如果有任何错误会显示给你。
  • 如果您愿意,请给我您的代码的 sn-ps,我会在空闲时间查看一次。
  • pastebin 正是我的 Nginx 代码。我使用'sudo service nginx restart'来重启Nginx,它除了-t之外没有。抱歉,我是 Nginx 的菜鸟。
【解决方案3】:

我建议对节点服务器进行命名空间(注意rewrite 行):

location /node-api {

      #strip the node-api and pass the rest of the URL to node
      rewrite /node-api/(.*) /$1  break; 

      proxy_pass http://localhost:3000;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection 'upgrade';
      proxy_set_header Host $host;
      proxy_cache_bypass $http_upgrade;
    }


location ~ ^/(?!node-api)(.*) { #all other URLs NOT starting with node-api
    #put Wordpress FCGI config here
 }

您只需在应用程序代码中更改您的 API URL 从 http://app.domain.comhttp://app.domain.com/node-api

【讨论】:

  • 感谢您的回复。但我想要做的是让 NodeJS 应用程序在http://domain.com 上为主要前端服务,PHP 在http://domain.com/wp-admin 上为 WP wp-admin 服务,在`domain.com/wp-json/ 上服务于 API。这可行吗?
猜你喜欢
  • 1970-01-01
  • 2012-05-31
  • 2018-07-14
  • 2021-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-03
  • 1970-01-01
相关资源
最近更新 更多