Too many redirects with reverse proxy

久未见 提交于 2021-01-05 12:22:49

问题


Setup:

  • cloudflare DNS
  • docker-compose
  • nginx reverse proxy
  • wordpress fpm
  • nginx php handler

Problem

Too many redirects, like in these logs:

Attaching to wp_db, wp, wp_web
wp_db     | 2020-01-27 12:13:10+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 1:10.4.11+maria~bionic started.
wp_db     | 2020-01-27 12:13:10+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
wp_db     | 2020-01-27 12:13:10+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 1:10.4.11+maria~bionic started.
wp        | [27-Jan-2020 12:13:10 UTC] PHP Warning:  mysqli::__construct(): (HY000/2002): Connection refused in Standard input code on line 22
wp        | 
wp        | MySQL Connection Error: (2002) Connection refused
wp_db     | 2020-01-27 12:13:11 0 [Note] mysqld (mysqld 10.4.11-MariaDB-1:10.4.11+maria~bionic-log) starting as process 1 ...
wp_db     | 2020-01-27 12:13:11 0 [Note] InnoDB: Using Linux native AIO
wp_db     | 2020-01-27 12:13:11 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
wp_db     | 2020-01-27 12:13:11 0 [Note] InnoDB: Uses event mutexes
wp_db     | 2020-01-27 12:13:11 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
wp_db     | 2020-01-27 12:13:11 0 [Note] InnoDB: Number of pools: 1
wp_db     | 2020-01-27 12:13:11 0 [Note] InnoDB: Using SSE2 crc32 instructions
wp_db     | 2020-01-27 12:13:11 0 [Note] mysqld: O_TMPFILE is not supported on /tmp (disabling future attempts)
wp_db     | 2020-01-27 12:13:11 0 [Note] InnoDB: Initializing buffer pool, total size = 256M, instances = 1, chunk size = 128M
wp_db     | 2020-01-27 12:13:11 0 [Note] InnoDB: Completed initialization of buffer pool
wp_db     | 2020-01-27 12:13:11 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
wp_db     | 2020-01-27 12:13:11 0 [Note] InnoDB: 128 out of 128 rollback segments are active.
wp_db     | 2020-01-27 12:13:11 0 [Note] InnoDB: Creating shared tablespace for temporary tables
wp_db     | 2020-01-27 12:13:11 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
wp_db     | 2020-01-27 12:13:11 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
wp_db     | 2020-01-27 12:13:11 0 [Note] InnoDB: 10.4.11 started; log sequence number 20529037; transaction id 14604
wp_db     | 2020-01-27 12:13:11 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
wp_db     | 2020-01-27 12:13:11 0 [Note] Plugin 'FEEDBACK' is disabled.
wp_db     | 2020-01-27 12:13:11 0 [Note] Server socket created on IP: '::'.
wp_db     | 2020-01-27 12:13:11 0 [Note] InnoDB: Buffer pool(s) load completed at 200127 12:13:11
wp_db     | 2020-01-27 12:13:11 0 [Warning] 'proxies_priv' entry '@% root@8da7502dc057' ignored in --skip-name-resolve mode.
wp_db     | 2020-01-27 12:13:11 0 [Note] Reading of all Master_info entries succeeded
wp_db     | 2020-01-27 12:13:11 0 [Note] Added new Master_info '' to hash table
wp_db     | 2020-01-27 12:13:11 0 [Note] mysqld: ready for connections.
wp_db     | Version: '10.4.11-MariaDB-1:10.4.11+maria~bionic-log'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  mariadb.org binary distribution
wp        | [27-Jan-2020 12:13:13] NOTICE: fpm is running, pid 1
wp        | [27-Jan-2020 12:13:13] NOTICE: ready to handle connections
wp        | 192.168.48.4 -  27/Jan/2020:12:13:56 +0000 "GET /index.php/wp-admin/install.php"     302
wp_web    | 172.28.0.4 - - [27/Jan/2020:12:13:56 +0000] "GET /index.php/wp-admin/install.php HTTP/1.1" 302 5 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:71.0) Gecko/20100101 Firefox/71.0" "MYSERVERIP, 162.158.90.139"
wp        | 192.168.48.4 -  27/Jan/2020:12:13:56 +0000 "GET /index.php/wp-admin/install.php" 302
wp_web    | 172.28.0.4 - - [27/Jan/2020:12:13:56 +0000] "GET /index.php/wp-admin/install.php HTTP/1.1" 302 5 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:71.0) Gecko/20100101 Firefox/71.0" "MYSERVERIP, 162.158.90.139"
// here is the loop

Configs:

docker-compose.yml

version: '3'

services:
  wp_db:
    image: mariadb
    container_name: wp_db
    command: --transaction-isolation=READ-COMMITTED --log-bin=mysqld-bin --binlog-format=ROW
    restart: always
    volumes:
      - ./wp_db:/var/lib/mysql
    environment:
      - PUID=33
      - PGID=33
      - MYSQL_PASSWORD=wordpress # needs to be hardcoded
      - MYSQL_DATABASE=wordpress # needs to be hardcoded
      - MYSQL_USER=wordpress # needs to be hardcoded
      - MYSQL_ROOT_PASSWORD=***
    networks:
      - wordpress

  wp:
    image: wordpress:php7.4-fpm-alpine
    container_name: wp
    depends_on:
      - wp_db
    restart: always
    environment:
      - PUID=33
      - PGID=33
      - WORDPRESS_DB_USER=${MYSQL_WORDPRESS_USER}
      - WORDPRESS_DB_PASSWORD=${MYSQL_WORDPRESS_PASSWORD}
      - WORDPRESS_DB_NAME=${MYSQL_WORDPRESS_DB}
      - WORDPRESS_DB_HOST=wp_db
    volumes:
      - ./wordpress-la:/var/www/html:rw
    networks:
      - wordpress

  wp_web:
    build: ./wp_web
    container_name: wp_web
    restart: always
    volumes:
      - ./wordpress-la:/var/www/html
      - ./wp_web/nginx.conf:/etc/nginx/nginx.conf
    environment:
      - PUID=33
      - PGID=33
      - VIRTUAL_HOST=domain.com
      - LETSENCRYPT_HOST=domain.com
    depends_on:
      - wp
    networks:
      - wordpress
      - proxy-tier

networks:
  proxy-tier:

  wordpress:

nginx.conf for wp_web

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;
    real_ip_header    X-Real-IP;

    upstream php {
        server wp:9000;
    }

    server {
        server_name domain.com;

        root /var/www/html;
        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;
        }

        location ~ \.php$ {
                #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
                include fastcgi.conf;
                fastcgi_intercept_errors on;
                fastcgi_pass php;
        proxy_redirect off;
        }

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

The nginx reverse proxy creates it's configs automatically and just forwards the requests to wp_web (IIRC).

Problem:

How to fix the redirect loop? Everything else seems to work. I also have other containers running and accessible via domain2.com without any problems.

Thanks in advance ;)


回答1:


Like @Damocles stated, there has to be

$_SERVER['HTTPS'] = 'on'; in wp-config.php. The standard configuration

 if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') {
     $_SERVER['HTTPS'] = 'on';
 }

seems to be not enough.



来源:https://stackoverflow.com/questions/59931256/too-many-redirects-with-reverse-proxy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!