Bitbucket is a code hosting site with unlimited public and private repositories. We're also free for small teams!

Close

OpenResty for Heroku

This is a Lua rock containing binaries for running OpenResty on Heroku. It's designed to be used in conjunction with the Lua buildpack.

This is an alternative to the OpenResty buildpack. The advantage of this is that you get Lua and LuaRocks with the Lua buildpack. Ideally there is Lua buildpack with LuaRocks and the http servers can be distributed as rocks.

What it does

In addition to a pre compiled openresty, some scripts to start nginx are included. nginx doesn't support passing in port through argument or reading from environment variable so we preprocess the config file.

Anything matching ${{NAME}} is replaced with the environment variable NAME inside of your nginx configuration config. (note: doesn't apply to included files)

The start_nginx.sh script will read in nginx.conf, compile it to nginx.conf.compiled then start nginx. It takes two optional arguments, an alternative path for the config as the first and and an alternative root directory to run the server from. The config must be specified relative to the root.

The daemon off; directive must be added to the config for the entire thing to work.

How to use

Create a new app with the Lua buildpack.

$ heroku create --buildpack http://github.com/leafo/heroku-buildpack-lua.git

Add to your rockspec dependencies:

dependencies = {
  "https://raw.github.com/leafo/heroku-openresty/master/heroku-openresty-dev-1.rockspec"
}

Create a basic nginx.conf, this is the one from the OpenResty guide (with the addition of daemon off and ${{PORT}}):

worker_processes  1;
error_log logs/error.log;
daemon off;
events {
    worker_connections 1024;
}
http {
    server {
        listen ${{PORT}};
        location / {
            default_type text/html;
            content_by_lua '
                ngx.say("<p>hello, world</p>")
            ';
        }
    }
}

Create a Procfile:

web: start_nginx.sh

Commit everything and push to deploy. Run heroku scale web=1 if nginx doesn't start automatically.

Using PostgreSQL

This build includes the Postgres nginx module, all you need to do is pass in your database configuration.

First add a database if you haven't added one already:

heroku addons:add heroku-postgresql:dev

This will set an environment variable inside of your application that looks something like this:

HEROKU_POSTGRESQL_ROSE_URL="postgres://user:password@database.domain.com/databasename"

The nginx config expects a slightly different format, but no worries the config preprocessor has a filter to convert to the correct format.

Add the database to your config like so: (notice the prefix pg)

http {
    upstream database {
      postgres_server ${{pg HEROKU_POSTGRESQL_ROSE_URL}};
    }
}

That's it, your application can now talk to Postgres through the database upstream.

Version

  • OpenResty 1.2.4.7 Development Release --with-luajit --with-http_postgres_module

Recent activity

Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.