# Nginx Reverse Proxy + Imaginary in a single image

**URL:** <https://community.fly.io/t/nginx-reverse-proxy-imaginary-in-a-single-image/117>\
**Category:** General\
**Created:** [August 12, 2020, 6:54pm UTC](https://community.fly.io/t/nginx-reverse-proxy-imaginary-in-a-single-image/117 "2020-08-12T18:54:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mike](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/mike/32/26_2.png) [@mike](https://community.fly.io/u/mike)\
**Post date:** [August 12, 2020, 6:54pm UTC](https://community.fly.io/t/nginx-reverse-proxy-imaginary-in-a-single-image/117/1 "2020-08-12T18:54:24Z")

</div>

Building off a few previous threads, I wanted to kick start the discussion of building a Docker image of an nginx reverse proxy setup that also automatically bundles imaginary.

Ref Threads:

> [@Best Ubuntu+Nginx Reverse Proxy setup?](https://community.fly.io/t/best-ubuntu-nginx-reverse-proxy-setup/41):
>
> Hi Everyone! I’m looking for a Fly optimized nginx Reverse proxy set up. I know I can just use a Ubuntu+Nginx docker image, or even just build one myself, but I’m looking for any best practices or tips on how best to configure Fly as a caching reverse proxy for my project. I’m using flyctl in production already and am familiar with all it provides. We’re using the Fly Certificates for SSL generation as well, so no need to generate SSL inside the image. Side topic: Any recommendation on usin…

> [@Fly image proxy?](https://community.fly.io/t/fly-image-proxy/42):
>
> Hey Fly folks, I’ve seen examples of using Fly as an EDGE image proxy with Imaginary. This is the link where I saw it: [https://fly.io/launch/github/h2non/imaginary](https://fly.io/launch/github/h2non/imaginary) Any best practices on how to set this up? Is there a Github project where I can easily plug this into my project? I’m already serving images via an S3+CloudFront setup managed by Terraform, so I’d want to run Fly in front of that. Thanks!

**The Goal**

The goal of this project is to build a `Dockerfile` intended to be deployed via Fly that covers the following requirements:

- Single Docker image
- nGinx Reverse Proxy for common HTTP requests (most HTML, JS+CSS, etc)
- Images served through the proxy are passed through Imaginary, pulled from the same backend as the Proxy

**Why?**  
Posting this here to put this together in public and solicit input & feedback from this community. I am most creative when I can bounce ideas off of others 🙂

Here’s my starting point from another project: [Example Reverse Proxy nginx.conf · GitHub](https://gist.github.com/mikehostetler/77ac2b78469fe0fb58be204bf0de6651)

Is this interesting?

Any thoughts on this use case?

---

<div class="post-metadata">

**Author:** ![michael](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/michael/32/16_2.png) [@michael](https://community.fly.io/u/michael)\
**Post date:** [August 12, 2020, 7:15pm UTC](https://community.fly.io/t/nginx-reverse-proxy-imaginary-in-a-single-image/117/2 "2020-08-12T19:15:49Z")

</div>

We run an app that bundles redis and a custom redis proxy into a single image. Here’s a [(simplified) gist](https://gist.github.com/michaeldwan/aec8905c54a95a5ab9f0aeba6905eb63) that shows how we got supervisord to manage the two processes.

It’s likely you’ll need to build an image from scratch with nginx + imaginary since the official images have scripts that conflict with each other and/or don’t play nicely with supervisord. supervisord will make sure both services are running, but you might also want health checks with fly to ensure both services are alive and accepting traffic.

---

<div class="post-metadata">

**Author:** ![kurt](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/kurt/32/43757_2.png) [@kurt](https://community.fly.io/u/kurt)\
**Post date:** [August 12, 2020, 9:39pm UTC](https://community.fly.io/t/nginx-reverse-proxy-imaginary-in-a-single-image/117/3 "2020-08-12T21:39:31Z")

</div>

Based on what I’ve seen elsewhere, I would probably run nginx and improxy/imaginary as separate applications. The image transforms are very CPU intensive and it’s likely better to scale them up/down separately than nginx.

Nginx is relatively lightweight, and it’s helpful to keep a fixed number of them running if you want to do disk based caching.

I would also start with OpenResty instead of vanilla nginx: [https://fly.io/docs/app-guides/openresty-nginx-plus-lua/](https://fly.io/docs/app-guides/openresty-nginx-plus-lua/)

OpenResty gives you easy Lua scripting, and almost no one wants to expose image transform URLs directly. Being able to write a little bit of Lua to add custom logic is helpful.
