V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
Anonono
V2EX  ›  NGINX

关于同端口 http 跳转 https 的配置问题

  •  
  •   Anonono · 64 天前 · 1138 次点击
    这是一个创建于 64 天前的主题,其中的信息可能已经有所发展或是发生改变。

    通过配置error_page 497 301 =307 https://$http_host$request_uri;可以实现 http 跳转 https ,但是不太明白这个 301 的作用有哪位可以解释下吗

    第 1 条附言  ·  64 天前

    这是原帖的内容,没明白最后一句...

    error_page 497 https://$host$request_uri; 默认用302,临时重定向
    error_page 497 =301 https://$host$request_uri; 永久重定向
    error_page 497 =307 https://$host$request_uri; 临时重定向,不改变请求的方法(如post还是post)
    如果重定向后,没有带上正确的端口号,则显式的加上端口号 $server_port,
    
    如 error_page 497 https://$host:$server_port$request_uri;
    还有一种写法,
    
    error_page 497 https://$host:$server_port$uri$is_args$args;
    最终写法:
    
    error_page 497 301 =307 https://$host:$server_port$request_uri; 永久重定向,不改变请求的方法(如post还是post)
    
    5 条回复    2024-02-23 17:11:16 +08:00
    twofox
        1
    twofox  
       64 天前
    if ($scheme = http) {
    return 301 https://$server_name$request_uri;
    }
    我是这么配置的
    直接配置
    error_page 497 https://$http_host$request_uri;

    也可以

    看起来这个 301 = 307 有点多余
    ysc3839
        2
    ysc3839  
       64 天前 via Android
    https://nginx.org/en/docs/http/ngx_http_core_module.html#error_page
    是遇到 407 或 301 这两个 code ,都跳转到目标 uri
    Anonono
        3
    Anonono  
    OP
       64 天前
    @ysc3839 感谢回答,我没能明白为什么原帖会加上 301 ,就有点怪了
    @twofox 是在一个链接有看到加上 301 =307 以后不会修改原始请求方式,POST 仍然是 POST ,=307 我能明白,链接里的 301 我按 2 楼的方式能明白,但是看起来好像也有点多余?(绕进去了)

    * https://www.v2ex.com/t/648986
    * https://www.cnblogs.com/haolb123/p/16553020.html
    * https://stackoverflow.com/a/15435799/11553658
    ysc3839
        4
    ysc3839  
       64 天前 via Android
    @Anonono 但是 307 不是永久重定向,以及对于网站来说,连主页都没访问就去发 POST 的情况几乎不可能存在吧,那用 301 并没有问题,用户肯定是先 GET 首页。
    Anonono
        5
    Anonono  
    OP
       64 天前
    @ysc3839 试着问了下 GPT4 ,得到的恢复是原帖描述有误,实际就是 497 301 都转向 307 而不是所谓的“永久重定向”
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2944 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 13:03 · PVG 21:03 · LAX 06:03 · JFK 09:03
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.