V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
Anonono
V2EX  ›  NGINX

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

  •  
  •   Anonono · Feb 23, 2024 · 2356 views
    This topic created in 795 days ago, the information mentioned may be changed or developed.

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

    Supplement 1  ·  Feb 23, 2024

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

    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 replies    2024-02-23 17:11:16 +08:00
    twofox
        1
    twofox  
       Feb 23, 2024
    if ($scheme = http) {
    return 301 https://$server_name$request_uri;
    }
    我是这么配置的
    直接配置
    error_page 497 https://$http_host$request_uri;

    也可以

    看起来这个 301 = 307 有点多余
    ysc3839
        2
    ysc3839  
       Feb 23, 2024 via Android
    https://nginx.org/en/docs/http/ngx_http_core_module.html#error_page
    是遇到 407 或 301 这两个 code ,都跳转到目标 uri
    Anonono
        3
    Anonono  
    OP
       Feb 23, 2024
    @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  
       Feb 23, 2024 via Android
    @Anonono 但是 307 不是永久重定向,以及对于网站来说,连主页都没访问就去发 POST 的情况几乎不可能存在吧,那用 301 并没有问题,用户肯定是先 GET 首页。
    Anonono
        5
    Anonono  
    OP
       Feb 23, 2024
    @ysc3839 试着问了下 GPT4 ,得到的恢复是原帖描述有误,实际就是 497 301 都转向 307 而不是所谓的“永久重定向”
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3231 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 13:42 · PVG 21:42 · LAX 06:42 · JFK 09:42
    ♥ Do have faith in what you're doing.