HTTP 到 HTTPS 重定向在 Apache+Varnish for WordPress 中不起作用
HTTP to HTTPS redirect not working in Apache+Varnish for WordPress
我面临的问题是 http 到 https 重定向不起作用。我当前的设置是清漆+apache。我正在使用 apache 来终止 SSL。遵循本指南 (https://bash-prompt.net/guides/apache-varnish/) 来执行此操作(也遵循该指南中的“ERR_TOO_MANY_REDIRECTS 错误”部分,因为我遇到了太多重定向错误)。
Varnish 在端口 80 上 运行。Apache 在端口 8181 上 运行 并终止 SSL。尝试在 htaccess 中添加重定向规则,但它不起作用。添加以下行到 wp-config
if ($_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
$_SERVER['HTTPS'] = 'on';
}
if ( !isset( $_SERVER['HTTPS'] ) ) {
$_SERVER['HTTPS'] = 'on';
}
define( 'WP_HOME', 'https://example.com' );
define( 'WP_SITEURL', 'https://example.com' );
我当前的 varnish vcl 配置:
vcl 4.0;
# Default backend definition. Set this to point to your content server.
backend default {
.host = "127.0.0.1";
.port = "8181";
}
sub vcl_recv {
if (req.url ~ "wp-admin|wp-login") {
return (pass);
}
sub vcl_backend_response {
# Happens after we have read the response headers from the backend.
#
# Here you clean the response headers, removing silly Set-Cookie headers
# and other mistakes your backend does.
}
sub vcl_deliver {
# Happens when we have all the pieces we need, and are about to send the
# response to the client.
#
# You can do accounting or modifying the final object here.
}
我的 htaccess 配置:
SetEnvIf X-Forwarded-Proto "https" HTTPS=on
Header append Vary: X-Forwarded-Proto
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP:X-Forwarded-Proto} !https [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
几乎每个教程我都试过了。甚至尝试通过 varnish vcl 配置本身将 HTTP 重定向到 HTTPS,但似乎没有任何效果。请帮忙!
将所有用户从 HTTP 重定向到 HTTPS
您可以使用以下任何解决方案。您只需要使用其中一种或全部三种,由您决定。
1. PHP解决方案
if(!isset($_SERVER['HTTPS']) || strtolower($_SERVER['HTTPS']) != 'on') {
header('Location: https://www.yourdomain.com');
exit;
}
2。 Apache .htaccess 解决方案
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.yourdomain.com/ [R,L]
3。 Nginx 解决方案
server {
listen 80 default_server;
server_name _;
return 301 https://$host$request_uri;
}
1. Varnish / Wordpress 配置 - 手动设置解决方案
https://betterprogramming.pub/set-up-varnish-cache-for-wordpress-8e2ac92ce347
2。 Varnish / Wordpress config - Wordpress 插件解决方案
对于另一个 php 解决方案,您也可以在 public/index.php 的开头使用它
这对你的情况应该更有效
if($_SERVER['SERVER_PORT'] != 443) {
header('Location: https://www.yourdomain.com');
exit;
}
echo 'Your current server port is = '.$_SERVER['SERVER_PORT'].'<br />';
我面临的问题是 http 到 https 重定向不起作用。我当前的设置是清漆+apache。我正在使用 apache 来终止 SSL。遵循本指南 (https://bash-prompt.net/guides/apache-varnish/) 来执行此操作(也遵循该指南中的“ERR_TOO_MANY_REDIRECTS 错误”部分,因为我遇到了太多重定向错误)。 Varnish 在端口 80 上 运行。Apache 在端口 8181 上 运行 并终止 SSL。尝试在 htaccess 中添加重定向规则,但它不起作用。添加以下行到 wp-config
if ($_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
$_SERVER['HTTPS'] = 'on';
}
if ( !isset( $_SERVER['HTTPS'] ) ) {
$_SERVER['HTTPS'] = 'on';
}
define( 'WP_HOME', 'https://example.com' );
define( 'WP_SITEURL', 'https://example.com' );
我当前的 varnish vcl 配置:
vcl 4.0;
# Default backend definition. Set this to point to your content server.
backend default {
.host = "127.0.0.1";
.port = "8181";
}
sub vcl_recv {
if (req.url ~ "wp-admin|wp-login") {
return (pass);
}
sub vcl_backend_response {
# Happens after we have read the response headers from the backend.
#
# Here you clean the response headers, removing silly Set-Cookie headers
# and other mistakes your backend does.
}
sub vcl_deliver {
# Happens when we have all the pieces we need, and are about to send the
# response to the client.
#
# You can do accounting or modifying the final object here.
}
我的 htaccess 配置:
SetEnvIf X-Forwarded-Proto "https" HTTPS=on
Header append Vary: X-Forwarded-Proto
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP:X-Forwarded-Proto} !https [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
几乎每个教程我都试过了。甚至尝试通过 varnish vcl 配置本身将 HTTP 重定向到 HTTPS,但似乎没有任何效果。请帮忙!
将所有用户从 HTTP 重定向到 HTTPS
您可以使用以下任何解决方案。您只需要使用其中一种或全部三种,由您决定。
1. PHP解决方案
if(!isset($_SERVER['HTTPS']) || strtolower($_SERVER['HTTPS']) != 'on') {
header('Location: https://www.yourdomain.com');
exit;
}
2。 Apache .htaccess 解决方案
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.yourdomain.com/ [R,L]
3。 Nginx 解决方案
server {
listen 80 default_server;
server_name _;
return 301 https://$host$request_uri;
}
1. Varnish / Wordpress 配置 - 手动设置解决方案
https://betterprogramming.pub/set-up-varnish-cache-for-wordpress-8e2ac92ce347
2。 Varnish / Wordpress config - Wordpress 插件解决方案
对于另一个 php 解决方案,您也可以在 public/index.php 的开头使用它 这对你的情况应该更有效
if($_SERVER['SERVER_PORT'] != 443) {
header('Location: https://www.yourdomain.com');
exit;
}
echo 'Your current server port is = '.$_SERVER['SERVER_PORT'].'<br />';