创建用户
groupadd -g 10301 nginx
useradd -m -d /home/nginx -g nginx -u 10301 nginx -s /bin/bash
创建相关文件系统
root用户下操作:
lvcreate -n lvnginx -L 10G /dev/mapper/rootvg
mkdir /usr/nginx
mkfs.xfs /dev/rootvg/lvnginx
echo -e "/dev/rootvg/lvnginx\t/usr/nginx\txfs\tdefaults\t0\t0">>/etc/fstab
mount -a
df -h
chown nginx:nginx /usr/nginx
安装依赖
root用户下:
#Redhat
yum install openssl-devel zlib-devel pcre-devel gcc-c++ libtool -y
#Ubuntu
apt-get install libssl-dev zlib1g-dev libpcre3-dev gcc libtool make -y
编译安装
nginx用户下
cd ~/
curl -kL -O https://nginx.org/download/nginx-1.26.2.tar.gz
tar -zxf nginx-1.26.2.tar.gz
#安装第三方监控模块选用
git clone https://github.com/vozlt/nginx-module-vts.git
export NGINX_VERSION=nginx-1.26.2
cd ~/${NGINX_VERSION}
mkdir -p /usr/nginx/${NGINX_VERSION}/logs
mkdir -p /usr/nginx/${NGINX_VERSION}/tmp
./configure --user=nginx \
--group=nginx \
--prefix=/usr/nginx/${NGINX_VERSION} \
--with-pcre \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/usr/nginx/${NGINX_VERSION}/tmp/client \
--http-proxy-temp-path=/usr/nginx/${NGINX_VERSION}/tmp/proxy \
--http-fastcgi-temp-path=/usr/nginx/${NGINX_VERSION}/tmp/fcgi \
--with-http_realip_module \
--with-stream \
--with-http_sub_module \
--add-module=../nginx-module-vts
make -j$(nproc) && make install
配置环境变量:
echo 'export PATH="/usr/nginx/nginx-1.26.2/sbin:$PATH"'>>~/.bashrc
#配给所有用户
#echo 'export PATH="/usr/nginx/nginx-1.26.2/sbin:$PATH"'>>/etc/profile
source ~/.bashrc
验证与启动
创建两个子文件夹夹用于存放nginx的配置文件
mkdir /usr/nginx/${NGINX_VERSION}/conf/http.d
mkdir /usr/nginx/${NGINX_VERSION}/conf/stream.d
编辑/usr/nginx/${NGINX_VERSION}/conf/nginx.conf
,
修改子进程用户为nginx(仅root拉起主进程时生效),取消关于log的注释,添加隐藏版本号选项,删除主要提示内容后保留内容如下:
#user nginx;
worker_processes auto;
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
server_tokens off;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
include http.d/*.conf;
}
stream{
include stream.d/*.conf;
}
默认配置文件迁移至http.d/zabbix.conf
并添加一段用于监控,最终如下
80端口修改为8088,添加如下一段用于监控:
location /nginx_status{
allow 127.0.0.1;
deny all;
stub_status on;
access_log off;
}
nginx用户启动(1024及以下端口启动时需要root权限):
- 验证:
nginx -t
- 启动:
nginx
- 验证:
curl localhost:8088/nginx_status
- 停止:
nginx –s stop
配置自启动
新建文件/etc/rc.local:
#!/bin/bash
su - nginx -c "/usr/nginx/nginx-1.26.2/sbin/nginx -c /usr/nginx/nginx-1.26.0/conf/nginx.conf"
exit 0
完成后赋权chmod 755 /etc/rc.local
,当存在/etc/rc.local
且有可执行权限事Ubuntu会自动启动rc-local.service服务。如果需要开启1080以下端口的监听,取消掉su - nginx -c ""
以下方案弃用
root用户下,新增文件/lib/systemd/system/nginx.service:
(记得根据实际情况修改启动命令)
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/nginx/nginx-1.24.0/logs/nginx.pid
ExecStartPre=/usr/local/bin/nginx -t -q -g 'daemon on; master_process on;'
ExecStart=/usr/local/bin/nginx -g 'daemon on; master_process on;'
ExecReload=/usr/local/bin/nginx -g 'daemon on; master_process on;' -s reload
ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /usr/nginx/nginx-1.24.0/logs/nginx.pid
TimeoutStopSec=5
KillMode=mixed
[Install]
WantedBy=multi-user.target
参数配置
日志循环
使用root用户,vim /etc/logrotate.d/nginx
,利用系统自带的logrotate工具实现日志循环:
/usr/nginx/nginx*/logs/*.log {
#使用nginx用户权限
su nginx nginx
#每天运行一次
daily
#保留7天日志
rotate 7
#当日志文件达到10M时才切割
size 10M
#以%Y%m%d作为后缀
dateext
dateformat -%Y-%m-%d
#转储后压缩
compress
#转储的日志文件到再下次切割时才压缩
delaycompress
#若日志文件丢失则忽略错误,处理下一个文件
missingok
#若日志文件为空,不做切割
notifempty
#切割后日志文件权限为644,属主nginx
create 644 nginx nginx
#所有日志都切割转储后,再把postrotate指定的命令只运行一次
sharedscripts
postrotate
[ -f /usr/nginx/nginx*/logs/nginx.pid ] && kill -USR1 `cat /usr/nginx/nginx*/logs/nginx.pid`
endscript
}
完成后使用logrotate -dv /etc/logrotate.d/nginx
测试,无误后使用logrotate -vf /etc/logrotate.d/nginx
进行试运行,可在/usr/nginx/logs
下找到分割后的日志。
server管理
为便于管理,这里对默认conf进行了修改,拆分http和stream两个模块的各conf至http与stream两个文件夹,最后配置如下(已包含了后续参数管理的建议值):
日志配置:
编辑/usr/nginx/conf/nginx.conf
(均默认注释,取消注释即可)
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
error_log logs/error.log;
参数配置
根据实际压力测试结果进行调整:
#在主标签端
#根据CPU核数调整工作进程个数
worker_processes 1;
#调整工作进程最大打开文件数(实际允许的最大连接数不超过此值)
worker_rlimit_nofile number;
#调整单个进程允许的最大连接数
events {
worker_connections 1024;
}
#在http下
#禁止在响应头和错误页显示nginx版本信息
server_tokens off;
#在server下增加Set-Cookie和X-Frame-Options响应头(见下一章)
add_header Set-Cookie "HttpOnly"; #http站这么设置,https站设为"Secure"
add_header X-Frame-Options "SAMEORIGIN";#允许在同域名页面的 frame 中嵌套,如设为`DENY`则不允许