【问题标题】:How to run Docker Wordpress through php-fpm on nginx?如何在 nginx 上通过 php-fpm 运行 Docker Wordpress?
【发布时间】:2020-10-24 22:24:27
【问题描述】:

嘿,我尝试了不同的方法,但它们将我引导到 nginx 起始页,错误 403 或错误 404。我目前正在运行没有 php-fpm 的 Wordpress,但它效果不佳,所以我想更改它,但我只做的时候遇到了问题。

我的 Wordpress Docker-compose(无 db):

  wordpress:
    container_name: wordpress
    links:
      - wordpress-db
    depends_on:
      - wordpress-db
    image: wordpress:php7.4-fpm
    ports: 
      - 9000:9000
    restart: always
    volumes:
      - /media/storage/wordpress:/var/www/html
      - .secrets/uploads.ini:/usr/local/etc/php/conf.d/uploads.ini
    networks:
      - wordpress
    environment:
      - TZ=Europe/Berlin
    env_file:
      - .secrets/wordpress.env

我的 nginx 撰写文件:

services:
  nginx:
    restart: always
    container_name: nginx
    image: cptdaydreamer/nginx:latest
    ports:
      - 80:80
      - 443:443
      - 4443:4443
    volumes:
      - /media/storage/log:/var/log/nginx
      - /etc/ssl:/etc/ssl
    external_links:
      - wordpress
    environment:
      - TZ=Europe/Berlin
    build:
      context: /media/storage/nginx
      dockerfile: Dockerfile
    networks:
      - nextcloudpi
      - wordpress
      - matomo
      - php

现在是重要的部分。我的 nginx 配置:

http {

  server_names_hash_bucket_size 64;

  access_log off;
  error_log /var/log/nginx/error.log;
  
  autoindex_localtime on;
  #more_clear_headers Server;

  ssl_certificate           /etc/ssl/tld.pem;
  ssl_certificate_key       /etc/ssl/tld.key;
  
  gzip            on;
  gzip_min_length 1000;
  gzip_proxied    expired no-cache no-store private auth;
  gzip_types      text/plain text/css text/xml application/javascript application/json application/xml application/rss+xml image/svg+xml;
  gzip_disable    "MSIE [1-6]\.";
  
  proxy_send_timeout 180s;
  proxy_read_timeout 180s;
  fastcgi_send_timeout 180s;
  fastcgi_read_timeout 180s;

  server {
    listen 80 default_server;
    server_name _;
    server_name_in_redirect off;
    
    location / {
        return 404;
    }
  }

  server {
    listen 80;
    listen [::]:80;
    server_name tld.de cloud.tld.de www.tld.de stats.tld.de;
    return 301 https://$host$request_uri;
  }
  
  # WordPress
  
  server {
    server_name tld.de www.tld.de ts.tld.de;
    
    #access_log /var/log/nginx/wordpress-access.log;
    error_log /var/log/nginx/wordpress-error.log;
    
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    
    client_max_body_size 500m;
    underscores_in_headers on;
    
    root /var/www/html; 
    index index.php;

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

    location ~ \.php$ {
       try_files $uri =404;
       fastcgi_split_path_info ^(.+\.php)(/.+)$;
       fastcgi_pass wordpress:9000;
       fastcgi_index index.php;
       include fastcgi_params;
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
       fastcgi_param PATH_INFO $fastcgi_path_info;
    }
  }

我得到的错误例如是这样的:/var/www/html 的目录索引被禁止。

如果我删除 $uri/,例如,我会收到错误 404。我需要做什么才能让我的实例运行?但我是对的,我可以使用我现有的 wordpress 实例,并且只有访问它的方式会改变?

【问题讨论】:

    标签: wordpress docker nginx


    【解决方案1】:

    您正在监听 ssl,但您没有提供证书路径?

    您是否碰巧使用任何外部 ssl 提供商,例如 Cloudflare?如果是,则您必须侦听端口 80(Cloudflare 上的默认端口)或在 Cloudflare 设置以获取 https 站点,在这种情况下,您还需要提供自己的证书路径。

    其次,在您的 location / 块中,我有这个 try_files $uri $uri/ /index.php?url=$uri; 以及其他一些设置

        location / {
          try_files $uri $uri/ /index.php?url=$uri;
        }
        location ~ \.(php)$ {
          fastcgi_pass 127.0.0.1:9000;
          fastcgi_index  index.php;
          fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
          include fastcgi_params;
          fastcgi_read_timeout 3000;
          proxy_connect_timeout 3000s;
          proxy_send_timeout   3000;
          proxy_read_timeout   3000;
        }
    

    fastcgi_pass 127.0.0.1:9000 会有所不同,在您的情况下是 wordpress:9000

    【讨论】:

    • 我使用的是 Cloudflare SSL 证书,没错。我已经在 http 部分定义了它。我的其他容器(如 matomo、nextcloud 和其他容器)与配置在 443 上侦听时配合良好。那么在端口 80 上侦听到底是什么意思?那么在哪里和什么需要在端口 80 上监听呢?我会将 http 部分添加到问题中。
    • 如果我删除了索引和根目录,我只会得到 Welcome to nginx!。否则我会得到“/usr/share/nginx/html/”的目录索引被禁止,这也是错误的。
    • 我明白了,www-data 目录 /var/www/html 的用户的 uid 是什么?您可以尝试链接到此文件夹 root /usr/share/nginx/html/,该文件夹应归 www-data 所有,uid 为 82
    • 你走错了路。我错过了教程上的一些内容,现在我进入了起始页,但是 13:每个图像和 wp-content 内容的权限被拒绝。
    猜你喜欢
    • 1970-01-01
    • 2022-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-25
    • 2011-11-10
    • 2021-04-02
    相关资源
    最近更新 更多