运行WordPress的NGINX服务器上的HTTPS



我正在尝试在nginx服务器上的站点上实现HTTPS,现在即使使用以下配置,它也只能打开HTTP站点 我的nginx服务器的服务器配置是这样的

server {        
listen 443 ssl http2;
ssl_certificate     /etc/letsencrypt/live/mydomain.in/fullchain.pem;
ssl_certificate_key     /etc/letsencrypt/live/mydomain.in/privkey.pem;
server_name mydomain.in www.mydomain.in;
rewrite ^(.*) http://$server_name$1 permanent;
}
server {

server_name mydomain.in  www.mydomain.in;

access_log /var/log/nginx/mydomain.in.access.log rt_cache_redis;
error_log /var/log/nginx/mydomain.in.error.log;

root /var/www/mydomain.in/htdocs;

index index.php index.html index.htm;

include  common/redis-php7.conf; 
include common/wpcommon-php7.conf;
include common/locations-php7.conf;
include /var/www/mydomain.in/conf/nginx/*.conf;
}

服务器不提供HTTPS请求,即即使我专门将https放在浏览器中,它仍然会将我带回http站点。我无法诊断它的nginx或wordpress是否有问题?

注意:流量通过 cloudflare dns 路由,证书是 在 Cloudflare 中关闭,以免干扰。我对nginx相对较新

下面是基本思想。

server {        
server_name mydomain.in www.mydomain.in;
listen 80;
location / {
return 301 https://mydomain.in$request_uri;
}
}
server {
listen 443 ssl http2;
ssl_certificate     /etc/letsencrypt/live/mydomain.in/fullchain.pem;
ssl_certificate_key     /etc/letsencrypt/live/mydomain.in/privkey.pem;
server_name mydomain.in  www.mydomain.in;
access_log /var/log/nginx/mydomain.in.access.log rt_cache_redis;
error_log /var/log/nginx/mydomain.in.error.log;
root /var/www/mydomain.in/htdocs;
index index.php index.html index.htm;
include common/redis-php7.conf; 
include common/wpcommon-php7.conf;
include common/locations-php7.conf;
include /var/www/mydomain.in/conf/nginx/*.conf;
}

顶部server块在端口80(http) 上listens。它有一个location块,可以执行return301。 在大多数情况下,return优先于重写。我还将其放入location块中,因为您有一个letsencryptssl 证书,可能需要另一个location ^~ /.well-known {块来帮助处理它。

第二个server块在端口443(https) 上listens。它具有 SSL 证书,并包含之前作为 httpserver块公开的信息。

此设置将处理从mydomain.inwww.mydomain.in上的http重定向到httpsmydomain.inhttpsmydomain.inwww.mydomain.in都将收到SSL请求。

如果您希望它重定向到主https域,则可以像这样为辅助域添加另一个服务器块。

server {        
server_name www.mydomain.in;
listen 443 ssl http2;
ssl_certificate     /etc/letsencrypt/live/mydomain.in/fullchain.pem;
ssl_certificate_key     /etc/letsencrypt/live/mydomain.in/privkey.pem;
location / {
return 301 https://mydomain.in$request_uri;
}
}

当然,这意味着您必须更改第二个server块才能删除辅助域名。

此外,在测试时,您可能希望将301s 更改为302s,以便在第一次配置错误时不会卡在浏览器缓存中。让一切恢复良好状态后,然后变回301s。

最新更新