如何使用gunicorn将heroku nginx构建包添加到python django应用程序中



我正试图在heroku上部署一个带有django的python,并配置一个nginx反向代理,使其像过滤器一样,在将令牌检查到第三方IDP之前,不会让请求传递到我的django后端。

**第1步**

我遵循本教程添加nginx构建包:https://elements.heroku.com/buildpacks/hq-mobile/v3-nginx-buildpack

在我添加了构建包并启动应用程序后,我收到以下消息:bin/start-nginx: line 37: bundle: command not found

经过一番挖掘,我注意到需要在heroku应用程序的配置变量中添加一些路径,以便bundler获得所需的依赖项:

所以我添加了以下路径:

heroku config:add GEM_PATH=vendor/bundle/1.9.3
heroku config:set PATH=bin:vendor/bundle/ruby/1.9.3/bin:/usr/local/bin:/usr/bin:/bin 

然后config/uniorn.rb文件:

require 'fileutils'
listen '/tmp/nginx.socket'
before_fork do |server,worker|
FileUtils.touch('/tmp/app-initialized')
end

程序文件

web: bin/start-nginx bundle exec unicorn -c config/unicorn.rb gunicorn -c gunicorn.conf.py MyApp.wsgi

gunicorn.conf.py

# gunicorn.conf
def when_ready(server):
# touch app-initialized when ready
open('/tmp/app-initialized', 'w').close()
bind = 'unix:///tmp/nginx.socket'
workers = 4

即使添加了这个,错误仍然存在

**第2步**

一旦正常的构建包就位,我想按照本教程的要求配置nginx:

https://www.nginx.com/blog/validating-oauth-2-0-access-tokens-nginx/

需要什么配置才能让这个构建包在我的情况下工作?

要解决此问题,请从Procfile中删除bundle exec部分。换句话说,您的Procfile变成:

web: bin/start-nginx unicorn -c config/unicorn.rb gunicorn -c gunicorn.conf.py MyApp.wsgi

(旁注:仅使用MyApp.wsgi可能会出现错误,因为可能需要将其更改为类似MyApp.wsgi:application的内容以公开应用程序的环境变量(

最新更新