【问题标题】:HTTPS on NGINX server running wordpress运行 wordpress 的 NGINX 服务器上的 HTTPS
【发布时间】:2019-05-06 07:35:32
【问题描述】:

我正在尝试在 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 块和 listen 443 ssl http2; 重写到其他非 SSL server 块所以看起来它就像你配置的那样工作。
  • @ShawnC。 ....实际上我过去已经由其他人配置了这个,有没有办法在这个配置中将流量路由到 https

标签: wordpress ssl nginx


【解决方案1】:

下面是基本思想。

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 阻止listens 在端口80 (http) 上。它有一个location 块,它执行return 301。return 优于most cases 中的重写。我还将它放入 location 块中,因为您有一个 letsencrypt ssl 证书,可能需要另一个 location ^~ /.well-known { 块来帮助处理。

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

此设置将处理从mydomain.inwww.mydomain.in 上的http 重定向到https mydomain.in。在 https 上,mydomain.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。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-16
    • 1970-01-01
    • 2021-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多