简体   繁体   English

从jwilder / nginx-proxy提供静态文件

[英]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. 我有一个Web应用程序(django由uwsgi提供),我使用nginx代理对特定容器的请求。 Here is a relevant snippet from my default.conf. 这是我的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. 现在我希望从nginx而不是uwsgi worker提供静态文件。

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. 我相信这应该让nginx从/ staticfiles文件夹中提供对web.ubuntu.com/static/*的所有请求。

But since the configuration(default.conf) is generated automatically, I don't know how to add the above location to the server block dynamically :( 但由于配置(default.conf)是自动生成的,我不知道如何动态地将上述位置添加到服务器块:(

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. 所以我不知道如何在那里添加位置块,除非我在nginx出现后动态添加到default.conf然后重新加载它我猜。

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. 我确实通过了https://github.com/jwilder/nginx-proxy ,我只看到了一个实例更改每个主机和默认位置设置的示例。 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. 我已经在jwilder / nginx-proxy的Q&A中发布了这个并没有得到回复。

Please help me if there is a way to achieve this. 如果有办法实现这一点,请帮助我。

This answer is based on this comment from the #553 issue discussion on the official nginx-proxy repo. 这个答案是基于官方nginx-proxy repo#553问题讨论中的评论 First, you have to create the default_location file with the static location: 首先,您必须使用静态位置创建default_location文件:

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

and save it, for example, into nginx-proxy folder in your project's root directory. 例如,将其保存到项目根目录中的nginx-proxy文件夹中。 Then, you have to add this file to /etc/nginx/vhost.d folder of the jwilder/nginx-proxy container. 然后,您必须将此文件添加到jwilder/nginx-proxy容器的/etc/nginx/vhost.d文件夹中。 You can build a new image based on jwilder/nginx-proxy with this file being copied or you can mount it using volumes section. 您可以使用jwilder/nginx-proxy构建新映像,并复制此文件,也可以使用volumes部分安装它。 Also, you have to share static files between your webapp and nginx-proxy containers using a shared volume. 此外,您必须使用共享卷在webappnginx-proxy容器之间共享静态文件。 As a result, your docker-compose.yml file will look something like this: 因此,您docker-compose.yml文件将如下所示:

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: 现在,/ /etc/nginx/conf.d/default.confserver块将始终包含静态位置:

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. 这将使Nginx为您提供静态文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM