2012-09-03 37 views
0

我設置Nginx作爲apache2服務Wordpress安裝的代理。問題是在根URL appsrd.devmbs.com即時獲取重定向循環。當我打到服務器時,我在日誌中看到如下12-15次。Wordpress重定向站點根上的循環。 Nginx的代理阿帕奇

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運行良好。沒有重定向問題。我試圖刪除數據庫,而數據庫不可用時,根顯示建立數據庫連接的錯誤味精,因爲這是預期的行爲,但沒有重定向問題。然後,我再次創建了數據庫並運行了wordpress設置,當一切完成時,重定向問題又回來了。

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

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

任何人都有線索可能發生什麼?

回答

0

我放棄了Nginx的+阿帕奇時的Nginx + PHP-FPM

+0

作爲最佳答案,我最終做了同樣的事情。 –

6

對於可能遇到此問題的任何未來的人......

  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文件。
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!

+0

使用if指令時請小心。 [如果是邪惡](https://www.nginx.com/resources/wiki/start/topics/depth/ifisevil/) –

+0

@LucasBonner關於如何避免使用IF的建議? –

+1

我的建議是使用另一個'location /index.php {...}塊來獲取請求,然後在將請求發送到Apache之前處理請求。除此之外,我沒有時間和動力去思考它。我最終放棄了整個場景,贊成Nginx + php-fpm。 –