背景描述:
nginx版本:nginx-1.18.0
nginx安装目录:/app/nginx/
nginx安装包文件位置:/app/nginx/nginx-1.18.0.tar.gz
nginx日志分割及清理脚本目录:/app/nginx/cut_nginx_log.sh
1. 安装依赖包:(需使用root用户安装)
yum install -y pcre-devel zlib-devel openssl-devel gcc*
2. 安装nginx:(请使用普通用户执行下面步骤)
将压缩包拷贝至/app/目录下,使用命令 ls /app 你可以看到:nginx-1.18.0.tar.gz
tar -xzvf /app/nginx-1.18.0.tar.gz -C /app
mkdir /app/nginx
cd /app/nginx-1.18.0/
./configure \
--prefix=/app/nginx/ \
--with-http_ssl_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-stream \
--with-ipv6
cd /app/nginx-1.18.0/ && make
cd /app/nginx-1.18.0/ && make install
3.自启动nginx
添加 vim /app/auto_start.sh配置文件
#vim /app/auto_start.sh
#!/bin/bash
cc=`ps -ef|grep nginx|grep process|grep -v grep|wc -l`
echo nginx process is $cc
if [ $cc -gt 0 ]
then /data/nginx/sbin/nginx -s quit
fi
/app/nginx/sbin/nginx -c /app/nginx/conf/nginx.conf
4.nginx日志分割及定期清理
Nginx:可参考以下脚本,每天生成一个日志,并删除30天之前的日志;将脚本加入到系统定时任务每天凌晨执行
$ cd /app/nginx/
$ vim cut_nginx_log.sh
#!/bin/bash
LOGS_PATH=/app/nginx/logs
YESTERDAY=$(date -d "yesterday" +%Y-%m-%d)
KEEPTIME=$(date -d "-30 days" +%Y-%m-%d)
mv ${LOGS_PATH}/access.log ${LOGS_PATH}/access_${YESTERDAY}.log
mv ${LOGS_PATH}/error.log ${LOGS_PATH}/error_${YESTERDAY}.log
kill -USR1 $(cat /app/nginx/logs/nginx.pid)
##delete logs 30 days ago
rm -f ${LOGS_PATH}/access_${KEEPTIME}.log
rm -f ${LOGS_PATH}/error_${KEEPTIME}.log
加入到系统定时任务,注意脚本要赋予可执行权限
$ crontab -e
0 0 * * * /app/nginx/cut_nginx_log.sh
5. 使用nginx(请使用普通用户执行下面步骤)
/app/nginx/sbin/nginx -c /app/nginx/conf/nginx.conf #指定启动加载的nginx配置文件并启动nginx
/app/nginx/sbin/nginx -s quit #等待nginx进程处理完任务后停止nginx(建议使用)
/app/nginx/sbin/nginx -s stop #直接停止nginx,相当于kill pid
/app/nginx/sbin/nginx -s reload #重新加载nginx配置文件
/app/nginx/sbin/nginx -s quit && /app/nginx/sbin/nginx -c /app/nginx/conf/nginx.conf #先停止在启动(建议使用)
6. 测试
wget http://127.0.0.1:8080
curl 127.0.0.1:8080
netstat -anp|grep 8080
7. 卸载nginx
rm -rf /app/nginx
8.运维nginx
ps -ef | grep nginx #查看nginx进程
netstat -anp | grep “nginx主进程号” #查看nginx启动端口
./nginx -t #测试配置是否正确
9.查看nginx安装位置
cd /proc/'nginx进程号'
ls -ail #查看全部进程详细信息及inode信息
查看exe对应的链接即为程序路径
10.查看系统nginx配置文件目录
locate nginx.conf
11.新增模块
mv /app/nginx/sbin/nginx /app/nginx/sbin/nginx_bak
cd /app/nginx-1.18.0/
./configure --prefix=/app/nginx/ --with-http_ssl_module --with-http_gzip_static_module --with-http_stub_status_module --with-stream
make
cp -a /app/nginx-1.18.0/objs/nginx /app/nginx/sbin/
12.配置文件新增V6支持
server {
listen 8080;
listen [::]:8080 ipv6only=on;
}
13.nginx配置
user www www;
worker_processes 2;#设置值和CPU核心数一致
error_log logs/error.log; #日志位置和日志级别
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid logs/nginx.pid;
events {
use epoll;
worker_connections 2048;
}
http {
include mime.types;
default_type application/octet-stream;
#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 65;
# gzip压缩功能设置
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 6;
gzip_types text/html text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml;
gzip_vary on;
# http_proxy 设置
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 75;
proxy_send_timeout 75;
proxy_read_timeout 75;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_temp_path /usr/local/nginx/proxy_temp 1 2;
# 设定负载均衡后台服务器列表
upstream backend {
#ip_hash;
server 192.168.10.100:8080 max_fails=2 fail_timeout=30s ;
server 192.168.10.101:8080 max_fails=2 fail_timeout=30s ;
}
# 很重要的虚拟主机配置
server {
listen 80;
server_name itoatest.example.com;
root /apps/oaapp;
charset utf-8;
access_log logs/host.access.log main;
#对 / 所有做负载均衡+反向代理
location / {
root /apps/oaapp;
index index.jsp index.html index.htm;
proxy_pass http://backend;
proxy_redirect off;
# 后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
}
#静态文件,nginx自己处理,不去backend请求tomcat
location ~* /download/ {
root /apps/oa/fs;
}
location ~ .*/.(gif|jpg|jpeg|bmp|png|ico|txt|js|css)$
{
root /apps/oaapp;
expires 7d;
}
location /nginx_status {
stub_status on;
access_log off;
allow 192.168.10.0/24;
deny all;
}
location ~ ^/(WEB-INF)/ {
deny all;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
## 其它虚拟主机,server 指令开始
}
14.nginx配置TCP访问
worker_processes 2;
error_log logs/error.log;
pid logs/nginx.pid;
events {
worker_connections 2048;
}
stream {
log_format proxy '$remote_addr [$time_local] '
'$protocol $status $bytes_sent $bytes_received '
'$session_time "$upstream_addr" '
'"$upstream_bytes_sent" "$upstream_bytes_received" "$upstream_connect_time" '
'$remote_addr $remote_port $server_addr $server_port';
access_log /data/nginx/logs/access.log proxy ;
upstream stream_led {
server 10.0.0.1:53016; # 转发的目的地址和端口
}
# 提供转发的服务,即访问localhost:9001,会跳转至代理socket_proxy指定的转发地址
server {
listen 9001;
proxy_connect_timeout 2s;
proxy_timeout 5s;
proxy_pass stream_led;
}
}