Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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 serve an app's static files. #270

Closed
cstrutton opened this issue Oct 17, 2015 · 7 comments
Closed

How to serve an app's static files. #270

cstrutton opened this issue Oct 17, 2015 · 7 comments

Comments

@cstrutton
Copy link

Thanks for all the work you have put into this great project. I must be missing something, but how do I serve an app's static files out of the same container. Do I need to create per VHOST config pointing to a shared volume? Is there an easier way that I am not seeing?

@jwilder
Copy link
Collaborator

jwilder commented Oct 17, 2015

nginx-proxy just proxies connections to your containers. It can't reach into your container to serve up files from the file system automatically. You would need to have a handler serving up your static assets when nginx-proxy forwards those request back to the container.

You could also use a custom template or vhost config pointing to a shared volume as you said so that nginx-proxy does have access to the files and can serve them directly.

@janekolszak
Copy link

If you can use separate VIRTUAl_HOST/domain for static content (say static.example.com):

  • Create static.example.com.conf file with something like:
server {
    server_name static.example.com;
    listen 80;
    access_log /var/log/nginx/access.log;
    location / {
        root /www/data;
    }
}
  • In dockerfile: COPY static.example.com.conf /etc/nginx/conf.d
  • Mount/Copy the data to the nginx container

If you can't use a separate domain

  • Create example.com file:
location /static {
    root /www/data;
}
  • In dockerfile: COPY example.com /etc/nginx/vhost.d/
  • Mount/Copy the data to the nginx container

You may consider a regex for a clever location specification.

@uLan08
Copy link

uLan08 commented Feb 9, 2017

Since this issue was never closed....

I was able to serve static files this way

server {
    server_name static.example.com;
    listen 80;
    access_log /var/log/nginx/access.log;
    location / {
        root /www/data;
    }
}

Will the proxy configurations from /etc/nginx/conf.d/default.conf apply to the one I created? If not, are there any repercussions to this?

@aliallaoui
Copy link

@janekolszak solution is great but could it be done in a more general manner by editing nginx.tmpl and using environment variables ( like STATIC_DOMAIN and STATIC_LOCATION) ? I am not used to go templating language so any help is welcome ;)

@uLan08
Copy link

uLan08 commented Jan 24, 2018

For me, I went on a different route than what I originally commented here. I set up a vanilla nginx container that does the serving then I just set it with the usual env vars, VIRTUAL_HOST and VIRTUAL_PORT and nginx-proxy just proxies to that container. This approach adheres to the docker way of containers having only one responsibility and also I think is what jwilder originally suggested.

Alexander-Krause-Glau pushed a commit to Alexander-Krause-Glau/rpi-docker-nginx-proxy that referenced this issue Mar 30, 2018
Fix forced renewal of certificates
@1fexd
Copy link

1fexd commented Dec 23, 2021

For me, I went on a different route than what I originally commented here. I set up a vanilla nginx container that does the serving then I just set it with the usual env vars, VIRTUAL_HOST and VIRTUAL_PORT and nginx-proxy just proxies to that container. This approach adheres to the docker way of containers having only one responsibility and also I think is what jwilder originally suggested.

I get that this is the "cleanest" way, but isn't a setup like this quite resource-expensive, since every request for a static file has to pass through two nginx instances? I also found this stackoverflow post where the author suggests to use nginx-proxy's nginx instance, are there any repercussions to doing it like this?

@tkw1536
Copy link
Collaborator

tkw1536 commented Apr 16, 2022

You should probably use Per Virtual-Host Configuration config for this.

@tkw1536 tkw1536 converted this issue into discussion #1962 Apr 16, 2022

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants