简体   繁体   English

如何使用gunicorn将heroku nginx buildpack添加到python django应用程序中?

[英]How to add the heroku nginx buildpack to a python django app with gunicorn?

I am trying to deploy a python with django on heroku and configure an nginx reverse proxy to have like a filter which won't let the requests pass to my django backend until the token is checked to an third party IDP.我正在尝试在 heroku 上部署带有 django 的 python 并配置一个 nginx 反向代理,使其像过滤器一样不会让请求传递到我的 django 后端,直到令牌被检查到第三方 IDP。

** STEP 1 ** ** 第1步 **

I am following this tutorial to add the nginx buildpack : https://elements.heroku.com/buildpacks/hq-mobile/v3-nginx-buildpack我正在按照本教程添加 nginx buildpack: https ://elements.heroku.com/buildpacks/hq-mobile/v3-nginx-buildpack

After I've added the buildpack and start the app, I get the following message: bin/start-nginx: line 37: bundle: command not found添加 buildpack 并启动应用程序后,我收到以下消息: bin/start-nginx: line 37: bundle: command not found

After some digging I noticed some paths need to be added to the config vars for the heroku app in order for bundler to get the needed dependencies :经过一番挖掘,我注意到需要将一些路径添加到 heroku 应用程序的配置变量中,以便捆绑程序获得所需的依赖项:

So I've added this paths:所以我添加了这个路径:

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 

Then the config/unicorn.rb file:然后是config/unicorn.rb文件:

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

Procfile :简介

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

gunicorn.conf.py 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

Even after adding this, the error still persists.即使添加了这个,错误仍然存​​在。

** STEP 2 ** ** 第2步 **

Once the normal buildpack will be in place I want to follow this tutorial to configure nginx how I want:一旦正常的 buildpack 就位,我想按照本教程来配置我想要的 nginx:

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

What configuration is needed to be able to have this buildpack working in my situation?需要什么配置才能让这个 buildpack 在我的情况下工作?

To fix this problem, you remove the bundle exec part from the Procfile.要解决此问题,请从 Procfile 中删除bundle exec部分。 In other words, your Procfile becomes:换句话说,您的 Procfile 变为:

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

(sidenote: there might be an error with just using MyApp.wsgi since you might need to change it to something like MyApp.wsgi:application to expose the environment variable for your application) (旁注:仅使用MyApp.wsgi可能会出错,因为您可能需要将其更改为MyApp.wsgi:application以公开MyApp.wsgi:application的环境变量)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM