这是我的问题
我需要在子目录网址(“https://test.com/blog/”)上安装wordpress应用程序。
由于我的整个环境都在Docker上运行,因此我决定对wordpress应用执行相同的操作。
为了尽可能简单地启动它,我将wordpress图像添加到了docker-compose.yml中,并使用nginx将一个子域(“http://blog.test.com”未使用https)放置在应用程序上反向代理。
它运作良好,我像这样安装了我的wordpress。
现在,我正在尝试将其迁移到我的最初需求,我的问题是,一切正常,除非我无法访问管理url(https://test.com/blog/admin),因为它保留了https://test.com/blog/wp-admin上的重定向,并且得到了“重定向太多”错误。
这是我的nginx配置:

server {
  listen 80 default_server;
  listen [::]:80 default_server;
  server_name test.com;
  return 301 https://$server_name$request_uri;
}

server {
    listen 443 ssl default_server;
    listen [::]:443 ssl default_server;
    server_name test.com;

    error_log /var/log/nginx/test_error.log;
    access_log /var/log/nginx/test_access.log;

    location /blog/ {
        auth_basic "Restricted";
        auth_basic_user_file /etc/nginx/test-htpasswd;
        proxy_pass http://127.0.0.1:8090;
        proxy_http_version 1.1;
        proxy_buffering off;
        proxy_read_timeout    90;
        proxy_connect_timeout 90;
        proxy_redirect        off;
        proxy_set_header Host $host;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $proxy_connection;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Ssl $proxy_x_forwarded_ssl;
        proxy_set_header X-Forwarded-Port 443;

    # Mitigate httpoxy attack (see README for details)
           proxy_set_header Proxy "";
    }

    location / {
        auth_basic "Restricted";
        auth_basic_user_file /etc/nginx/test-htpasswd;
        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:8082;
    }

    ssl_certificate /etc/letsencrypt/live/test.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/test.com/privkey.pem; # managed by Certbot

    add_header X-Robots-Tag "noindex, nofollow, nosnippet, noarchive";
}


我还在wordpress容器中的wp-config.php文件顶部添加了以下内容:
define('FORCE_SSL_ADMIN', true);

if($_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https'){

    $_SERVER['HTTPS'] = 'on';
    $_SERVER['SERVER_PORT'] = 443;
}

define('WP_HOME','https://test.com/blog/');
define('WP_SITEURL','https://test.com/blog/');

我注意到的是,如果我尝试通过执行以下操作来打印 $ _SERVER 变量

在wp-config.php中,没有任何nginx代理变量。
这是输出:
array(42) {
["REDIRECT_STATUS"]=> string(3) "200"
["HTTP_HOST"]=> string(15) "test.com"
["HTTP_X_REAL_IP"]=> string(15) "itsmyip"
["HTTP_CONNECTION"]=> string(5) "close"
["HTTP_CACHE_CONTROL"]=> string(9) "max-age=0"
["HTTP_UPGRADE_INSECURE_REQUESTS"]=> string(1) "1"
["HTTP_USER_AGENT"]=> string(104) "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36"
["HTTP_SEC_FETCH_USER"]=> string(2) "?1"
["HTTP_ACCEPT"]=> string(124) "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
["HTTP_SEC_FETCH_SITE"]=> string(4) "none"
["HTTP_SEC_FETCH_MODE"]=> string(8) "navigate"
["HTTP_ACCEPT_ENCODING"]=> string(17) "gzip, deflate, br"
["HTTP_ACCEPT_LANGUAGE"]=> string(35) "fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7"
["HTTP_COOKIE"]=> string(547) "_lfa=eyJYYnAxb2FFR2RSazdFZFZqIjoiTEYxLjEuNGFiNTNhYjI1ZWMwNDE3MS4xNTk1OTM2MTk5MjIyIn0%3D; _ga=GA1.2.1198854717.1595936200; _gid=GA1.2.2083987609.1595936200; _hjid=51fe1835-b72b-4957-873f-dc4147a455fc; _hjIncludedInSample=1; _fbp=fb.1.1595936200807.1293959861; __zlcmid=zPjJja34jXiPWc; PHPSESSID=bo5jv9md5j6kmtomigjgi2bdnl; REMEMBERME=VGhpdmVvXENvcmVCdW5kbGVcRW50aXR5XFVzZXI6Y21Gd2FHRmxiQzV3WlhKamFHVmpLM1JsYzNSaGNtTm9hVzFsWkdWQVoyMWhhV3d1WTI5dDoxNjI3NDcyMjI1Ojg5MWY3ZGM1M2QyMmQyNzdiNWI3MjQ5NTY4NThkZWE5MWIxYmU2NjUzM2EzMmQ5Yzc5MzFjOWJmM2E4ZDliNjk%3D" ["PATH"]=> string(60) "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
["SERVER_SIGNATURE"]=> string(76) "
Apache/2.4.38 (Debian) Server at test.com Port 80
"
["SERVER_SOFTWARE"]=> string(22) "Apache/2.4.38 (Debian)"
["SERVER_NAME"]=> string(15) "test.com"
["SERVER_ADDR"]=> string(11) "itsmyip"
["SERVER_PORT"]=> string(2) "80"
["REMOTE_ADDR"]=> string(15) "itsmyip"
["DOCUMENT_ROOT"]=> string(13) "/var/www/html"
["REQUEST_SCHEME"]=> string(4) "http"
["CONTEXT_PREFIX"]=> string(0) "
" ["CONTEXT_DOCUMENT_ROOT"]=> string(13) "/var/www/html"
["SERVER_ADMIN"]=> string(19) "webmaster@localhost"
["SCRIPT_FILENAME"]=> string(23) "/var/www/html/index.php"
["REMOTE_PORT"]=> string(5) "38136"
["REDIRECT_URL"]=> string(12) "/blog/"
["GATEWAY_INTERFACE"]=> string(7) "CGI/1.1"
["SERVER_PROTOCOL"]=> string(8) "HTTP/1.0"
["REQUEST_METHOD"]=> string(3) "GET"
["QUERY_STRING"]=> string(0) ""
["REQUEST_URI"]=> string(12) "/blog/"
["SCRIPT_NAME"]=> string(10) "/index.php"
["PHP_SELF"]=> string(10) "/index.php"
["REQUEST_TIME_FLOAT"]=> float(1595941685.61)
["REQUEST_TIME"]=> int(1595941685)
["argv"]=> array(0) { }
["argc"]=> int(0)
你有什么主意吗 ?

最佳答案

其实我找到了解决方法!
我的nginx配置出错,如果有人感兴趣,这是我的最终工作配置:
nginx.conf文件(wordpress bloc):

location /blog/ {
        proxy_pass http://127.0.0.1:8093/;
        proxy_http_version 1.1;
        proxy_buffering off;
        proxy_read_timeout    90;
        proxy_connect_timeout 90;
        proxy_redirect        off;
        proxy_set_header Host $host;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Port 443;
        proxy_set_header Proxy "";
    }
这是我在wordpress容器的wp_config.php文件顶部添加的内容:
define('FORCE_SSL_ADMIN', true);

$_SERVER['REQUEST_URI'] = str_replace("/wp-admin/", "/blog/wp-admin/",  $_SERVER['REQUEST_URI']);

if($_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https'){

    $_SERVER['HTTPS'] = 'on';
    $_SERVER['SERVER_PORT'] = 443;
}

define('WP_HOME','https://test.com/blog/');
define('WP_SITEURL','https://test.com/blog/');
干杯!

关于php - 使用SSL的nginx反向代理背后的Docker上的Wordpress,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/63135042/

10-16 21:33