且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用Heroku分开前端和后端

更新时间:2022-12-21 19:08:10

对于您要尝试的操作,您必须使用webserver来提供静态内容并提供容器(gunicorn,tomcat等)持有你的应用程序。这也是***的做法。



Asume使用nginx作为web服务器,因为它更容易设置。 nginx配置文件看起来像这样

 #项目的服务器定义A 
server {
listen 80;
server_name derpshow.com www.derpshow.com;

位置/ {
#Proxy to gnnicorn。
proxy_pass http://127.0.0.1:<projectA port&gt ;;
#etc ...
}
}

#项目B
server {
listen 80;
server_name api.derpshow.com www.api.derpshow.com;

位置/ {
#在不同的端口上代理gnnicorn。
proxy_pass http://127.0.0.1:<projectBg port&gt ;;
允许127.0.0.1;
否认所有;
#etc ...
}
}



老解答:尝试使用 nginx-buildpack ,它允许你在Heroku的应用服务器前面运行NGINX。然后,您需要在不同的端口上运行应用程序,并将api.derpshow.com和其他端口设置为app.derpshow.com,然后您只能从localhost将呼叫限制为api.derpshow.com。


I have an application, let's call it derpshow, that consists of two repositories, one for the frontend and one for the backend.

I would like to deploy these using Heroku, and preferably on the same domain. I would also like to use pipelines for both parts separate, with a staging and production environment for each.

Is it possible to get both apps running on the same domain, so that the frontend can call the backend on /api/*? Another option would be to serve the backend on api.derpshow.com and the frontend on app.derpshow.com but that complicates security somewhat.

What are the best practices for this? The frontend is simply static files, so it could even be served from S3 or similar, but I still need the staging and production environments and automatic testing and so and so forth.

Any advice is greatly appreciated!

For what you are trying to you must use webserver for serving static content and provide access to container(gunicorn, tomcat, etc...) holding your app. Also this is best practice.

Asume your use nginx as webserver, because its easier to setup. nginx config file would look like this

# Server definition for project A
server {
    listen             80;
    server_name        derpshow.com www.derpshow.com;

    location / {
        # Proxy to gUnicorn.
        proxy_pass             http://127.0.0.1:<projectA port>;
        # etc...
    }
}

# Server definition for project B
server {
    listen             80;
    server_name        api.derpshow.com www.api.derpshow.com;

    location / {
        # Proxy to gUnicorn on a different port.
        proxy_pass             http://127.0.0.1:<projectBg port>;
        allow   127.0.0.1;
        deny    all;
        # etc...
    }
}

And thats it.

OLD ANSWER: Try using nginx-buildpack it allows you to run NGINX in front of your app server on Heroku. Then you need to run your apps on different ports and setup one port to api.derpshow.com and other to app.derpshow.com, and then you can restrict calls to api.derpshow.com only from localhost.