Docker编写NGINX代理和静态文件

b1payxdu  于 2022-11-28  发布在  Nginx
关注(0)|答案(1)|浏览(202)

由于某种原因,我的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

停靠文件:

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配置文件:

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/停靠文件:

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问题:)。

oxiaedzo

oxiaedzo1#

在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

相关问题