大网数据广告活动
服务器运维问答

nginx https www redirect to non-www using let's encrypt certbot

· 52CE 我爱测

Hi this must be a basic question but I haven't seen an answer with cerbot considerations (if there are any considerations). How do I get https www to redirect to non-www instead of timing out? The https www version of my site tim…

问题原文

原帖链接 · duncangarde · CC BY-SA 3.0

Hi this must be a basic question but I haven't seen an answer with cerbot considerations (if there are any considerations). How do I get https www to redirect to non-www instead of timing out?

The https www version of my site times out instead of redirecting to non-www, whereas all other versions (http and https non-www) work fine.

Preferably I would like to future proof so that I can renew the certs through certbot and not need to manually change the nginx config afterwards.

The nginx server config is shown below:

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;
    server_name www.mydomain.com mydomain.com;

    listen 443 ssl; # managed by Certbot
    ssl_certificate <path_to_cert> # managed by Certbot
    ssl_certificate_key <path_to_key>; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam <path_to_this>

    if ($scheme != "https") {
        return 301 https://$host$request_uri;
    } # managed by Certbot
}

已采纳解答

原帖链接 · Tim · CC BY-SA 3.0

You use separate server blocks for each of the four variations of http/s and www/non-www. You never use "if" statements if you can do things another way - Nginx If is Evil.

# http server, static websites        
server {            
  server_name example.com;            

  listen 443 ssl http2;            

  ssl_certificate /var/lib/acme/certs/***CERT_DIRECTORY/fullchain;            
  ssl_certificate_key /var/lib/acme/certs/***CERT_DIRECTORY/privkey;            

  root     /var/www/***rootdir;            
}            

# This server simply redirects the requested to the https version of the page            
server {            
  listen 80;            
  server_name www.example.com example.com;            

  # Let's Encrypt certificates with Acmetool            
  location /.well-known/acme-challenge/ {            
    alias /var/www/.well-known/acme-challenge/;            
  }            

  location / {            
    return 301 https://example.com$request_uri;            
  }            
}            

server {            
  listen 443 ssl http2;            
  server_name www.example.com;            

  ssl_certificate /var/lib/acme/certs/***CERT_DIRECTORY/fullchain;            
  ssl_certificate_key /var/lib/acme/certs/***CERT_DIRECTORY/privkey;            

  return 301 https://example.com$request_uri;            
}            

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-01-12)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。