我有一个运行在Google Compute Engine实例上的WordPress网站,并且在Apache里面使用了SSL。
由于SSL和图像大小调整给这个实例带来了很大的压力,我build立了Google Compute Engine负载均衡器,在负载均衡器中设置了SSL并启用了CDN。
在Apache端,我禁用了*:443configuration,只剩下*:80configuration。负载均衡器现在接受端口443上的请求,并指向端口80上的实例。
<VirtualHost *:80> ServerAdmin [email protected] ServerName mysite.com ServerAlias www.mysite.com # Indexes + Directory Root. DirectoryIndex index.php index.html DocumentRoot /var/www/mysite/htdocs/ # Logfiles ErrorLog /var/www/mysite/logs/error.log CustomLog /var/www/mysite/logs/access.log combined </VirtualHost>
这工作,除了我现在得到混合内容错误,因为所有的资源仍然通过HTTP加载。
我现在尝试启用URL重写,以查看是否可以通过https访问所有内容:
<VirtualHost *:80> ServerAdmin [email protected] ServerName mysite.com ServerAlias www.mysite.com # Indexes + Directory Root. DirectoryIndex index.php index.html DocumentRoot /var/www/mysite/htdocs/ # Logfiles ErrorLog /var/www/mysite/logs/error.log CustomLog /var/www/mysite/logs/access.log combined RewriteEngine on RewriteCond %{HTTPS} off RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [L,QSA,R=permanent] </VirtualHost>
只要我启用了RewriteEngine并重写了上面的url,就可以获得无限数量的redirect。
我在Apache方面有点生疏,因为我几乎在过去的5年中一直使用NGINX,关于如何让WordPress在这个设置上正常工作的任何想法?
结果发现我的设置没什么问题,在WordPress的wp-config.php添加了以下内容
// force SSL $_SERVER['HTTPS']='on';
或者,如果你想同时运行http和https:
if ($_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https'){ $_SERVER['HTTPS']='on'; }
很明显,删除httpsredirect:
# RewriteEngine on # RewriteCond %{HTTPS} off # RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [L,QSA,R=permanent]