请求body日志记录

自定义日志格式,使用 $request_body 变量:

  1. http {
  2. log_format custom '$remote_addr - $remote_user [$time_local] "$request" '
  3. '$status $body_bytes_sent "$http_referer" '
  4. '"$http_user_agent" "$request_body"';
  5. ...
  6. }

在 server 或 location 块中使用自定义的日志格式:

  1. server {
  2. ...
  3. access_log /var/log/nginx/your_access.log custom;
  4. ...
  5. }

请求头密钥验证

  1. http {
  2. map $http_authorization $valid_token {
  3. default 0; # 默认情况下,令牌无效
  4. "Bearer xxx" 1; # 如果令牌匹配,则设置为有效,xxx一般为ak-加32位字符串,例如【Bearer ak-bjtq3nr4m74j2v8fy5j6qaes32h8888】
  5. }
  6. ...
  7. }
  8. server {
  9. location / {
  10. # 检查Authorization头中的令牌是否有效
  11. if ($valid_token = 0) {
  12. return 403; # 返回403 Forbidden
  13. }
  14. ...