出于某种原因,我的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容器,以便正确提供这些文件?
相关代码:

docker-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;的http块中添加nginx.conf可以解决空白CSS问题:)。

最佳答案

在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

关于nginx - Docker撰写NGINX代理和静态文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36296566/

10-16 12:39