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

How to forward all http requests to https without breaking letsencrypt with nginx

· 52CE 我爱测

I'm using Nginx to serve as a reverse proxy for Rstudio server and Shiny server for the purposes of securing the connection to these services. I'm using Letencrypt for free signed certs. I have this entry for letsencrypt to verif…

问题原文

原帖链接 · Dean MacGregor · CC BY-SA 3.0

I'm using Nginx to serve as a reverse proxy for Rstudio server and Shiny server for the purposes of securing the connection to these services. I'm using Letencrypt for free signed certs.

I have this entry for letsencrypt to verify I am who I say I am.

server { 
  listen 80 default_server;
  listen [::]:80 default_server ipv6only=on;

  root /usr/share/nginx/html;
  index index.html index.htm;

  # Make site accessible from http://localhost/
  server_name mydomain.com;

  location ~ /.well-known {

    allow all;
  }
}

I also have another server to listen on 443 and proxy_redirect to my servers. Everything there works fine. What I want to do is have something like

server {
listen 80;
return 301 https://$host$request_uri;
}

but I think this will break letsencrypt so how do I have the above without breaking letsencrypt verification?

已采纳解答

原帖链接 · Michael Hampton · CC BY-SA 3.0

My working configuration for this is:

server {
         listen 80;
         listen [::]:80;
         server_name example.com www.example.com;

         root /srv/www/empty;

         include includes/letsencrypt;

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

where /etc/nginx/includes/letsencrypt is Ansible managed and contains:

location /.well-known/acme-challenge/ {
         try_files $uri =404;
}

and /srv/www/empty is an otherwise empty directory, which only contains files when Let's Encrypt is being used to issue a challenge (with --webroot).

Let's Encrypt is then run with:

letsencrypt certonly --webroot -w /srv/www/empty -d example.com -d www.example.com

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

资料来源与适用范围

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

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

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