Nginx proxy_next_upstream non_idempotent
在 Nginx 做反向代理的时候,我们一般会配置 proxy_next_upstream
,如果某个 upstream 超时或出错,自动切换到下一个 upstram。
upstream backend{
server 192.168.0.1;
server 192.168.0.2;
}
location /example/ {
proxy_pass http://backend;
proxy_next_upstream error timeout http_500 non_idempotent;
}
这里有一个地方需要注意,non_idempotent
,Nginx 默认对 non-idempotent 请求,比如 POST/LOCK/PATCH,是不进行重试。常见的情况就是 POST 请求出错后不会重试,需要加上该设置。
normally, requests with a non-idempotent method (POST, LOCK, PATCH) are not passed to the next server if a request has been sent to an upstream server (1.9.13); enabling this option explicitly allows retrying such requests;
Homebrew Speedup
通过修改 Homebrew git repo 来加速:
cd "$(brew --repo)"
git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git
// 中科大 git remote set-url origin https://mirrors.ustc.edu.cn/brew.git
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git
// 中科大 git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git
// homebrew-cask
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-cask"
git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-cask.git
// homebrew-bottles
export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles
// export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles
// export HOMEBREW_BOTTLE_DOMAIN=http://7xkcej.dl1.z0.glb.clouddn.com
重置为官方源:
cd "$(brew --repo)"
git remote set-url origin https://github.com/Homebrew/brew.git
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://github.com/Homebrew/homebrew-core
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-cask"
git remote set-url origin https://github.com/Homebrew/homebrew-cask
Python Development Environment 2019
macOS
brew install python
to install python 3python3 -m venv .venv
orvirtualenv -p $(which python3) .venv
source .venv/bin/activate
pip install 'python-language-server[all]'
to install pyls, will switch to MSPyls.- coding
deactivate
to leave
No more pyenv, pipenv, use pipsi to install utils written in Python.
AWS Services List (2018)
一句话说清楚 AWS X 是什么
计算:
- EC2
- Amazon Elastic Compute Cloud,或者叫亚马逊的虚拟机服务
- 一台托管在 AWS 的电脑/服务器
- ECS
- Amazon Elastic Container Service
- 亚马逊 Docker 服务,配合内部 Elastic Container Registry(ECR) 做服务容器化
- 还有 EKS(ECS for Kubernetes)
- Lambda
- AWS App Scripts
- 只跑代码,不需要考虑服务器问题
- 可访问其他服务资源
存储:
- S3
- Amazon Simple Storage Service,或者叫亚马逊无限量 FTP 服务器
- 存储网站图片、视频、备份等
- 不同服务间文件共享
- EFS
- Amazon Elastic File System
- EBS
- Amazon Elastic Block Store
- EFS 和 EBS 很像,简单理解就是 EBS 是一个可以挂载到 EC2 的硬件磁盘,EFS 是一个网络文件系统
- Glacier
- 云归档服务
- 成本极低的数据存储和长期备份
数据库:
- RDS
- Amazon Relational Database Service
- 亚马逊数据库服务,帮你做好备份、扩容等
- Aurora
- 兼容 MySQL/PostgreSQL 的分布式关系型数据库
- Elasticache
- 亚马逊 Redis/Memcached 服务
- DynamoDB
- 亚马逊 NoSQL 服务
- KV 存储服务
- RedShift
- PB 级数据仓库
网关和内容分发:
- Cloudfront
- 亚马逊 CDN 服务
- Route53
- 域名注册,DNS 服务
- ELB
- Elastic Load Balancing,负载均衡
- API Gateway
- API 网关代理
- 管理后端 API,比如流量控制,监控,版本切换,A/B 测试分流等
开发者工具:
- CodeCommit
- 私有 Git 服务器
- CodeBuild
- CI 系统
- CodePipeline
- 持续交付发布
- Code Deploy
- 自动化部署
- 尤其适用于同时部署到多个 EC2
- Cloud9
- 云端 IDE
移动应用服务:
- Cognito
- 亚马逊 OAuth 服务
- 让用户使用社交网站信息注册、登录
- Device Farm
- 在真实设备上做应用测试
- 提供视频、截图、日志等信息
- Mobile Analytics
- 收集、查看应用分析数据
- 类似 Flurry、Google Analytics
Web 基础服务:
- SES
- Amazon Simple Email Service
- 亚马逊邮件发送服务,包括邮件通知、订阅等
- SNS
- Amazon Simple Notification Service
- 向用户发 push、邮件、短信
- SQS
- Amazon Simple Queue Service
- 亚马逊消息队列服务
- WAF
- Web 应用防火墙
- CloudSearch
- 或者叫亚马逊全文搜索服务
- Elastic Transcoder
- 亚马逊视频转码服务
其他,包括 AWS 安全,管理,监控等:
- IAM
- AWS Identity and Access Management
- 管理用户组,管理资源访问权限
- CloudTrail
- 跟踪 AWS 用户活动和 API 使用状况
- CloudWatch
- AWS 服务监控
- Config
- 记录、评估 AWS 资源配置
- OpsWorks
- 用 Chef 和 Puppet 实现操作自动化
- Trusted Advisor
- 对使用的 AWS 资源进行成本分析、安全建议等
- Inspector
- 自动安全评估服务,评估应用程序风险、漏洞
OpenResty/Nginx 日志输出到 Rsyslog
在 OpenResty/Nginx 开发中,日志输出一般是这么两种方案:
- 通过
ngx.log(ngx.ERR, ...)
输出到 error.log log_by_lua
阶段通过 lua-resty-logger-socket 输出到远端 syslog-ng 服务器
第一种方案方便开发,但是会和常规 error.log 比如 404等混在一起。第二种方案可以把日志分开,但需要日志服务器,也不方便开发的时候逐步 log,多用于日志收集分析。回到方案一,可以通过 rsyslog
将日志切割到独立文件,方便排查。
nginx:
error_log syslog:server=127.0.0.1,tag=nginx_crit;
or
error_log syslog:server=127.0.0.1,tag=nginx_crit crit;
ngx.log(ngx.CRIT, ...)
rsyslog:
module(load="imudp")
input(type="imudp" port="514" ruleset="ngx_ruleset")
template(name="json" type="list") {
constant(value="{")
constant(value="\"timestamp\":\"") property(name="timereported" dateFormat="rfc3339")
constant(value="\",\"host\":\"") property(name="hostname")
constant(value="\",\"tag\":\"") property(name="syslogtag" format="json")
constant(value="\",\"message\":\"") property(name="msg" format="json")
constant(value="\"}")
constant(value="\n")
}
ruleset(name="ngx_ruleset"){
if $msg contains 'lua' then {
/var/log/ngx_lua.log
stop
}
if $programname == 'nginx_crit' {
/var/log/ngx_lua.log
stop
}
action(type="omfile" file="/var/log/ngx.log" template="json")
}
Pretty format JSON in Vim
command! PrettyJSON %!python -m json.tool
or, coc.nvim + coc-prettier, which can format JavaScript/TypeScript/HTML/JSON using Prettier.
Programming is the art of adding bugs to an empty text file
Louis Srygley:
Without requirements or design, programming is the art of adding bugs to an empty text file.
Vim folding
- zi - toggle folding
- za - toggle current fold open/close
- zR - open all folds
- zM - close all folds
- zc - close current fold
-
zo - open currrent fold
- zj - move down to next fold
- zk - move up to previous fold
nnoremap <Space> za
在所有子目录下执行 git
git config --global alias.all '!f() { ls -R -d */.git | sed 's,\/.git,,' | xargs -P10 -I{} git -C {} $1; }; f'
git all pull