# Conserving unicorns by serving images with nginx?

**URL:** https://meta.discourse.org/t/conserving-unicorns-by-serving-images-with-nginx/177775
**Category:** Development
**Created:** [January 30, 2021, 2:13pm UTC](https://meta.discourse.org/t/conserving-unicorns-by-serving-images-with-nginx/177775 "2021-01-30T14:13:17Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![mcdanlj](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mcdanlj/32/131829_2.png) [@mcdanlj](https://meta.discourse.org/u/mcdanlj)
#### Post date: [January 30, 2021, 2:13pm UTC](https://meta.discourse.org/t/conserving-unicorns-by-serving-images-with-nginx/177775/1 "2021-01-30T14:13:17Z")

</div>

I’ve been thinking of trying to conserve memory-heavy unicorn instances by configuring my external nginx (which I already have for serving 502 maintenance page and for correctly attributing IPv6 addresses) to also serve requests for images; with the intent of getting much of the value of moving them to S3 (or compatible object service) but still keeping the site self-contained on my server. (This wouldn’t move other site assets off of unicorns, but those are cached well from page to page, so that’s a much smaller overall cost in unicorns.)

Just like pointing external nginx at a socket in /var/discourse/shared I would serve `/uploads` from `/var/discourse/shared/$container/uploads`

I don’t see mention here on meta that anyone has done this, though my search foo might be weak. Am I missing any reasons this would not work, or would work poorly in practice?

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [January 30, 2021, 2:30pm UTC](https://meta.discourse.org/t/conserving-unicorns-by-serving-images-with-nginx/177775/2 "2021-01-30T14:30:20Z")

</div>

We already serve the images using the internal nginx in the container.

> <https://github.com/discourse/discourse/blob/main/config/nginx.sample.conf#L187-L190>

---

<div class="post-metadata">

### Author: ![mcdanlj](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mcdanlj/32/131829_2.png) [@mcdanlj](https://meta.discourse.org/u/mcdanlj)
#### Post date: [January 30, 2021, 3:16pm UTC](https://meta.discourse.org/t/conserving-unicorns-by-serving-images-with-nginx/177775/3 "2021-01-30T15:16:26Z")

</div>

Oh. At least now the next person with this bright idea might find it when they search for it. Many thanks! 😁

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [January 30, 2021, 3:16pm UTC](https://meta.discourse.org/t/conserving-unicorns-by-serving-images-with-nginx/177775/4 "2021-01-30T15:16:50Z")

</div>

If you want to help your unicorns, one thing that actually helps is [Enable a CDN for your Discourse](https://meta.discourse.org/t/enable-a-cdn-for-your-discourse/14857), as it will cache the few assets that are served on unicorns like the stylesheets.

---

<div class="post-metadata">

### Author: ![mcdanlj](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mcdanlj/32/131829_2.png) [@mcdanlj](https://meta.discourse.org/u/mcdanlj)
#### Post date: [January 30, 2021, 4:29pm UTC](https://meta.discourse.org/t/conserving-unicorns-by-serving-images-with-nginx/177775/5 "2021-01-30T16:29:37Z")

</div>

I would guess that enabling caching on the external nginx would have the same benefit with respect to loading the unicorns, without having to set up a CDN. I see cache control headers on all the javascript assets, so I might give this a try…

… well, I see the internal nginx is also already using `proxy_cache` with a 7d validity for non-error requests:

```plaintext
    location ~ ^/(svg-sprite/|letter_avatar/|letter_avatar_proxy/|user_avatar|highlight-js|stylesheets|theme-javascripts|favicon/proxied|service-worker) {
      ...
      # note x-accel-redirect can not be used with proxy_cache
      proxy_cache one;
      proxy_cache_key "$scheme,$host,$request_uri";
      proxy_cache_valid 200 301 302 7d;
      proxy_cache_valid any 1m;
      proxy_pass http://discourse;
      break;
    }

```

How does a CDN help with unicorns in that case? I see `stylesheets` in that list.
