Docker编写NGINX代理和静态文件

时间:2016-03-29 22:54:51

标签: nginx docker proxy docker-compose

出于某种原因,我的docker-compose / node / nginx堆栈在提供静态文件时遇到问题。反向代理端工作,但服务我的CSS和JavaScript失败。对于请求的每个静态文件,我收到如下错误:

customnginx_1 | 2016/03/29 22:42:13 [error] 6#6: *1 open()     
"/var/www/static/styles/normalize.css" failed (2: No such file or     
directory), client: 192.168.99.1, server: , request: "GET     
/styles/normalize.css HTTP/1.1", host: "192.168.99.100", referrer:     
"http://192.168.99.100/"

如何将我的“静态”目录正确链接到我的nginx容器,以便正确提供这些文件? 相关代码:

搬运工-compose.yml:

db:
  image: postgres:latest
  environment:
    POSTGRES_USER: sizzeDog
    POSTGRES_PASSWORD: awfulPassword
    POSTGRES_DB: ceres

ceres:
  build: .
  links:
    - db
  environment:
    POSTGRES_HOST: db
    POSTGRES_USER: sizzeDog
    POSTGRES_PASSWORD: awfulPassword
    NODE_ENV: production
  volumes:
    - .:/var/www/

customnginx:
  build: nginx/.
  links:
    - ceres
  volumes:
    - static:/var/www/static
  ports:
    - 80:80

Dockerfile:

FROM node:latest

RUN npm install pm2 -g

VOLUME ["/var/www"]
ADD start.sh /start.sh
RUN chmod 755 /start.sh

EXPOSE 5000
CMD ["/start.sh"]

的nginx / nginx.conf:

worker_processes 1;

events { worker_connections 1024; }

http {
    sendfile        on;
    keepalive_timeout  65;

    gzip              on;
    gzip_http_version 1.0;
    gzip_proxied      any;
    gzip_min_length   500;
    gzip_disable      "MSIE [1-6]\.";
    gzip_types        text/plain text/xml text/css
                      text/comma-separated-values
                      text/javascript
                      application/x-javascript
                      application/atom+xml;

    # List of application servers
    upstream app_server {

        server ceres:5000;
    }

  server {
    listen 80;

    root /var/www/static;

    location /styles/ {}
    location /dist/ {}

    location / {
        proxy_redirect off;
        proxy_set_header   X-Real-IP            $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Proto $scheme;
        proxy_set_header   Host                   $http_host;
        proxy_set_header   X-NginX-Proxy    true;
        proxy_set_header   Connection "";
        proxy_http_version 1.1;
        proxy_pass         http://app_server;
    }
  }
}

的nginx / Dockerfile:

FROM nginx:alpine

VOLUME ["/var/www/static"]

RUN rm -v /etc/nginx/nginx.conf

ADD nginx.conf /etc/nginx/

CMD ["nginx", "-g", "daemon off;"]

更新:将include /etc/nginx/mime.types;添加到nginx.conf的http块修复空css问题:)。

1 个答案:

答案 0 :(得分:0)

在nginx文件中,试一试:

location /static/{
        alias  /var/www/static/;
        expires 1y;
        access_log off;
        add_header Cache-Control "public";
    }

css& js& img,....应该在下面的文件夹中:

/var
-/www
 -/static
  -/css
  -/js
  -/img