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

Redirect all requests to HTTPS, except for one subdirectory

· 52CE 我爱测

I'm trying to move from self-signed certificates to Let's Encrypt certificates on my nginx webserver. Currently, I redirect all requests to http/80 to https/443 , which uses a self signed certificate I created a while ago. Now - …

问题原文

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

I'm trying to move from self-signed certificates to Let's Encrypt certificates on my nginx webserver.

Currently, I redirect all requests to http/80 to https/443, which uses a self signed certificate I created a while ago.

Now - from what I understand Let's Encrypt makes a request to port 80 (as I am using the webroot option of certbot). These requests are redirected, which renders the certificate generation unsuccessful.

I tried to achieve this with the following server block, listening at port 80:

server {
        listen  80;     
        server_name     sub.domain.tld;
        server_tokens   off;


        location /.well-known {
                root /var/www/letsencrypt;
        }

        location / {
                return 301 https://$host$request_uri;
        }
}

But requests to /.well-known are redirected to https/443 anyways.

How can I redirect all requests from http/80 to https/443, except the requests to /.well-known/?

已采纳解答

原帖链接 · Tero Kilkanen · CC BY-SA 3.0

Try this:

server {
    listen  80;     
    server_name     sub.domain.tld;
    server_tokens   off;

    root /var/www/letsencrypt;

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

    location / {
        return 301 https://$host$request_uri;
    }
}

Since there was no try_files entry in your virtual server, it didn't know what to do with requests coming to /.well-known.

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

资料来源与适用范围

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

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

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