对css,js和静态文件的更改未反映在nginx docker容器上

时间:2016-03-31 10:55:04

标签: php nginx docker nginx-location

我创建了一个关于php的多容器应用程序。当我修改php文件时,我可以看到浏览器中的更改。但是,当我修改静态文件(如css和js)时,浏览器中没有任何更改。以下是我的Dockerfile代码:

Dockerfile

`FROM nginx:1.8
`ADD default.conf /etc/nginx/conf.d/default.conf
`ADD nginx.conf /etc/nginx/nginx.conf
`WORKDIR /Code/project/
`RUN chmod -R 777 /Code/project/
`VOLUME /Code/project

default.conf

`server {
listen       80;
server_name  localhost;

root   /Code/project/public;
index  index.html index.htm index.php;
#charset koi8-r;
#access_log  /var/log/nginx/log/host.access.log  main;

location / {
    try_files $uri $uri/ /index.php?$query_string;
}


#error_page  404              /404.html;

# redirect server error pages to the static page /50x.html
#
#error_page   500 502 503 504  /50x.html;
#location = /50x.html {
 #   root   /usr/share/nginx/html;
#}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
#    proxy_pass   http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
    try_files $uri /index.php =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass   fpm:9000;
    #fastcgi_pass   127.0.0.1:9000;   
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
#    deny  all;
#}
 }

nginx.conf

user  root;
worker_processes  8;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
     worker_connections  1024;
}


http {
   include       /etc/nginx/mime.types;
   default_type  application/octet-stream;

   log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';

   access_log  /var/log/nginx/access.log  main;

sendfile        on;
#tcp_nopush     on;

keepalive_timeout  65;
client_max_body_size 20m;

#gzip  on;

include /etc/nginx/conf.d/*.conf;
}`

搬运工-compose.yml

webphp:
    image: index.alauda.cn/chuanty/local_php
    #image: index.alauda.cn/chuanty/local_nginx:snapshot
    ports:
     - "9000:9000"
volumes:
     - .:/Code/project
links:
     - cache:cache
     - db:db
     - es:localhost
extra_hosts:
     - "chaunty.taurus:192.168.99.100"  

cache:
     image: redis
ports:
     - "6379:6379"
db:
  #image: mysql
  image: index.alauda.cn/alauda/mysql
  ports:
      - "3306:3306"
environment:
   MYSQL_ROOT_PASSWORD: chuantaiyidev
   MYSQL_USER: cty
   MYSQL_PASSWORD: chuantaiyidev
   MYSQL_DATABASE: taurus
es:
   image: index.alauda.cn/chuanty/local_elasticsearch
ports:
   - "9200:9200"
   - "9300:9300"

server:
   #image: index.alauda.cn/ryugou/nginx
   image: index.alauda.cn/chuanty/local_nginx:1.1
ports:
   - "80:80"
   - "443:443"
links:
   - webphp:fpm  
volumes_from:
   - webphp:rw

3 个答案:

答案 0 :(得分:4)

我猜你的问题是#34; sendfile on"在你的nginx.conf中。

出于开发目的,尝试在服务器块的server-directive中设置它:

server {
    ...

    sendfile off;
}

这将强制重新加载静态文件,如css和js。 nginx不会从内存中加载文件。

http://nginx.org/en/docs/http/ngx_http_core_module.html#sendfile

答案 1 :(得分:1)

我真的希望你实际上不要将配置文件复制到图像中!

Docker最佳实践

Docker镜像应该是不可变的,因此在部署时,配置文件(取决于很多[环境]变量)应该通过docker run选项{{传递给容器/与容器共享1}}而不是。

关于你的问题

如果您希望在修改静态文件时看到任何更改,则需要-v|--volume然后docker build 进行每次修改才能实际更改网页。你可能不希望(显然)。我建议你使用共享目录(通过docker compose up选项)。

答案 2 :(得分:0)

一种选择是将修改后的静态文件直接复制到Docker容器中:

docker cp web/static/main.js container_name:/usr/src/app/static/main.js

  • web/static是主机上的静态目录
  • main.js是修改后的文件
  • container_name是可以使用docker ps
  • 找到的容器名称
  • /usr/src/app/static是Docker容器中的静态目录

如果要确定Docker容器上的静态文件的确切位置,可以使用docker exec -t -i container_name /bin/bash来探索其目录结构。