Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

serving static files from jwilder/nginx-proxy

I have a web app (django served by uwsgi) and I am using nginx for proxying requests to specific containers. Here is a relevant snippet from my default.conf.

upstream web.ubuntu.com {
server 172.18.0.9:8080;
}
server {
server_name web.ubuntu.com;
listen 80 ;
access_log /var/log/nginx/access.log vhost;
location / {
include uwsgi_params;
uwsgi_pass uwsgi://web.ubuntu.com;
}
}

Now I want the static files to be served from nginx rather than uwsgi workers.

So basically I want to add something like:

location /static/ {
autoindex on;
alias /staticfiles/;
}

to the automatically generated server block for the container.

I believe this should make nginx serve all requests to web.ubuntu.com/static/* from /staticfiles folder.

But since the configuration(default.conf) is generated automatically, I don't know how to add the above location to the server block dynamically :(

I think location block can't be outside a server block right and there can be only one server block per server?

so I don't know how to add the location block there unless I add dynamically to default.conf after nginx comes up and then reload it I guess.

I did go through https://github.com/jwilder/nginx-proxy and I only see an example to actually change location settings per-host and default. But nothing about adding a new location altogether.

I already posted this in Q&A for jwilder/nginx-proxy and didn't get a response.

Please help me if there is a way to achieve this.

like image 887
user3732361 Avatar asked Feb 07 '19 11:02

user3732361


People also ask

How do I serve a static file in nginx Docker?

Serving Static Files To deploy the container, use Docker Compose. The Docker Compose output. Your static folder and all of its contents are now being served at http://localhost:8080/ using Nginx running inside Docker. Our static files being served on port 8080.

Where does nginx serve files from?

html , the browser sends the requests to fetch these JavaScript files. Nginx will then serve the requested files from the same root directory. That's it!


1 Answers

This answer is based on this comment from the #553 issue discussion on the official nginx-proxy repo. First, you have to create the default_location file with the static location:

location /static/ {
    alias /var/www/html/static/;
}

and save it, for example, into nginx-proxy folder in your project's root directory. Then, you have to add this file to /etc/nginx/vhost.d folder of the jwilder/nginx-proxy container. You can build a new image based on jwilder/nginx-proxy with this file being copied or you can mount it using volumes section. Also, you have to share static files between your webapp and nginx-proxy containers using a shared volume. As a result, your docker-compose.yml file will look something like this:

version: "3"

services:
  nginx-proxy:
    image: jwilder/nginx-proxy
    ports:
      - "80:80"
    volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro
      - ./nginx-proxy/default_location:/etc/nginx/vhost.d/default_location
      - static:/var/www/html/static

  webapp:
    build: ./webapp
    expose:
      - 8080
    volumes:
      - static:/path/to/webapp/static
    environment:
      - VIRTUAL_HOST=webapp.docker.localhost
      - VIRTUAL_PORT=8080
      - VIRTUAL_PROTO=uwsgi

volumes:
  static:

Now, the server block in /etc/nginx/conf.d/default.conf will always include the static location:

server {
    server_name webapp.docker.localhost;
    listen 80 ;
    access_log /var/log/nginx/access.log vhost;
    location / {
        include uwsgi_params;
        uwsgi_pass uwsgi://webapp.docker.localhost;
        include /etc/nginx/vhost.d/default_location;
    }
}

which will make Nginx serve static files for you.

like image 173
constt Avatar answered Oct 23 '22 04:10

constt