docker wordpress 的 Nginx 配置:php8.0-fpm-alpine

docker wordpress 的 Nginx 配置:php8.0-fpm-alpine

我的目标是在主机 Nginx 配置中创建一个服务器块,作为 wordpress php docker 容器的 fastcgi 前端。

也就是说,nginx 服务器不在容器中,我也不希望它在容器中。我也不太在意 wordpress 站点的静态文件是否以某种方式直接由 Nginx 提供服务——如果让容器为它们提供服务更容易,那么这就是我想要做的。

我有一个使用 wordpress:php8.0-fpm-alpine 镜像运行的 docker 容器。到目前为止,我只尝试了 Nginx 提供的服务器块:

upstream php {
        server unix:/tmp/php-cgi.socket;
        server 127.0.0.1:9000;
}
server {
        ## Your website name goes here.
        server_name domain.tld;
        ## Your only path reference.
        root /var/www/wordpress;
        ## This should be in your http block and if it is, it's not needed here.
        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 / {
                # This is cool because no php is touched for static content.
                # include the "?$args" part so non-default permalinks doesn't break when using query string
                try_files $uri $uri/ /index.php?$args;
        }

        location ~ \.php$ {
                #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
                include fastcgi_params;
                fastcgi_intercept_errors on;
                fastcgi_pass php;
                #The following parameter can be also included in fastcgi_params file
                fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        }

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

以及链接的建议示例:https://gist.github.com/md5/d9206eacb5a0ff5d6be0#file-wordpress-fpm-conf

server { 
  listen 80; 
  server_name localhost; 
  root /var/www/html; 

  index index.php; 

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

  rewrite /wp-admin$ $scheme://$host$uri/ permanent;

  location ~ [^/]\.php(/|$) { 
    fastcgi_split_path_info ^(.+?\.php)(/.*)$;
    if (!-f $document_root$fastcgi_script_name) {
      return 404;
    }

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

    fastcgi_pass   fpm:9000;
    fastcgi_index  index.php; 
  } 
}

在这两个示例中,我都根据需要替换了我的域和 php 服务器主机名/套接字。但是,两者都明显使用对 wordpress 静态文件的直接访问。我该如何转发这些请求?这能解决问题吗?我实际上并不知道 fpm-alpine 映像是否提供其静态内容,但我认为它确实提供。

答案1

PHP-FPM 仅提供 PHP 内容。大多数示例都提供了一个-v标志,用于将主机上的路径/卷映射到 docker 容器内的路径

第一个问题是“docker 容器是否响应非 CGI 请求”,这个问题的答案又回到“我也不关心 wordpress 网站的静态文件是否以某种方式直接由 Nginx 提供服务”

docker 容器是在 nginx 主机上运行还是在远程主机上运行?如果是本地的,那么在第二个示例中更改servernameroot是可行的方法。如果内容在远程服务器上,那么您需要使用 NFS 之类的东西与 Nginx 服务器共享内容。还有其他方法,但它们是最简单的

编辑1

我更改了服务器名称和根目录,并能够访问文件。但我有点担心我是否做得对。

那是对的

有没有办法确保使用较新版本的docker镜像总是覆盖卷?

Docker 将使用相关主机上的镜像。您无需在此处提供任何详细信息,但通常需要执行docker stop <name>&& docker pull <new image>&&docker run <new image> 请注意,镜像可能是latest或其他滚动标签。

EDIT2(来自评论回复:卷会被覆盖吗)

这取决于您的运行方式。-v选项未涵盖的所有内容都将丢失。如果我没记错的话,wordpress 有一个wp-content,可能是一个wp-includes文件夹和 sql 配置。所以我希望有 2 个-v选项将本地文件夹映射到容器中,以及一堆 -e或一个--env-file

这些卷和环境未涵盖的所有内容都将被新容器替换

相关内容