AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 1069186
Accepted
scaramouche
scaramouche
Asked: 2021-07-10 20:41:47 +0800 CST2021-07-10 20:41:47 +0800 CST 2021-07-10 20:41:47 +0800 CST

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

  • 772

我需要 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;
    }
}

我究竟做错了什么?

更新:

结合来自@mforsetti 和@Pothi_Kalimuthu 的评论,以下工作有效:

server {
    listen 80;
 
    server_name _;
 
    location /.secret { }
 
    location / {
        return 301 https://$host$request_uri;
    }
}
redirect nginx 301-redirect
  • 1 1 个回答
  • 1393 Views

1 个回答

  • Voted
  1. Best Answer
    mforsetti
    2021-07-10T21:45:12+08:002021-07-10T21:45:12+08:00

    它返回包含“_”的地址。

    server_name _;
    
    location /.secret {
       return http://$server_name$request_uri;
    }
    

    $server_name返回分配server_name给server块,在你的情况下是_; 因此_返回的地址。

    如果您希望它返回主机名或Host请求标头,请尝试使用$host,例如:

    location /.secret {
        return http://$host$request_uri;
    }
    

    .secret/应该继续作为 http 服务的目录

    如果要提供目录,请指定一个root目录。

    location /.secret {
        root /path/to/your/parent/of/secret/directory;
    }
    
    • 5

相关问题

  • 301 将根域重定向到 Godaddy Windows 主机帐户上的 www 子域

  • 使用 IIS7 重定向 Godaddy 中的子域

  • apache重写以将文件夹分配给域

  • 如何让 Apache2 重定向到子目录

  • 如何强制我的网址始终以 www 开头?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve