从页面中不可用的docker中的nginx提供的样式表

时间:2015-09-26 00:32:34

标签: css nginx docker

我在docker容器中运行nginx。下面是我的nginx.conf文件:

worker_processes 4;

events {
    worker_connections 1024;
}

http {
    types {
        text/css css;
    }

    upstream appserver {
        server:3000;
    }

    server {
        listen 80;
        server_name localhost;

        root /public;

        try_files $uri/index.html $uri @appserver;

        location @appserver {
            proxy_pass http://appserver;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_redirect off;
        }

        error_page 500 502 503 504 /500.html;
        client_max_body_size 4G;
        keepalive_timeout 10;
    }
}

我的所有静态文件都在/public中。这个nginx容器后面还有另一个容器导轨。

当我curl http://192.168.59.103/landing_page.css时,我的CSS显示正确(192.168.59.103是来自boot2docker ip的地址)。但是,当我直接访问页面时,CSS不可见。我包括这样的样式表:

<link rel="stylesheet" type="text/css" href="/landing_page.css" />

在Chrome检查器中显示如下:

Chrome inspector network tab

但是,回复是空的:

Response for the CSS file

关于该看什么的任何想法?我很难过

2 个答案:

答案 0 :(得分:1)

解决了它。答案在这里:Nginx finds css but doesn't load it into index.html

...但是,最重要的是我遇到了一个额外的障碍:CSS文件被缓存了,所以我不得不转移+重新加载才能让它工作。

答案 1 :(得分:0)

我遇到了这个问题,当我发现应该运行时,没有必要修改nginx conf:

docker stop $(docker ps -a -q)

docker rm $(docker ps -a -q)

要稍后再查看更改conf nginx之后的效果,然后运行:

docker-compose up --build

清除网络浏览器的缓存,Cookie和历史记录

但是即使如此,我仍将和我的conf一起离开这里,目前正在工作:

worker_processes  1;  ## Default: 1

worker_rlimit_nofile 8192;

events {
  worker_connections  4096;  ## Default: 1024
}

http {
  include  mime.types;

  index    index.html index.htm index.php;

  default_type application/octet-stream;
  log_format   main '$remote_addr - $remote_user [$time_local]  $status '
    '"$request" $body_bytes_sent "$http_referer" '
    '"$http_user_agent" "$http_x_forwarded_for"';

  sendfile     on;
  tcp_nopush   on;
  server_names_hash_bucket_size 128; # this seems to be required for some vhosts



    server {

            listen *:80;
            server_name my.devcom;

            error_log  /var/log/nginx/error.log;
            access_log /var/log/nginx/access.log;

            root /www;
            index index.php;

            location = /favicon.ico {
                            log_not_found off;
                            access_log off;
                    }

            location = /robots.txt {
                            allow all;
                            log_not_found off;
                            access_log off;
                    }

            location / {
                try_files $uri $uri/ /index.php?$args;

            }


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

            # FastCGI 
            location ~ \.php$ {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass php:9000;
                fastcgi_index index.php;
                fastcgi_intercept_errors on;

                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_param PATH_INFO $fastcgi_path_info;
            }

            location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
                expires max;
                log_not_found off;
        }


    }
}