- 如果没有特殊需求,使用
yum安装nginx,用systemctl进行管理。
特殊情况下编译安装 nginx 后,建议配置 nginx 服务,通过 systemctl 管理 nginx。
默认情况下要求配置目录保持为
/etc/nginx。禁止修改配置文件中的
user nginx;配置,确保使用 nginx 用户启动nginx。/etc/nginx/nginx.conf中配置日志格式和日志路径、body 大小限制、超时限制等全局配置,示例如下:

推荐的日志格式如下,一定要记录 $request_body,除非后端业务责任人可以明确表示,后端服务有记录post body。
log_format main '$time_iso8601 $host $remote_addr "$request" ''$status $bytes_sent $body_bytes_sent $gzip_ratio "$http_referer" "$http_user_agent" ''$request_time $upstream_response_time $http_x_forwarded_for $request_length ''"$upstream_addr" $http_x_real_port $request_body';
日志路径如果不使用默认的 /var/log/nginx/access.log,请设置为 /data/log/nginx/access.log。
/etc/nginx/conf.d目录下配置实际的server,每个server配置一个文件,避免所有server配置在default 文件中,示例如下:

/var/log/nginx/log {
create 0644 nginx nginx
daily
rotate 60
missingok
notifempty
compress
sharedscripts
postrotate
/bin/kill -USR1 cat /run/nginx.pid 2>/dev/null 2>/dev/null || true
endscript
}
``
在没有转储能力的前提下,本地至少保留 30 天的日志,建议保留 180 天。<br />在有转储能力(通过 filebeat 或 logstash 收集到大数据平台存储)的前提下,本地建议保存 7 天日志。<br />如果做日志转储,必须进行数据脱敏!!譬如日志中记录的apikey=1234567890abcd,可以脱敏为apikey=1234*abcd`。
