nginx-install ¶
编译安装 nginx ¶
Nginx-Web 安装时可以指定很多的模块,默认需要安装 rewrite 模块,需要系统有 PCRE 库,安装 PCRE 支持 rewrite 功能。以下为安装 Nginx-Web 服务器的方法,注意 Nginx 整合 PCRE 库,需要指定 PCRE 源码目录,而不是 PCRE 编译完成之后的路径,否则会报错。
1. 安装 PCRE 库
yum install pcre-devel pcre openssl-devel gcc-c++ -y
2. 下载并解压 nginx 源码
cd /usr/src/ && wget -c https://nginx.org/download/nginx-1.20.1.tar.gz && tar -xf nginx-1.20.1.tar.gz
3. 进入解压目录,使用 sed 修改 Nginx 版本信息为 JWS
cd nginx-1.20.1 && sed -i -e 's/1.20.1//g' -e 's/nginx\//JWS/g' -e 's/"NGINX"/"JWS"/g' src/core/nginx.h
4. 预编译 Nginx
useradd www && ./configure --user=www --group=www --prefix=/etc/nginx --with-http_stub_status_module --with-http_ssl_module
5. 预编译成功后,执行 make 命令进行编译
make && make install
1. 检查 Nginx 配置文件是否正确,返回 OK 即正确
/etc/nginx/sbin/nginx -t && /etc/nginx/sbin/nginx
2. 启动 Nginx,查看是否启动
ps -axuf|grep nginx
3. 查看返回信息
> curl localhost -I
HTTP/1.1 200 OK
Server: JWS
Date: Fri, 10 May 2024 06:03:04 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Fri, 10 May 2024 05:57:25 GMT
Connection: keep-alive
ETag: "663db745-264"
Accept-Ranges: bytes
1. 查看 nginx 进程
ps -axuf|grep nginx
2. 平滑启动 Nginx
平滑启动的意思在不停止 Nginx 的情况下,重启 Nginx ,重新加载配置文件,启动新的工作线程,
1. 检测 nginx 配置是否正确
/etc/nginx/sbin/nginx -t
2. 重启 nginx
/etc/nginx/sbin/nginx -s reload
3. 停止 nginx
/etc/nginx/sbin/nginx -s stop # 方法1
pkill nginx # 方法2
#其中进程文件路径在配置文件 nginx.conf 中可以找到
#完美停止旧的工作线程
# 平滑启动
kill -HUP 'cat /var/run/nginx.pid'
#完美停止Nginx
kill - QUIT 'cat /var/run/nginx.pid'
#快速停止Nginx
kill - TERM 'cat /var/run/nginx.pid'
#或者
kill - INT 'cat /var/run/nginx.pid'
#完美停止工作进程(主要用于平滑升级)
kill - WINCH 'cat/var/run/nginx.pid'
#强制停止Nginx
pkill - 9 nginx
1. NginxWeb 软件定期更新,以下为将低版本升级或者将高坂本降级的方法,一般分为四个步骤:软件下载、预编译、编译、配置:
1. 下载新版本
wget -c https://nginx.org/download/nginx-1.4.2.tar.gz && tar -xf nginx-1.4.2.tar.gz
3. 预编译 Nginx
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
4. 预编译成功后,执行 make 命令进行编译
make && make install
5. 备份旧版本的 Nginx 可执行文件,复制新版本的 Nginx 可行文件
mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old
cp objs/nginx /usr/local/nginx/sbin/
/usr/local/nginx/sbin/nginx -s reload
6. 验证 Nginx 是否升级成功
/usr/local/nginx/sbin/nginx -V # 显示为新版本
Nginx 配置文件优化 ¶
user www;
worker_processes auto;
pid logs/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 102400;
}
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;
error_log logs/error.log notice; # 全局错误日志及 PID 文件,错误日志定义等级,[ debug | info | notice | warn | error | crit ]
sendfile on; # sendfile 指令指定 Nginx 是否调用 sendfile 函数 (zerocopy方式) 来输出文件
tcp_nopush on; # 防止网络阻塞,在一个数据包里发送所有头文件,而不一个接一个的发送
keepalive_timeout 65; # keepalive 超时时间,客户端到服务端的连接持续有效时间,可避免重新建立连接
tcp_nodelay on; # 不要缓存数据,提高数据的实时响应性
proxy_connect_timeout 90; # Nginx 跟后端服务器连接超时时间(代理连接超时)
proxy_send_timeout 90; # 后端服务器数据回传时间(代理发送超时)
server_names_hash_max_size 1024;
server_names_hash_bucket_size 1024;
server_tokens off;
proxy_read_timeout 90; # 连接成功后,后端服务器响应时间(代理接收超时)
keepalive_requests 300;
#gzip on;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}