小编sca*_*che的帖子

让 nginx 将所有内容重定向到 https,除了一个目录

我需要 nginx 将所有 http URL 重定向到 https,但“.secret/”目录除外,该目录应继续用作 http。

因此,例如:

  • http://example.com/a.html --> https://example.com/a.html
  • http://example.org/z/b.html --> https://example.org/z/b.html
  • http://example.com/.secret/x.html --> http://example.com/.secret/x.html

我的配置中有以下内容,但对于 http,它返回包含“_”的地址。

server {
    listen 80;
 
    server_name _;
 
    location /.secret {
        return http://$server_name$request_uri;
    }
 
    location / {
        return 301 https://$server_name$request_uri;
    }
}
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

更新:

结合@mforsetti 和@Pothi_Kalimuthu 的评论,以下内容有效:

server {
    listen 80;
 
    server_name _;
 
    location /.secret { }
 
    location / {
        return 301 https://$host$request_uri;
    }
}
Run Code Online (Sandbox Code Playgroud)

nginx redirect 301-redirect

4
推荐指数
1
解决办法
254
查看次数

标签 统计

301-redirect ×1

nginx ×1

redirect ×1