• 主页
  • 标签
  • 归档
  • 搜索
  • Github

April 29, 2020

2020-4-29 工作日志

小书匠

apache2 设置 cross-origin resource sharing

现在的浏览器,有些 ajax 请求,会先执行 pre-flight , 导致即使服务器配置了 Access-Control-Allow-Origin, 依然会报跨域请求的错误.

这里记录下在 apache 里的解决方法.

打开 rewrite mod

conf/httpd.confapache    1行
  1. 1LoadModule rewrite_module modules/mod_rewrite.so 

conf/conf-available/dav.confapache    10行
  1. 1# Always set these headers. 

  2. 2Header always set Access-Control-Allow-Origin "*" 

  3. 3Header always set Access-Control-Allow-Methods "POST, GET, OPTIONS, DELETE, PUT" 

  4. 4Header always set Access-Control-Max-Age "1000" 

  5. 5Header always set Access-Control-Allow-Headers "x-requested-with, Content-Type, origin, authorization, accept, client-security-token" 

  6. 6  

  7. 7# Added a rewrite to respond with a 200 SUCCESS on every OPTIONS request. 

  8. 8RewriteEngine On 

  9. 9RewriteCond %{REQUEST_METHOD} OPTIONS 

  10. 10RewriteRule ^(.*)$ $1 [R=200,L] 

参考文章

https://suziwen.github.io/acorns/tech/setting-cors-cross-origin-resource-sharing-on-apache-with-correct-response-headers-allowing-everything-through

即使按照上面的方法,还是会有 401 的错误, 最后版本.

添加 LimitExcept OPTIONS 信息

  1. 1<Directory "/var/lib/dav/data/"> 

  2. 2 Dav On 

  3. 3 Options Indexes FollowSymLinks 

  4. 4 

  5. 5 # Always set these headers. 

  6. 6 

  7. 7 Header always set Access-Control-Allow-Origin "*" 

  8. 8 Header always set Access-Control-Allow-Headers "*" 

  9. 9 Header always set Access-Control-Expose-Headers "ETag" 

  10. 10 Header always set Access-Control-Allow-Methods "*" 

  11. 11 Header always set Access-Control-Allow-Credentials "true" 

  12. 12 # Added a rewrite to respond with a 200 SUCCESS on every OPTIONS request. 

  13. 13 RewriteEngine On 

  14. 14 RewriteCond %{REQUEST_METHOD} OPTIONS 

  15. 15 RewriteRule ^(.*)$ blank.html [R=200,L,E=HTTP_ORIGIN:%{HTTP:ORIGIN}] 

  16. 16 AuthType Basic 

  17. 17 AuthName "WebDAV" 

  18. 18 AuthUserFile "/user.passwd" 

  19. 19 <LimitExcept OPTIONS> 

  20. 20 Require valid-user 

  21. 21 </LimitExcept> 

  22. 22</Directory> 

  23. 23 

docker 启动失败分析处理

由于直接在 docker 容器里修改了文件,导致 容器无法正常启动.(注:不建议直接在容器内修改文件)

  1. 1docker ps -a 

  2. 2docker logs {容器id} 

报错信息

  1. 1Invalid command 'RewriteEngine', perhaps misspelled or defined by a module not included in the server configuration 

重新修改配置文件 , 将 rewrite mods 打开

  1. 1docker cp {容器id}:/usr/local/apache2/conf/conf-available/dav.conf ./ 

  2. 2vi dav.conf 

  3. 3docker cp ./dav.conf {容器id}:/usr/local/apache2/conf/conf-available/ 

webdav 协议不支持递归创建不存在的目录问题

  1. 1# 逆向递归创建目录 

  2. 2ensureDir = (client, paths, cb)-> 

  3. 3 client.exists(paths.join("/")).then((isExists)-> 

  4. 4 if isExists 

  5. 5 # 结束递归 

  6. 6 cb() 

  7. 7 else 

  8. 8 if paths.length <= 1 

  9. 9 # 创建最开始一层的目录,并结束递归 

  10. 10 client.createDirectory(paths.join("/")).then(()-> 

  11. 11 cb() 

  12. 12 ).catch((error)-> 

  13. 13 cb(error) 

  14. 14 ) 

  15. 15 else 

  16. 16 ensureDir(client, paths.slice(0, -1), (error)-> 

  17. 17 if error 

  18. 18 cb(error) 

  19. 19 else 

  20. 20 client.createDirectory(paths.join("/")).then(()-> 

  21. 21 cb() 

  22. 22 ).catch((error)-> 

  23. 23 cb(error) 

  24. 24 ) 

  25. 25 ) 

  26. 26 ) 

快速制作 u 盘引导程序

https://github.com/ventoy/Ventoy

https://www.ventoy.net/en/index.html

生活

一声哈武镖车走,年年江湖平安回

英语

Tagged with 远程工作 | 工作日志 | 小书匠
Time Flies, No Time for Nuts
Copyright © 2020 suziwen
Build with  Gatsbyjs  and  Sculpting theme