【问题标题】:css is not loading in docker using nginixcss 未使用 nginx 在 docker 中加载
【发布时间】:2019-02-11 00:26:21
【问题描述】:

我正在尝试在 docker 中运行我的 codeigniter 项目。我在 docker-compose.yml 中有以下应用配置(仅应用部分如下所示)

app:
  build: .
  volumes:
    - .:/var/www/html/codeigniter/
  depends_on:
    - db
  environment:
    - POSTGRES_HOST=db
    - POSTGRES_USER=postgres
    - POSTGRES_PASSWORD=dummy
  ports:
    - 8080:81

var/www/html/codeigniter/app/nginx/default.conf中有如下内容(nginx conf)

server {
   listen 81 default_server;
   listen [::]:81 default_server;
   server_name localhost;

   root /var/www/html/codeigniter;
   index index.html index.php;

   location ~* \.(ico|css|js|gif|jpe?g|png)$ {}

   location / {
      try_files $uri $uri/ /index.php$is_args$args;
      include /etc/nginx/mime.types;
   }

   location ~* \.php$ {
      fastcgi_pass app:9000;
      fastcgi_index index.php;
      fastcgi_split_path_info ^(.+\.php)(.*)$;
      include fastcgi_params;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  }

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

在运行 docker-compose up 命令时,它正在加载项目。但是对于 css 和 js 文件显示 404。 css 文件夹路径是 /var/www/html/codeigniter/app/css/。我错过了什么?

我的文件夹结构

codeigniter
├── app
│   ├── application
│   ├── css
│   ├── img
│   ├── js
│   ├── nginx
│   ├── system
│   ├── user_guide
│   └── vendor
│   └── Dockerfile
│   └── docker-composer.yml

请帮忙。谢谢

【问题讨论】:

  • 您的 nginx 配置似乎是这里的罪魁祸首。
  • 能否请您指出所需的更改。我可以在没有 docker 的情况下在本地运行项目,并且 nginx 具有相同的配置
  • 它在本地使用 nginx 配置工作,但使用 docker 无法正常工作。我根据您提供的链接更改了 nginx conf。但无法解决问题。
  • 你能告诉我/var/log/nginx/access.log

标签: php codeigniter docker nginx


【解决方案1】:

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

docker stop $(docker ps -a -q)

docker rm $(docker ps -a -q)

为了以后修改 conf nginx 后才能看到效果,然后运行:

docker-compose up --build

清除网络浏览器的缓存、cookie 和历史记录

我的配置文件:

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;
        }


    }
}

【讨论】:

    猜你喜欢
    • 2021-06-27
    • 2021-12-04
    • 2017-03-15
    • 2020-10-02
    • 2016-08-15
    • 1970-01-01
    • 1970-01-01
    • 2018-05-27
    • 2020-04-02
    相关资源
    最近更新 更多