Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to handle static assets using root directive #553

Closed
reejosamuel opened this issue Aug 31, 2016 · 11 comments
Closed

How to handle static assets using root directive #553

reejosamuel opened this issue Aug 31, 2016 · 11 comments

Comments

@reejosamuel
Copy link

I'm using this with rails running a puma server.

Rials is relying on nginx to serve static assets. Could you please explain how to set the root /app/public; setting on nginx config when using your automated image.

@wader
Copy link

wader commented Aug 31, 2016

Hi, the easiest way is probably to run a web server inside the container for servering the assets and in turn that one proxies to puma etc. I guess it is doable but a bit tricky for nginx-proxy to server assets for another container, that would require it to share some filesystem volume etc.
My suggestion is to threat nginx-proxy as a pure forwarding proxy.

@mjdavies
Copy link

I'm stuck in the same spot, there must be a way to accomplish this, as you can override most things per virtual host setting.

Anyone got any ideas?

I really don't want to have another nginx container, that sounds a bit backwards.

@wader
Copy link

wader commented Oct 20, 2016

@mjdavies i think the tricky bit is how nginx-proxy would access the static assets. Share volume on the host or share volume between the containers? in my opinion both are quite ugly and also would require container recreate if a container is added/removed/changed

@mjdavies
Copy link

Hi @wader

After looking into it a bit more, I see what you mean. :-(

I'm still a bit lost as to how to achieve the web server inside the container bit though.

Puma or unicorn or thin are all running in web container, so create another nginx container that points at the web container?

A bit lost, tis all.

@wader
Copy link

wader commented Oct 20, 2016

Hi again. My suggestion it to run two web servers inside the container. One nginx etc with a tiny config to serve static files and forward the rest to puma, unicorn etc. The static/forward web server is the only web server that will be accessible from outside, puma, unicorn will only listen locally or use unix socket etc.

@mjdavies
Copy link

Thanks for replying @wader

I've never seen anyone running two 'things' inside the one container before, it kind of breaks docker rules doesn't it?

Having said that, I don't really care that much about the rules.

if you have 2 minutes to knock up a quick docker-compose file to give me a rough idea of what you mean that would be really helpful, it doesn't have to work, just an idea of what you mean as I have none now.

I'm attaching what I've got so far for a rails app, running locally in production mode(docker-entrypoint.sh kicks that off)

Any tips on how to add another service to the container very much welcomed.

docker-compose-prod.txt

@mjdavies
Copy link

HI @wader

Sorry, must have been having a mental block yesterday, you mean install nginx and set it up inside the Dockerfile for the app, just like we're setting up puma or unicorn.

Thanks for your help with this, it is appreciated.

@mjdavies
Copy link

Something alot like this

http://nmajor.com/hosting-rails-in-production-on-docker/

@wader
Copy link

wader commented Oct 21, 2016

@mjdavies 👍 but i agree that it feels a bit backwards... but for a medium size ruby app i guess it is the simplest solution. All other solution i can think of gets very complicated 😄

@geoff-va
Copy link

I found the following to work -- I'm using django and it currently collects all the static content into a dir in its container, then I share it to the nginx-proxy container via a mapped volume.

  • Make sure static content is available in the nginx-proxy container (via something like a mapped/shared volume)
  • create a file named either default_location or <hostname>_location with contents something like:
location /static/ {
    alias /mapped/dir/to/top/of/static/;
}
  • default_location will inject the contents into location for all hosts
  • <hostname>_location will inject the contents into location for the specified host
  • copy the file into /etc/nginx/vhost.d/ during your build

When you use default_location in vhost.d, your server block becomes:

server {
	server_name <your_host_name>;
	listen 80 ;
	access_log /var/log/nginx/access.log vhost;
	location / {
		proxy_pass http://<your_host_name>;
		include /etc/nginx/vhost.d/default_location;
	}
}

Since I'm only serving one site, this seems to work well for me.

I didn't realize it, but it would seem you can use nested location directives. I wasn't expecting this to work :)

@tkw1536
Copy link
Collaborator

tkw1536 commented Apr 10, 2022

This seems to be resolved by the last comment. If this is not the case, please reopen.

@tkw1536 tkw1536 closed this as completed Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants