nginx对cookie设置,取消Secure属性

weixin_38052215 2018-05-31 07:20:49
用nginx做反向代理的时候,后台总是收不到session,发现cookie的属性有一个Secure,查阅资料说这个属性会使客户端在发送请求的时候,如果是http,不会带上cookie。但我不知道怎么取消这个属性。
...全文
2429 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
btx258 2021-01-14
  • 打赏
  • 举报
回复 2
遇到同样的问题,去查了官网手册,在1.19.3以后的版本,可以用proxy_cookie_flag实现。 proxy_cookie_flags ~ nosecure; 这个表示去掉所有cookie里的secure属性,亲测可用。 Syntax: proxy_cookie_flags off | cookie [flag ...]; Default: proxy_cookie_flags off; Context: http, server, location This directive appeared in version 1.19.3. Sets one or more flags for the cookie. The cookie can contain text, variables, and their combinations. The secure, httponly, samesite=strict, samesite=lax, samesite=none parameters add the corresponding flags. The nosecure, nohttponly, nosamesite parameters remove the corresponding flags. The cookie can also be specified using regular expressions. In this case, cookie should start from the “~” symbol. Several proxy_cookie_flags directives can be specified on the same configuration level: proxy_cookie_flags one httponly; proxy_cookie_flags ~ nosecure samesite=strict; If several directives can be applied to the cookie, the first matching directive will be chosen. In the example, the httponly flag is added to the cookie one, for all other cookies the samesite=strict flag is added and the secure flag is deleted. The off parameter cancels the effect of the proxy_cookie_flags directives inherited from the previous configuration level.
Sticky是nginx的一个模块,它是基于cookie的一种nginx的负载均衡解决方案,通过分发和识别cookie,来使同一个客户端的请求落在同一台服务器上,默认标识名为route (a)客户端首次发起访问请求,nginx接收后,发现请求头没有cookie,则以轮询方式将请求分发给后端服务器。 (b)后端服务器处理完请求,将响应数据返回给nginx。 (c)此时nginx生成带route的cookie,返回给客户端。route的值与后端服务器对应,可能是明文,也可能是md5、sha1等Hash值 (d)客户端接收请求,并保存带route的cookie。 (e)当客户端下一次发送请求时,会带上route,nginx根据接收到的cookie中的route值,转发给对应的后端服务器。 其他需要注意的 (a)同一客户端的请求,有可能落在不同的后端服务器上。如果客户端启动时同时发起多个请求。由于这些请求都没带cookie,所以服务器会随机选择后端服务器,返回不同的cookie。当这些请求中的最后一个请求返回时,客户端的cookie才会稳定下来,值以最后返回的cookie为准。 (b)cookie不一定生效。由于cookie最初由服务器端下发,如果客户端禁用cookie,则cookie不会生效。 (c)客户端可能不带cookie。Android客户端发送请求时,一般不会带上所有的cookie,需要明确指定哪些cookie会带上。如果希望用sticky做负载均衡,请对Android开发说加上cookie。 (d)cookie名称不要和业务使用的cookie重名。Sticky默认的cookie名称是route,可以改成任何值。 (e)客户端发的第一个请求是不带cookie的。服务器下发的cookie,在客户端下一次请求时才能生效。 (f)Nginx sticky模块不能与ip_hash同时使用 sticky [name=route] [domain=.foo.bar] [path=/] [expires=1h] [hash=index|md5|sha1] [no_fallback] [secure] [httponly]; [name=route] 设置用来记录会话的cookie名称 [domain=.foo.bar] 设置cookie作用的域名 [path=/] 设置cookie作用的URL路径,默认根目录 [expires=1h] 设置cookie的生存期,默认不设置,浏览器关闭即失效,需要是大于1秒的值 [hash=index|md5|sha1] 设置cookie中服务器的标识是用明文还是使用md5值,默认使用md5 [no_fallback] 设置该项,当sticky的后端机器挂了以后,nginx返回502 (Bad Gateway or Proxy Error) ,而不转发到其他服务器,不建议设置 [secure] 设置启用安全的cookie,需要HTTPS支持 [httponly] 允许cookie不通过JS泄漏,没用过

472

社区成员

发帖
与我相关
我的任务
社区描述
其他技术讨论专区
其他 技术论坛(原bbs)
社区管理员
  • 其他技术讨论专区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧