WordPress 与 nginx 上游不断重定向

问题描述 投票:0回答:2

出现了一种情况,运行 Nginx 的 server1 会将所有

/
位置转发到
server2
,同时将
/api
和其他一些位置保留在
server1
上。这也是为了让 SSL 保持正常运行。尝试将 WP url 从 http://test.example.com 移动到 https://example.com 可以正确加载首页,但加载
wp-admin
会给出太多重定向

服务器1 Nginx

upstream webapp_url {
    server IP:80;
}

server {
        listen 443 ssl;
        server_name www.example.com example.com;
        access_log /var/log/nginx/example.log;

        ssl_certificate /etc/nginx/ssl/example.crt;
        ssl_certificate_key /etc/nginx/ssl/server.key;
        ssl_ciphers RC4:HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers on;

        location /files/ {
                root /home;
                access_log off;
                expires max;
                if ($request_filename !~* ^.*?\.(jpg)|(png)|(gif)|(pdf)){
                        add_header Content-Disposition: "$request_filename";
                }
        }

        location / {
                # proxy_pass http://site_url/;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-For $remote_addr;
                proxy_set_header X-Forwarded-Proto https;
                proxy_cache_bypass $http_upgrade;
                proxy_set_header X-Example "1";
                proxy_pass http://webapp_url/;
        }

这可以很好地加载其他服务器,主页和链接都可以工作(尽管混合内容警告,因为我无法在管理员中更改它)。 WP

siteurl
home
均设置为新地址。

服务器2 Nginx

server {
    #listen       443 ssl;
    listen 80;
    server_name example.com test.example.com;
    client_max_body_size 30M;
    error_log /var/log/wordpress/error.log info;
    location / {
        root   /home/wordpress;
        try_files $uri $uri/ /index.php?q=$request_uri;
        index index.php  index.html index.htm;
    }

    #ssl_certificate /etc/nginx/ssl/example.crt;
    #ssl_certificate_key /etc/nginx/ssl/example.key;
    #ssl_ciphers RC4:HIGH:!aNULL:!MD5;
    #ssl_prefer_server_ciphers on;

    error_page  404              /404.html;
    location = /404.html {
        root   /usr/share/nginx/html;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    #
    location ~ \.php$ {
        root           /home/wordpress;
        fastcgi_pass   unix:/var/run/php5-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

加载

/wp-admin/
会启动无限重定向(到相同的 URL)。我也在
wp-config.php
中定义了它:

define('WP_HOME','https://example.com');
define('WP_SITEURL','https://example.com');
php wordpress http-redirect nginx
2个回答
0
投票

wp-admin
检查连接是否安全,否则会重定向到同一 URL 的
https
版本。在像您这样的情况下,这会导致重定向循环。

您需要告诉 WordPress 连接是安全的。

我注意到您在服务器 1 上设置了适当的标头:

proxy_set_header X-Forwarded-Proto $scheme;

(在您的情况下,

$scheme
的值硬连接到
https
)。

但是,您还需要将其以

HTTPS
参数的形式传递给 WordPress,其值为
on

这是通过

map
指令(在服务器 2 上)实现的:

map $http_x_forwarded_proto $https_flag {
    default off;
    https on;
}
server {
    ...
}

map
指令放置在
http
块内。您可以将其放置在
server
块上方,如上所示。有关详细信息,请参阅此文档

此外,添加另一个

fastcgi_param
HTTPS
参数传递给 WordPress(在服务器 2 上):

location ~ \.php$ {
    ...
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param HTTPS $https_flag;
    ...
}

0
投票

步骤1.

  • 在您的服务器上启用并安装所需的 PHP 版本。

步骤 2.

  • sudo yum install php-cli php-pdo php-fpm php-mysqlnd

步骤 3.

  • sudo systemctl start php-fpm

第 4 步。

  • sudo systemctl enable php-fpm

最后复制Nginx配置

server {
    root /path/to/your/wordpress;
    index index.php index.html index.htm;
    server_name site.com www.site.com;
    add_header X-Frame-Options SAMEORIGIN;
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;

    location ~ .php$ {
       try_files $uri =404;
       fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; # path might differ from OS to OS
       fastcgi_index index.php;
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
       include fastcgi_params;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.