1

我将 Nginx 设置为 apache2 的代理,为 Wordpress 安装服务。问题是在根 url appsrd.devmbs.com 上我得到了一个重定向循环。当我点击服务器时,我在日志中看到了 12-15 次以下内容。

127.0.0.1 - - [03/Sep/2012:12:29:25 +0000] "GET /index.php HTTP/1.0" 301 529 "http://appsrd.devmbs.com/wp-admin/options-general.php" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_0) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1"

但是 /wp-admin 运行良好。没有重定向问题。我尝试删除数据库,虽然 db 不可用,但根显示了错误建立数据库连接的消息,这很好,因为这是预期的行为,但没有重定向问题。然后我再次创建了数据库并运行了 wordpress 设置,当一切都完成后,重定向问题又回来了。

Beloe 我的 nginx 服务器配置:

server {
        listen       80 default_server;
        server_name  appsrd.devmbs.com;
        root /home/ubuntu/projecs/APPS-RD;
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /home/ubuntu/projects/APPS-RD;
            index  index.html index.htm index.php;
        }

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

        location ~ \.php$ {
        proxy_pass   http://127.0.0.1:3000;
        proxy_buffering on;
        proxy_buffers 12 12k;
        proxy_set_header Host $host;
        }
}

网址是appsrd.devmbs.com,appsrd.devmbs.com/wp-admin 工作正常。

任何人都知道可能会发生什么?

4

3 回答 3

9

对于任何可能遇到此问题的未来人......</p>

  1. 走这条路并不是最实际的方法。
  2. 我决心让它发挥作用。

出于我实验的目的,我希望 Nginx 显示任何和所有现有的非 PHP 文件,并将 PHP 文件和动态 URL 代理到 Apache 以便 WordPress 完成它的工作。我还希望 WordPress 能够使用普通的 .htaccess 文件。

Luis 最初发布的初始代码的问题在于 Nginx 明确声明使用 index.php,因为它是 WordPress 环境中提供的唯一索引。结果是,当您转到“domain.com/”时,Nginx 会将其发送给 Apache,看起来与“domain.com/index.php”一模一样。当 WordPress 收到“domain.com/index.php”时,它会自动缩短并将您重定向到“domain.com/”;因此你最终得到了那个循环。

适用于 WordPress 环境的最佳解决方法(对于确定的)是将任何目录也发送到 WordPress。此设置的缺点是它将忽略任何不是 index.php 的索引。

server {
        listen       80;
    server_name  domain.com;
    root   /path/to/web/root/;


    # Proxy anything that isn't a file.
    location / {
        try_files $uri @apache;
    }

    # Proxy directories (This fixes the loop)
    # This will kill any other indexes like index.html if they're not explicitly used in the URL.
    location ~[^?]*/$ {
        include proxy_apache;
    }

    # Proxy any .php file.
    location ~ \.php$ {
        include proxy_apache;
    }

        # Proxy to apache, used by location / {...}
    location @apache {
        include proxy_apache;
    }

    # this will prevent files like .htaccess .htpassword .secret etc from being served
    # You can remove the log directives if you wish to
    # log any attempts at a client trying to access a hidden file
    location ~ /\. {
        deny all;
        access_log off;
        log_not_found off;
    }   
}

如果您注意到上面多次包含的名为 *proxy_apache* 的文件的内容,

proxy_set_header X-Real-IP  $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:8080;

同样,不一定是最实用的解决方案,但它确实具有以下优点,

  1. 使用 Nginx 显示任何非 PHP 文件,而不必在正则表达式中定义显式静态文件列表。
  2. 使用 WordPress 钟爱的 .htaccess 文件;尽管您不太可能在初始设置后更改 .htaccess 文件。
于 2013-10-30T12:53:57.027 回答
2

使用 Nginx + php-fpm 时我放弃了 Nginx + Apache

于 2012-09-04T22:39:50.463 回答
0

我想要卢卡斯做的同样的事情,我得到了它的工作。这是我的一个基本例子。

# A basic configuration with reverse proxy to apache2

server {
  listen 80;

  server_name someurl.com www.someurl.com;

  root /var/www/someurl.com/html/;

  index index.php index.html index.html;

  access_log /var/log/nginx/someurl.com.access.log;
  error_log /var/log/nginx/someurl.com.error.log;

  # send anything with .php in it to apache
  location ~* \.php$ {
    try_files /dev/null @proxy;
  }

  # for everything else, 
  location / {
    try_files $uri $uri/ @proxy;
    error_page 404 = @proxy;
  }

  # Deny access to all dotfiles
  location ~ /\. {
    deny all;
  }

  # Named location for reverse proxy
  location @proxy {
    if ( $uri = /index.php ) {
      rewrite /index.php / break;
    }
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header Host $host;
    proxy_pass http://127.0.0.1:8080;
  }
}

# need to add https support

关键是我的@proxy 位置中的 if 语句。我检查 $uri 并使用中断标志将请求 URI 重写回 / 。这避免了重定向循环。

奇怪的是问题只发生在根索引上,而不是子目录索引上。感谢您对我的配置提供任何反馈。它可能有我尚未发现的问题,但到目前为止,它为我提供了 WordPress 部署所需的东西。

GL!

于 2013-12-10T22:24:51.830 回答