You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker Compose部署WordPress样式不加载问题求助

排查与解决WordPress CSS不生效问题

1. 修正Nginx配置文件

你的Nginx若未正确处理静态文件或PHP请求,会导致CSS加载异常。将./nginx.conf替换为以下配置,确保静态文件直接由Nginx返回,PHP请求转发到WordPress FPM容器:

user nginx;
worker_processes auto;
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;
    keepalive_timeout  65;

    server {
        listen 80;
        server_name localhost; # 替换为你的访问IP或域名
        root /var/www/html;
        index index.php index.html index.htm;

        # 直接返回静态资源,避免转发到PHP
        location ~* \.(css|js|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
            expires 30d;
            add_header Cache-Control "public, no-transform";
        }

        # 支持WordPress固定链接规则
        location / {
            try_files $uri $uri/ /index.php?$args;
        }

        # 转发PHP请求到WordPress FPM容器
        location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass wordpress:9000; # 使用docker-compose服务名wordpress
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param PATH_INFO $fastcgi_path_info;
        }
    }
}

2. 验证静态文件MIME类型

通过浏览器开发者工具的网络标签,查看CSS请求的Content-Type字段,正常应为text/css。若显示为application/octet-stream,说明Nginx未正确加载mime.types文件,上述配置已包含该文件引入,可忽略额外操作。

3. 同步WordPress站点URL设置

若访问地址与WordPress配置的URL不一致,会导致资源路径错误:

  • 若能进入后台:到设置→常规,将**WordPress地址(URL)和站点地址(URL)**改为你当前的访问地址(如http://你的IP)。
  • 若无法进入后台:通过数据库修改
    1. 进入MariaDB容器:docker exec -it wordpress-sql mysql -uroot -proot
    2. 切换数据库:use wordpress;
    3. 执行更新语句:
      UPDATE wp_options SET option_value = 'http://你的IP' WHERE option_name = 'siteurl';
      UPDATE wp_options SET option_value = 'http://你的IP' WHERE option_name = 'home';
      

4. 调整文件权限

偶尔会出现权限异常导致资源无法正常读取,执行以下命令修复:

docker exec -it wordpress-main chown -R www-data:www-data /var/www/html

5. 重启服务

修改配置或权限后,重启所有容器使设置生效:

docker-compose down && docker-compose up -d

内容的提问来源于stack exchange,提问作者Anirudh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 00:38:15