proc-hdr.conf 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # 标记客户端是否已安装 Service Worker
  2. # 0:请求任何路径,都返回 SW 安装页面(www/__setup.html)
  3. # 1:正常反向代理
  4. set $_hasSw '0';
  5. # 标记资源的协议
  6. # 0:HTTP
  7. # 1:HTTPS
  8. set $_isHttp '0';
  9. # 标记是否为 CORS 请求
  10. # 0:不转发 Origin 头
  11. # 1:调整并转发 Origin 头
  12. set $_hasCors '0';
  13. # 记录资源的端口号
  14. set $_port '';
  15. set $_ref '';
  16. set $_ori $http_origin;
  17. set $_acao '';
  18. # 获取并删除 flag 参数
  19. # 参数格式: isHttp .. port
  20. if ($args ~
  21. (?<_pre>.*?)&flag__=(?<_hasSw>.)(?<_isHttp>.)(?<_hasCors>.)(?<_port>\d*)(?<_post>.*)
  22. ) {
  23. set $args $_pre$_post;
  24. set $_js 1;
  25. }
  26. # 调整 Referer 头
  27. # TODO:未考虑协议和端口,下面的 cors 也有这问题
  28. if ($http_referer ~ ^https://(?<_vhost>[^/]+)(?<_path>.*)) {
  29. set $_ref https://$_vhost_to_rhost$_path;
  30. set $_acao https://$_vhost;
  31. }
  32. if ($_ori) {
  33. set $_acao $_ori;
  34. }
  35. if ($_acao = '') {
  36. set $_acao '*';
  37. }
  38. # ServiceWorker 的 fetch 强制 cors 模式,
  39. # 所以需要该标记,标识原始请求是否为 cors
  40. if ($_hasCors = '0') {
  41. set $_ori '';
  42. }
  43. if ($_ori ~ ^https://(?<_vhost>.*)) {
  44. set $_ori https://$_vhost_to_rhost;
  45. }
  46. proxy_set_header Origin $_ori;
  47. proxy_set_header Referer $_ref;
  48. proxy_set_header Upgrade $http_upgrade;
  49. proxy_set_header Connection $http_connection;
  50. # CSP 可能导致注入页面资源无法加载
  51. more_clear_headers
  52. content-security-policy
  53. content-security-policy-report-only
  54. expect-ct
  55. x-frame-options
  56. ;
  57. more_set_headers
  58. 'access-control-allow-credentials: true'
  59. 'access-control-allow-origin: $_acao'
  60. 'strict-transport-security: max-age=99999999; includeSubDomains; preload'
  61. ;
  62. # 重定向调整
  63. # 直接用 return 指令返回状态码,会丢失其他的头,比如 set-cookie
  64. header_filter_by_lua_file ../proc-redir.lua;