[心缘地方]同学录
首页 | 功能说明 | 站长通知 | 最近更新 | 编码查看转换 | 代码下载 | 常见问题及讨论 | 《深入解析ASP核心技术》 | 王小鸭自动发工资条VBA版
登录系统:用户名: 密码: 如果要讨论问题,请先注册。

[备忘]nginx针对302跳转的处理[http转https,还有cookie的处理]

上一篇:[备忘]nginx的windows版本,支持lua
下一篇:[备忘]websocket报错:The remote endpoint was in state [TEXT_FULL_WRITING] which is an invalid state for

添加日期:2023/11/14 11:38:14 快速返回   返回列表 阅读205次
我端口用的2222


    location  / {

        proxy_cookie_flags ~ nosecure nosamesite;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-Port $remote_port;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_pass https://order.xxx.net;
        proxy_redirect https://order.xxx.net/ http://$host:2222/;
    }


(1)proxy_pass https://order.xxx.net;
这句负责转发

(2)proxy_redirect https://order.xxx.net/ http://$host:2222/;
这句负责替换302跳转的地址,这样浏览器地址始终是192.168.1.100:2222,不会变成目标网址

(3)由于是http转发为https网页,有个问题,就是cookie
响应中cookie是带Secure=true的,
格式如下:
SESSION=MjIyZjk4ZTYtZTFkNS00ZjBlLTlkMWQtZDI3NGVlMDU1YmYz; Path=/; secure=true; SameSite=None;; HttpOnly

由于是http://192.168.1.100:2222访问的,如果设置secure=true,浏览器会拒绝写入cookie。
但注意,如果用http://localhost:2222访问,是没问题的。

在本地开发环境下,浏览器对 localhost 的处理是一个例外情况。根据浏览器的安全策略,当你在 localhost 上使用 HTTP 访问时,它被视为一个安全连接,即使实际上不是通过 HTTPS 进行的。这样做是为了方便开发和调试过程。

解决办法:
nginx中替换掉set-cookie中的secure

proxy_cookie_flags ~ nosecure;



发现还是不行,查看浏览器提示,有SameSite,没有secure,写入cookie还是被拒绝了。

所以最后是这样:

proxy_cookie_flags ~ nosecure nosamesite;


替换后OK了。


 

评论 COMMENTS
没有评论 No Comments.

添加评论 Add new comment.
昵称 Name:
评论内容 Comment:
验证码(不区分大小写)
Validation Code:
(not case sensitive)
看不清?点这里换一张!(Change it here!)
 
评论由管理员查看后才能显示。the comment will be showed after it is checked by admin.
CopyRight © 心缘地方 2005-2999. All Rights Reserved