【问题标题】:Serving WordPress static files from Docker container从 Docker 容器提供 WordPress 静态文件
【发布时间】:2017-08-11 21:42:17
【问题描述】:

我有一个 WordPress Docker 容器,并在主机上使用 nginx 作为反向代理。

在 32776 端口上为 WordPress 提供服务的 WP 容器和我的 nginx 配置在主机上是这样的:

nginx 配置(主机)

map $http_upgrade $connection_upgrade {
        default upgrade;
        '' close;
}
server {
...
    location /blog {
                    proxy_set_header Host $host;
                    proxy_set_header X-Real-IP $remote_addr;
                    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                    proxy_pass http://127.0.0.1:32776;
                    proxy_http_version 1.1;
                    proxy_set_header Upgrade $http_upgrade;
                    proxy_set_header Connection $connection_upgrade;
            }
...
}

Docker 内部的 nginx 配置

server {
    listen 0.0.0.0:80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;

    # Add index.php to the list if you are using PHP
    index index.php index.html index.htm;

    server_name _;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        error_log ... error;
        access_log ...;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

当我浏览到 domain.com/blog 时,它可以工作,但没有样式和 javascript 文件。因为当我转到 domain.com/blog/wp-content/themes/twentyseventeen/style.css 时,它会自动重定向到 domain.com/blog/wp-content/themes/twentyseventeen/ style.css/ 并根据我的访问日志,它试图找到 domain.com/blog/wp-content/themes/twentyseventeen/style.css/index.php

如何正确设置我的 Nginx 配置文件?

【问题讨论】:

    标签: wordpress docker nginx


    【解决方案1】:

    当使用 nginx 反向代理时.. 你应该把你的静态文件放在那个 nginx 容器中,比如

    location /static{
        alias /usr/src/app/static;
    }
    

    我建议使用 volumesmulti-build-staging 进行此设置

    【讨论】:

    • 由于 WordPress 的文件夹结构,静态文件不在一个特定的文件夹中。它可以在 wp-content 文件夹或 wp-includes 文件夹中,甚至可以在 wp-admin 文件夹或其他自定义文件夹中
    猜你喜欢
    • 1970-01-01
    • 2020-06-17
    • 2019-05-31
    • 2017-01-22
    • 1970-01-01
    • 1970-01-01
    • 2019-05-19
    • 2020-12-29
    • 1970-01-01
    相关资源
    最近更新 更多