如何在同一个域上托管我的API和Web应用程序? [英] How can I host my API and web app on the same domain?

查看:106
本文介绍了如何在同一个域上托管我的API和Web应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Rails API和一个Web应用程序(使用express),它们彼此完全独立.我想知道的是,我是否必须分别部署它们?如果可以,该如何做才能使我的api在mysite.com/api中,而Web应用程序在mysite.com/

I have a Rails API and a web app(using express), completely separate and independent from each other. What I want to know is, do I have to deploy them separately? If I do, how can I make it so that my api is in mysite.com/api and the web app in mysite.com/

我见过许多这样做的项目,甚至将api和app放在单独的存储库中.

I've seen many projects that do it that way, even have the api and the app in separate repos.

推荐答案

通常,您不会直接将此类Web应用程序公开给客户端.而是使用代理服务器,该代理服务器将所有传入的请求转发到节点或Rails服务器.

Usually you don't expose such web applications directly to clients. Instead you use a proxy server, that forwards all incoming requests to the node or rails server.

nginx 是一个流行的选择. 入门指南甚至包含与您尝试执行的操作非常相似的示例

nginx is a popular choice for that. The beginners guide even contains a very similar example to what you're trying to do.

您可以使用类似于以下的配置来实现所需的目标:

You could achieve what you want with a config similar to this:

server {
    location /api/ {
        proxy_pass http://localhost:8000;
    }

    location / {
        proxy_pass http://localhost:3000;
    }
}

这是假设您的API在端口8000上本地运行,而您的express应用程序在端口3000上运行.这也不是完整的配置文件-需要将其加载或添加到http块中.从发行版的默认配置开始.

This is assuming your API runs locally on port 8000 and your express app on port 3000. Also this is not a full configuration file - this needs to be loaded in or added to the http block. Start with the default config of your distro.

当有多个位置条目时,nginx选择最具体的一个.您甚至可以添加其他条目,例如投放静态内容.

When there are multiple location entries nginx chooses the most specific one. You could even add further entries, e.g. to serve static content.

这篇关于如何在同一个域上托管我的API和Web应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆