DigitalOcean http://example.com 上的 WordPress 無法正常運作,但 https://example.com 和 http://www.example.com 的所有其他組合都正常

DigitalOcean http://example.com 上的 WordPress 無法正常運作,但 https://example.com 和 http://www.example.com 的所有其他組合都正常

這是一個很難用谷歌搜尋的問題,甚至是標題。

我可以使用以下命令成功造訪我的 WordPress 網站:

http://www.example.com/

https://www.example.com/

https://example.com/

但這失敗了:

http://example.com/

如果您嘗試從此位址訪問該網域,您會看到一個藍色的 DigitalOcean 品牌網頁,其中有一張鯊魚的圖片和以下錯誤:“請透過 SSH 登入您的 Droplet 以設定您的 LAMP 安裝。”

我有一個運行 Ubuntu 16.04、Apache 2 和多個網域的 DigitalOcean Droplet。其中一個網域有一個從另一台主機遷移過來的 WordPress 網站。該網域的配置與我的所有其他網域相同,包括透過 LetsEncrypt 安裝的憑證。

我不認為我的問題出在證書上。我相信這個問題與 Wordpress 有關,因為我伺服器上的其他網站在所有 4 個條件下都運作得很好(http:// 和 https:// 以及 www 和非 www)

這是我的(匿名).conf 檔案:

    <VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName example.com
    ServerAlias www.example.com

    DocumentRoot /var/www/example
    <Directory />
            Options FollowSymLinks
            AllowOverride All
    </Directory>
    <Directory /var/www/example>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    <IfModule mod_dir.c>
        DirectoryIndex index.php index.pl index.cgi index.html index.xh$
    </IfModule>

RewriteEngine on
RewriteCond %{SERVER_NAME} =example.com [OR]
RewriteCond %{SERVER_NAME} =www.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

這是 /var/www/example 中我的 .htaccess 資料夾

        # BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
#test
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
# END WordPress

這是我的 DNS 設置,已貼上並匿名:

        CNAME   
ftp.example.com is an alias of example.com. 

A   
example.com directs to 159.65.36.136

CNAME
www.example.com is an alias of example.com. 

NS  
example.com directs to ns1.digitalocean.com.    

NS  
example.com directs to ns2.digitalocean.com.    

NS  
example.com directs to ns3.digitalocean.com.    

這是我的 apache2ctl -S 的輸出

            VirtualHost configuration:
*:443                  is a NameVirtualHost
        default server othersite1.com (/etc/apache2/sites-enabled/othersite1.com-le-ssl.conf:2)
        port 443 namevhost othersite1.com (/etc/apache2/sites-enabled/othersite1.com-le-ssl.conf:2)
                alias www.othersite1.com
        port 443 namevhost othersite2.co (/etc/apache2/sites-enabled/othersite2.co-le-ssl.conf:2)
                alias www.othersite2.co
        port 443 namevhost example.com (/etc/apache2/sites-enabled/example.com-le-ssl.conf:2)
                alias www.example.com
        port 443 namevhost othersite3.com (/etc/apache2/sites-enabled/othersite3.com-le-ssl.conf:2)
                alias www.othersite3.com
        port 443 namevhost othersite4.com (/etc/apache2/sites-enabled/othersite4.com-le-ssl.conf:2)
                alias www.othersite4.com
        port 443 namevhost othersite5.net (/etc/apache2/sites-enabled/othersite5.net-le-ssl.conf:2)
                alias www.othersite5.net
        port 443 namevhost othersite6.com (/etc/apache2/sites-enabled/othersite6.com-le-ssl.conf:2)
                alias www.othersite6.com
*:80                   is a NameVirtualHost
        default server example.com (/etc/apache2/sites-enabled/000-default.conf:1)
        port 80 namevhost example.com (/etc/apache2/sites-enabled/000-default.conf:1)
        port 80 namevhost othersite1.com (/etc/apache2/sites-enabled/othersite1.com.conf:1)
                alias www.othersite1.com
        port 80 namevhost othersite2.co (/etc/apache2/sites-enabled/othersite2.co.conf:1)
                alias www.othersite2.co
        port 80 namevhost example.com (/etc/apache2/sites-enabled/example.com.conf:1)
                alias www.example.com
        port 80 namevhost othersite3.com (/etc/apache2/sites-enabled/othersite3.com.conf:1)
                alias www.othersite3.com
        port 80 namevhost othersite4.com (/etc/apache2/sites-enabled/othersite4.com.conf:1)
                alias www.othersite4.com
        port 80 namevhost othersite5.net (/etc/apache2/sites-enabled/othersite5.net.conf:1)
                alias www.othersite5.net
        port 80 namevhost othersite6.com (/etc/apache2/sites-enabled/othersite6.com.conf:1)
                alias www.othersite6.com
    ServerRoot: "/etc/apache2"
    Main DocumentRoot: "/var/www/html"
    Main ErrorLog: "/var/log/apache2/error.log"
    Mutex rewrite-map: using_defaults
    Mutex ssl-stapling-refresh: using_defaults
    Mutex ssl-stapling: using_defaults
    Mutex ssl-cache: using_defaults
    Mutex default: dir="/var/lock/apache2" mechanism=fcntl 
    Mutex mpm-accept: using_defaults
    Mutex watchdog-callback: using_defaults
    PidFile: "/var/run/apache2/apache2.pid"
    Define: DUMP_VHOSTS
    Define: DUMP_RUN_CFG
    User: name="www-data" id=33
    Group: name="www-data" id=33

答案1

可能性#1

看起來您的預設 VirtualHost *:80,使用設定檔與輸出頂部的000-default.confnamevhost 相符...;example.com

VirtualHost configuration:
    default server example.com (/etc/apache2/sites-enabled/000-default.conf:1)
    port 80 namevhost example.com (/etc/apache2/sites-enabled/000-default.conf:1)

然而,你希望它在這裡匹配;

    port 80 namevhost example.com (/etc/apache2/sites-enabled/example.com.conf:1)
    alias www.example.com

可能發生的情況是,該頁面帶有文字「請透過 SSH 登入您的 Droplet 以設定您的 LAMP 安裝」。是來自 的預設內容/var/www/html

所以我會嘗試刪除/etc/apache2/sites-enabled/000-default.conf並確保它example.com與正確的 VHost 相符。

可能性#2

檢查您的設置wp-config.php,它應該與您的重定向最終指向的內容相符;

define('WP_HOME','https://example.com');
define('WP_SITEURL','https://example.com');

當 wordpress 與 apache 想要的不符時,這有時會導致 wordpress 出現問題。

進一步排除故障

另一件事是你已經貼上了配置http://example.com:80/地點;

 <VirtualHost *:80>
ServerName example.com 
...
</VirtualHost>

然而,重寫規則看起來會將每個請求退回到 https://

RewriteCond %{SERVER_NAME} =example.com [OR]
RewriteCond %{SERVER_NAME} =www.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

所以我們可能還需要查看 https 配置。

另外,您可能應該貼上與一個特定請求相關的 access.log 和 error.log 的輸出,以查看實際發生的重定向。

另外,如果還是看不出問題,可以開啟重寫日誌記錄;

 LogLevel alert rewrite:trace3

然後追蹤請求日誌;

tail -f error_log|fgrep '[rewrite:' 

相關內容