Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Have you managed to make your node nginx proxy setup on Heroku work?

Have you managed to make your node + nginx proxy setup on Heroku work?

Could you, please, tell me how have your organized the directories structure and the files in each directory before doing "git push heroku master"? Which buildpack did you use?

I am getting the message "Push rejected, no Cedar-supported app detected" every time I do "git push heroku master". I have put a "nginx.conf.erb" file in a "/conf" directory.

Thank you.

like image 285
T900M Avatar asked Jul 19 '13 11:07

T900M


2 Answers

I have used a Node.js + NGINX setup on heroku for many projects. This way, you can have nginx handle serving static files, caching, proxying to other servers, and proxying to several node processes.

Use the multi-buildpack buildpack (https://github.com/ddollar/heroku-buildpack-multi).
It allows you to specify a .buildpacks file which refers to several buildpacks. In my .buildpacks file, I use the default Heroku Node buildpack, and a fork of an nginx buildpack that I rebuilt to include SSL support.

https://github.com/theoephraim/nginx-buildpack.git
https://github.com/heroku/heroku-buildpack-nodejs.git

The nginx buildpack uses a nginx.conf.erb file that can reference ENV vars. You must tell it to listen on the port specified by heroku in the environment variable called "PORT"

listen <%= ENV["PORT"] %>;

Then you have your node server start up on whatever port you choose, say 5001, and in your nginx config, you can set up a proxy pass to your node app:

location / {
  proxy_pass      http://127.0.0.1:5001;
}

Note - your procfile needs to use a special start-nginx command (part of the nginx buildpack) that then calls whatever else you pass it. In my case I use forever to run my node app:

web: bin/start-nginx ./node_modules/.bin/forever app.js

And within your main node file, you must create a file when it has started up successfully to signal to the nginx buildpack that it should begin listening

fs.openSync('/tmp/app-initialized', 'w');

There is a full explanation of how to use the nginx buildpack in the readme @ https://github.com/theoephraim/nginx-buildpack

like image 173
theozero Avatar answered Sep 20 '22 15:09

theozero


On Heroku, this setup is used in production by me successfully once the buildpack is installed:

upstream node_entry {
    server unix:/tmp/nginx.socket fail_timeout=0;
}
server {
    listen  <%= ENV['PORT'] %>;
    server_name localhost;
    keepalive_timeout 5;
    location / {
        [other settings…]
        proxy_pass http://node_entry;
    }
}

Then, in your app.js file you can connect with:

Server.listen(‘/tmp/nginx.socket’);
like image 28
droid-zilla Avatar answered Sep 21 '22 15:09

droid-zilla