我正在尝试在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) 上listen
s。它有一个location
块,可以执行return
301。 在大多数情况下,return
优先于重写。我还将其放入location
块中,因为您有一个letsencrypt
ssl 证书,可能需要另一个location ^~ /.well-known {
块来帮助处理它。
第二个server
块在端口443
(https) 上listen
s。它具有 SSL 证书,并包含之前作为 httpserver
块公开的信息。
此设置将处理从mydomain.in
或www.mydomain.in
上的http
重定向到https
mydomain.in
。https
mydomain.in
和www.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
块才能删除辅助域名。
此外,在测试时,您可能希望将301
s 更改为302
s,以便在第一次配置错误时不会卡在浏览器缓存中。让一切恢复良好状态后,然后变回301
s。