问题描述
我有一个应用程序,我们称之为derpshow,它由两个存储库组成,一个用于前端,另一个用于后端。
我想使用Heroku部署这些应用程序,并且最好在同一个域中进行部署。我也想为两个部分分别使用管道,每个部分都有一个分段和生产环境。
是否可以让两个应用程序都在同一个域上运行前端可以调用 / api / *
的后端?另一种选择是在 api.derpshow.com
和 app.derpshow.com
的前端提供后端服务,但是这使安全性有些复杂。
这有什么最佳实践?前端只是静态文件,所以它甚至可以从S3或类似的服务器,但我仍然需要暂存和生产环境和自动测试,等等。
任何建议非常感谢!
对于您要尝试的操作,您必须使用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> ;;
#etc ...
}
}
#项目B
server {
listen 80;
server_name api.derpshow.com www.api.derpshow.com;
位置/ {
#在不同的端口上代理gnnicorn。
proxy_pass http://127.0.0.1:<projectBg port> ;;
允许127.0.0.1;
否认所有;
#etc ...
}
}
老解答:尝试使用,它允许你在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.
这篇关于用Heroku分开前端和后端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!