Heroku Nginx configuration

Vaidas Lungis
Feb 23, 2017 · 2 min read

The Setup

A PHP application with NGINX web server runs on Heroku infrastructure.

It is quite easy to add application level Nginx configuration rules. Follow theirs documentation Custom PHP settings. It gets complicated if you want to edit something in Nginx `http` configuration block (server level configuration).

I wanted to use `ngx_http_limit_req_module`. This requires adding `limit_req_zone` in http block. So, I can not add it into configuration file from the above tutorial.

Virtually, you need to and one line into `conf/nginx/heroku.conf.php` of Heroku PHP builpack

First approach

Why not clone Heroku PHP buildpack official repo, edit that file. Then change application buildpack and ready to go! It was pretty easy and straight forward change.

Apparently, buildpacks tend to change. After a month of using custom buildpack, every application build process failed. Forked buildpack repo sync with the origin fixed the issue. This accident rang a bell to find a better solution for this. Heroku support gave one.

I would say it is typical “wanted a banana, got a gorilla holding a banana in the jungle” example.

Second approach

Heroku support suggested using -c option in Procfile to override server-level configuration file. The same file I edited previous in the forked repo. So:

  • created new file in the project,
  • copied contents from official conf/nginx/heroku.conf.php file,
  • added custom modifications (limit_req_zone) rules,
  • edited Procfile to use both -c and -C options (server and application configuration files),
  • set application buildpack to default one — `heroku/php`.

Currently, only one file is overridden instead of the whole repo. This will lead to less failed builds when builpack repo changes.

Vaidas Lungis

Written by

PHP developer | Developer Advocate |Laravel fan. Just write some thoughts to improve my english skills

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade