Nginx 重定向循环与 Wordpress

Nginx 重定向循环与 Wordpress

我在另一台服务器上有一个 rails 应用程序和一个基于 wordpress 的 cms。我试图将它们托管在同一个域下。我希望所有非 cms 流量都重定向到 https。并且所有 /cms/ 流量都应转到 http 并代理到 wordpress 服务器。但 /cms/ 目录失败,陷入无限循环。我尝试了许多替代方案,但无法解决问题。您能确定这个 nginx 配置上的问题吗?

upstream example {
   server unix:/tmp/unicorn.example.sock fail_timeout=0;
 }

 server {
   listen 80;
   server_name www.example.com example.com;
   root /home/deployer/apps/example/current/public;

   set $redirect true;
   if ($request_uri ~ ^/cms/(/.*)$) {
     set $redirect false;
   }

   location /cms/ {
            proxy_pass  http://cms.example.com:80/;
            proxy_set_header Host cms.example.com;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto https;
            proxy_redirect    off;
   }

   if ($redirect = true) {
      rewrite ^ https://www.example.com$request_uri? permanent;
   }

   location ^~ /assets/ {
     gzip_static on;
     expires max;
     add_header Cache-Control public;
   }

   try_files $uri/index.html $uri @example;
   location @example {
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     proxy_set_header Host $http_host;
     proxy_redirect off;
     proxy_pass http://example;
   }

   error_page 500 502 504 /500.html;
   client_max_body_size 4G;
   keepalive_timeout 10;

   if (-f $document_root/system/maintenance.html) {
     return 503;
   }

   error_page 503 @maintenance;
   location @maintenance {
     rewrite  ^(.*)$  /system/maintenance.html last;
     break;
   }

}

server {
    listen 443;
    server_name www.example.com example.com;
    root /home/deployer/apps/example/current/public;

    ssl on;
    ssl_ciphers RC4:HIGH:!aNULL:!MD5:!kEDH;
    ssl_session_cache shared:SSL:10m;
    ssl_prefer_server_ciphers on;
    ssl_certificate /home/deployer/certs/server.crt;
    ssl_certificate_key /home/deployer/certs/server.key;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

    error_log /home/deployer/apps/example/current/log/nginx_error.log;

    location ^~ /assets/ {
        gzip_static on;
        expires max;
        add_header Cache-Control public;
    }

    location /cms/ {
        rewrite ^ http://www.example.com$request_uri? permanent;
    }

    try_files $uri/index.html $uri @example;
    location @example {
        proxy_set_header  X_FORWARDED_PROTO https;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://example;
    }
    error_page 500 502 504 /500.html;
    client_max_body_size 4G;
    keepalive_timeout 10;

    if (-f $document_root/system/maintenance.html) {
      return 503;
    }

    error_page 503 @maintenance;
    location @maintenance {
      rewrite  ^(.*)$  /system/maintenance.html last;
      break;
    }
}

答案1

这很可能是问题所在:

if ($request_uri ~ ^/cms/(/.*)$) {
    set $redirect false;
}

仅当 URI 类似 时才会匹配/cms//anything

我建议从块中删除if语句和set语句server 80并使用server如下块:

server {
    listen 80;
    server_name www.example.com example.com;
    root /home/deployer/apps/example/current/public;

    location / {
        rewrite ^ https://www.example.com$request_uri? permanent;
    }

    location /cms {
        proxy_pass  http://cms.example.com:80/;
        proxy_set_header Host cms.example.com;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto https;
        proxy_redirect    off;
    }
}

我删除了其他块,因为根据您的意愿,location它们与该块无关。server 80

相关内容