简体   繁体   English

网站根上的Wordpress重定向循环。 Nginx代理Apache

[英]Wordpress redirect loop on site root. Nginx proxy apache

I'm setting up Nginx as a proxy to apache2 serving a Wordpress installation. 我正在将Nginx设置为服务于Wordpress安装的apache2的代理。 Issue is that on the root url appsrd.devmbs.com im getting a redirect loop. 问题是,在根URL appsrd.devmbs.com上,我获得了重定向循环。 When I hit the server I see the following in the logs like 12-15 times. 当我打服务器时,我在日志中看到以下内容,例如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"

But the /wp-admin works well. 但是/ wp-admin运作良好。 No redirect issue. 没有重定向问题。 I tried deleting the database, and while the db wasn't available the root displayed a msg of Error establishing a database connection which is fine since this is the expected behavior but there was no redirect issue. 我尝试删除数据库,并且在数据库不可用时,根目录显示了建立数据库连接的错误消息,这很好,因为这是预期的行为,但是没有重定向问题。 Then I created the DB again and ran the wordpress setup, and when everything is done, the redirect issue comes back. 然后,我再次创建数据库并运行wordpress设置,完成所有操作后,重定向问题又回来了。

Beloe my nginx server conf: Beloe我的Nginx服务器conf:

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;
        }
}

The url is appsrd.devmbs.com, appsrd.devmbs.com/wp-admin works fine. 网址为appsrd.devmbs.com,appsrd.devmbs.com / wp-admin可以正常运行。

Anyone have a clue what might be happening? 任何人都有线索可能会发生什么?

For any future person that may encounter this problem… 对于将来可能遇到此问题的任何人……

  1. Going this route isn't really the most practical way of doing this. 走这条路线实际上并不是最实用的方法。
  2. I was determined to make it work for the sake of doing it. 我下定决心要做到这一点。

For the purpose of my experiment I wanted Nginx to display any and all existing non-PHP files and to proxy the PHP files and the dynamic URL's to Apache for WordPress to do its thing. 出于实验目的,我希望Nginx显示所有现有的非PHP文件,并将PHP文件和动态URL代理给Apache,以供WordPress完成其工作。 I also wanted WordPress to be able to use the normal .htaccess file. 我还希望WordPress能够使用普通的.htaccess文件。

The problem with the initial code that Luis' original posted is that Nginx is explicitly declaring the use of index.php because it's the only index that is given in a WordPress environment. Luis最初发布的初始代码的问题在于Nginx明确声明使用index.php,因为它是WordPress环境中唯一给出的索引。 The result is that when you go to "domain.com/" Nginx sends that to Apache looking exactly like "domain.com/index.php". 结果是,当您转到“ domain.com/”时,Nginx会将其发送给Apache,就像“ domain.com/index.php”一样。 When WordPress receives "domain.com/index.php" it automatically shortens and redirects you to "domain.com/"; 当WordPress收到“ domain.com/index.php”时,它会自动缩短并重定向到“ domain.com/”; thus you end up with that loop. 因此,您最终会遇到该循环。

The best work around (for the determined) that works for a WordPress environment is to just send any directories to WordPress as well. 对于WordPress环境,最好的解决方法(针对确定的)是将任何目录也发送到WordPress。 The downside to this setup is that it will ignore any indexes that aren't index.php. 此设置的缺点是它将忽略不是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;
    }   
}

If you noticed the contents of the file named *proxy_apache* which is included several times above is, 如果您注意到上面多次包含的名为* 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;

Again, not necessarily the most practical solution but it does have the advantages of, 同样,不一定是最实用的解决方案,但它确实具有以下优点:

  1. Displaying any none-PHP files with Nginx instead of having to define an explicit list of static files in a regular expression. 使用Nginx显示任何非PHP文件,而不必在正则表达式中定义静态文件的显式列表。
  2. Using WordPress's beloved .htaccess file; 使用WordPress最受欢迎的.htaccess文件; though you're not likely to change the .htaccess file after the initial setup. 尽管您在初始设置后不太可能更改.htaccess文件。

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

I wanted the same thing Lucas did and I got it to work. 我想要卢卡斯做的同样的事情,并且让它起作用。 Here is a basic example of my congiration. 这是我的陪伴的基本示例。

# 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

The key is the if statement inside my @proxy location. 关键是@proxy位置内的if语句。 I check for a $uri and rewrite the request URI back to / using the break flag. 我检查$ uri,然后使用break标志将请求URI改写回/。 This avoids the redirect loop. 这样可以避免重定向循环。

What's weird is that the problem only occurred on the root index, not on subdirectory indexes. 奇怪的是,该问题仅发生在根索引上,而不发生在子目录索引上。 I appreciate any feedback on my configuration. 感谢您对我的配置的任何反馈。 It might have problems I have yet to discover, but as of now it's providing me with what I need for WordPress deployment. 它可能有我尚未发现的问题,但到目前为止,它为我提供了WordPress部署所需的功能。

GL! GL!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM