Linux常用命令--源代码安装nginx

摘要

  • 虽然绝大部分软件都可以通过rpm或者yum的方式进行安装,但是由于yum中的版本不一定是最新版,或者软件开发商没有将软件放到yum源中,所以也有一些软件需要通过源代码的方式进行安装。

  • 本文以源代码安装nginx为例说明如何通过源代码安装软件。

  • 本文基于CentOS8(x86_64)

安装nginx

  • yum 安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 安装nginx最小依赖
sudo yum install gcc zlib-devel pcre-devel openssl-devel -y

# 如果通过yum安装nginx,只需要执行如下命令
sudo yum install nginx -y # 先安装nginx核心模块,Amazon Linux 2023
sudo amazon-linux-extras install nginx1 # 先安装nginx核心模块,Amazon Linux 2

sudo yum install nginx-all-modules -y # 若需要安装nginx扩展模块,就是动态链接库 .so

# 如果安装了扩展模块,可以通过如下命令查看
$ ls -1 /usr/lib64/nginx/modules/
# 输出
ngx_http_image_filter_module.so
ngx_http_perl_module.so
ngx_http_xslt_filter_module.so
ngx_mail_module.so
ngx_stream_module.so

# 查看 Nginx 当前已编译的模块和动态加载的模块情况
nginx -V

# 加入开机启动
sudo systemctl enable nginx
# 启动nginx
sudo systemctl start nginx
  • 源码安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# 安装nginx最小依赖
sudo yum install gcc zlib-devel pcre-devel openssl-devel -y

# 下载nginx最新稳定版,当前是1.22.1 : https://nginx.org/en/download.html
wget https://nginx.org/download/nginx-1.22.1.tar.gz

# 解压
tar -zxvf nginx-1.22.1.tar.gz

# 进入解压后的目录
cd nginx-1.22.1

# 检查配置并指定安装参数
./configure --with-http_stub_status_module --with-http_ssl_module
# 参数说明
--with-http_ssl_module :安装ssl模块
--with-http_stub_status_module :查看nginx的客户端状态模块

# 输出内容,所有文件的默认路径
Configuration summary
+ using system PCRE library
+ using system OpenSSL library
+ using system zlib library

nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx modules path: "/usr/local/nginx/modules"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"

# 也可以自定义安装路径
./configure --prefix=/usr/local/nginx-1.22.1 --with-http_stub_status_module --with-http_ssl_module
# 参数说明
--prefix=/usr/local/nginx-1.22.1 :安装路径

#输出内容,此时看到所有文件都会被安装到指定的路径
Configuration summary
+ using system PCRE library
+ using system OpenSSL library
+ using system zlib library

nginx path prefix: "/usr/local/nginx-1.22.1"
nginx binary file: "/usr/local/nginx-1.22.1/sbin/nginx"
nginx modules path: "/usr/local/nginx-1.22.1/modules"
nginx configuration prefix: "/usr/local/nginx-1.22.1/conf"
nginx configuration file: "/usr/local/nginx-1.22.1/conf/nginx.conf"
nginx pid file: "/usr/local/nginx-1.22.1/logs/nginx.pid"
nginx error log file: "/usr/local/nginx-1.22.1/logs/error.log"
nginx http access log file: "/usr/local/nginx-1.22.1/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"

# 编译
make
# 或者为了加快编译过程,在多核系统下,可以开启多个线程进行编译,比如 `make -j4` 表示开启4个线程进行编译
make -j4


# 将相关文件拷贝到 --prefix 指定的安装目录中
make install
# 或者多线程安装
make -j4 install
  • 上面就完成了nginx安装,之后我们可以根据需要对配置文件进行修改,并启动nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# 假设我们将nginx安装到了自定义安装路径/usr/local/nginx-1.22.1下
cd /usr/local/nginx-1.22.1
ls -1
# 输出说明
conf :nginx配置文件目录
html :默认web根目录
logs :默认日志输出目录
sbin :启动命令nginx所在目录,make后真正有用的就这个nginx命令,重新编译就是为了重新生成新的nginx命令

# 将nginx命令软连接到系统命令路径下,因为/usr/bin/已经被加入到PATH中了
ln -s /usr/local/nginx-1.22.1/sbin/nginx /usr/bin/
# 查看nginx版本,测试nginx命令是否全局可用
nginx -v
# 输出版本
nginx version: nginx/1.22.1

# 创建nginx用户和组
$ useradd nginx -M -s /sbin/nologin

# 修改配置文件
vim /usr/local/nginx-1.22.1/conf/nginx.conf
# 为了方便演示,这里仅仅修改如下两项内容
# worker用户,这里配置为我们上面创建的nginx用户和nginx组
user nginx nginx;
# 启动2个worker进程
worker_processes 2;


# 检查配置文件是否正确
nginx -t
# 输出
nginx: the configuration file /usr/local/nginx-1.22.1/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx-1.22.1/conf/nginx.conf test is successful

# 如果配置文件被修改了,可以通过该命令重新加载新的配置,而不需要重新启动nginx
nginx -s reload

# 启动nginx
# 会使用默认的配置文件
nginx
# 启动时指定配置文件
nginx -c /usr/local/nginx-1.22.1/conf/nginx.conf


# 查看进程
ps -ef | grep nginx
# 输出
root 18538 1 0 11:11 ? 00:00:00 nginx: master process nginx
nginx 18592 18538 0 11:22 ? 00:00:00 nginx: worker process
nginx 18593 18538 0 11:22 ? 00:00:00 nginx: worker process

# 查看端口
netstat -tunpl | grep nginx
# 输出
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 18538/nginx: master


# 立即停止nginx
nginx -s stop

# 从容停止nginx
kill -QUIT `cat /usr/local/nginx-1.22.1/logs/nginx.pid`

# 立即重启nginx
nginx -s stop # 先关闭
nginx # 再启动

# 平滑重启nginx
kill -HUP `cat /usr/local/nginx-1.22.1/logs/nginx.pid` # 只会重启worker进程

为nginx添加新的模块

  • nginx安装成功后,发现有一些其他模块没有编译进去,或者想额外添加一些模块,这时候就要重新编译nginx。

  • 重新编译之前,需要查看之前安装时的参数

1
2
3
4
5
6
7
$ nginx -V
# 输出
nginx version: nginx/1.22.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx-1.22.1 --with-http_stub_status_module --with-http_ssl_module
  • configure arguments中就是之前编译安装时配置的参数

  • 如添加新的模块http_gzip_static_module

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 进入nginx源码目录
cd /usr/local/soft/nginx-1.22.1

# 检查配置并指定安装参数,使用上面得到的上一次安装的参数,还要将新的模块也加上
./configure --prefix=/usr/local/nginx-1.22.1 --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module

# 编译,注意不要执行 make install,因为我们只需要得到nginx命令,运行make install会覆盖之前的所有目录
make

# 先备份原先的nginx命令
cp /usr/local/nginx-1.22.1/sbin/nginx /usr/local/nginx-1.22.1/sbin/nginx.bak

# 将新生成的nginx命令复制到安装目录中
cp ./objs/nginx /usr/local/nginx-1.22.1/sbin/nginx
# 此时如果遇到提示 cp: 无法创建普通文件"/usr/local/nginx-1.22.1/sbin/nginx": 文本文件忙 , 可以通过如下命令进行复制
cp -rfp ./objs/nginx /usr/local/nginx-1.22.1/sbin/nginx


# 重启nginx
nginx -s stop
nginx


# 查看是否是新的nginx
nginx -V
# 输出
nginx version: nginx/1.22.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx-1.22.1 --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module
  • 安装第三方模块,configure中需要添加--add-module=module_dir,具体查看第三方模块官网说明即可

nginx编译时可以添加哪些参数

  • 以下是通过yum安装nginx时的编译参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# 指定nginx安装路径,至少web根目录在该路径下
--prefix=/usr/share/nginx
# 指定nginx命令安装路径
--sbin-path=/usr/sbin/nginx
# 指定模块安装路径
--modules-path=/usr/lib64/nginx/modules
# 指定主配置文件
--conf-path=/etc/nginx/nginx.conf
# 指定错误日志
--error-log-path=/var/log/nginx/error.log
# 指定访问日志
--http-log-path=/var/log/nginx/access.log
# 设定http客户端请求临时文件路径
--http-client-body-temp-path=/var/lib/nginx/tmp/client_body
# 设定http代理临时文件路径
--http-proxy-temp-path=/var/lib/nginx/tmp/proxy
# 设定http fastcgi临时文件路径,基本就是php缓存
--http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi
# 设定http uwsgi临时文件路径,python缓存路径
--http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi
# 设定http scgi临时文件路径
--http-scgi-temp-path=/var/lib/nginx/tmp/scgi
# master进程ID
--pid-path=/run/nginx.pid
# 锁路径,防止重复启动nginx
--lock-path=/run/lock/subsys/nginx
# worker进程的用户和组
--user=nginx --group=nginx
# 启用动态模块兼容性
--with-compat
# 启用debug日志
--with-debug
# 使用nginx的aio特性会大大提高性能,比如图片站的特点是大量的读io操作,nginx aio不用等待每次io的结果,有助于并发处理大量io和提高nginx处理效率。
# aio的优点就是能够同时提交多个io请求给内核,然后直接由内核的io调度算法去处理这些请求(directio),这样的话,内核就有可能执行一些合并,节约了读取文件的处理时间。
# 异步非阻塞
--with-file-aio
# 调试用,剖析程序性能瓶颈
--with-google_perftools_module
# 作为一个输出过滤器,支持不完全缓冲,分部分响应请求。在nginx响应之前或者之后追加文本内容,比如想在站点底部追加一个js广告或者新增的css样式
--with-http_addition_module
# 认证模块,实现基于一个子请求的结果的客户端授权。如果该子请求返回的2xx响应代码,所述接入是允许的。如果它返回401或403中,访问被拒绝与相应的错误代码。由子请求返回的任何其他响应代码被认为是一个错误。
--with-http_auth_request_module
# WebDAV模块,增加 PUT,DELETE,MKCOL,COPY和MOVE方法。默认情况下为关闭
# 扩展了HTTP 1.1,在GET、POST、HEAD等几个HTTP标准方法以外添加了一些新的方法,使应用程序可直接对Web Server直接读写,并支持写文件锁定(Locking)及解锁(Unlock),还可以支持文件的版本控制。
--with-http_dav_module
# 允许在内存不足的情况下返回204或444码
--with-http_degradation_module
# 提供寻求内存使用基于时间的偏移量文件
--with-http_flv_module
# 它为不支持“gzip”编码方法的客户端解压具有“Content-Encoding: gzip”头的响应。
--with-http_gunzip_module
# 在线实时压缩输出数据流
--with-http_gzip_static_module
# 传输JPEG/GIF/PNG 图片的一个过滤器)(默认为不启用。gd库要用到)
--with-http_image_filter_module=dynamic
# 多媒体模块
--with-http_mp4_module
--with-http_perl_module=dynamic
# nginx显示随机首页模块
--with-http_random_index_module
# Nginx获取真实IP模块,这个模块允许从请求标头更改客户端的IP地址值,默认为关
--with-http_realip_module
# nginx安全下载模块
--with-http_secure_link_module
# nginx中文文档
--with-http_slice_module
# 安装ssl模块
--with-http_ssl_module
# 查看nginx客户端状态模块
--with-http_stub_status_module
# 允许用一些其他文本替换nginx响应中的一些文本
--with-http_sub_module
# 提供HTTP2支持,要使用http2,还需要同时启用ssl服务,nginx1.19.1之前的版本不支持http2
--with-http_v2_module
# 过滤转换XML请求
--with-http_xslt_module=dynamic
# 启用POP3/IMAP4/SMTP代理模块支持
--with-mail=dynamic --with-mail_ssl_module
# 支持正则,使nginx支持http rewrite模块
--with-pcre
# 编译PCRE包含“just-in-time compilation”
--with-pcre-jit
# TCP\UDP负载均衡模块。nginx从1.9.0开始,新增加了一个stream模块,用来实现四层协议的转发、代理或者负载均衡等。
--with-stream=dynamic --with-stream_ssl_module --with-stream_ssl_preread_module
# 支持多线程模块
--with-threads
# 设置C编译器参数
--with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic'
# 设置连接文件参数
--with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'
  • 其它参数

1
2
3
4
5
6
7
8
9
10
11
12
# 启用ipv6支持
--with-ipv6
# SPDY可以缩短网页的加载时间
--with-http_spdy_module
# 该模块创建基于与MaxMind GeoIP二进制文件相配的客户端IP地址的ngx_http_geoip_module变量
--with-http_geoip_module
# 计算和检查要求所需的安全链接网址
--with-http_secure_link_module
# 禁用http cache功能
--without-http-cache
# 启用外部模块支持
--add-module=module_dir

nginx编译安装出现的常见错误

  • 参照yum安装nginx的配置进行手工编译

1
2
3
4
5
6
7
8
./configure --prefix=/usr/local/nginx-1.22.1  --user=nginx --group=nginx --with-compat --with-debug \
--with-file-aio --with-google_perftools_module --with-http_addition_module --with-http_auth_request_module \
--with-http_dav_module --with-http_degradation_module --with-http_flv_module --with-http_gunzip_module \
--with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_mp4_module \
--with-http_perl_module=dynamic --with-http_random_index_module --with-http_realip_module \
--with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module \
--with-http_sub_module --with-http_v2_module --with-http_xslt_module=dynamic --with-mail=dynamic --with-mail_ssl_module \
--with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-stream_ssl_preread_module --with-threads
  • ./configure: error: the HTTP rewrite module requires the PCRE library.You can either disable the module by using --without-http_rewrite_module option, or install the PCRE library into the system, or build the PCRE library statically from the source with nginx by using --with-pcre= option.

1
yum install pcre pcre-devel
  • ./configure: error: SSL modules require the OpenSSL library.You can either do not enable the modules, or install the OpenSSL library into the system, or build the OpenSSL library statically from the source with nginx by using --with-openssl= option.

1
yum install openssl openssl-devel
  • ./configure: error: the HTTP XSLT module requires the libxml2/libxslt libraries. You can either do not enable the module or install the libraries.

1
yum install libxml2 libxml2-devel libxslt libxslt-devel
  • ./configure: error: the HTTP image filter module requires the GD library. You can either do not enable the module or install the libraries.

1
yum install gd gd-devel
  • ./configure: error: perl module ExtUtils::Embed is required

1
yum install perl-ExtUtils-Embed
  • ./configure: error: the Google perftools module requires the Google perftools library. You can either do not enable the module or install the library

1
yum install google-perftools google-perftools-devel

Nginx 配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# 全局参数设置
#user nobody;
user nginx; # worker进程用户
worker_processes 2; # 设置nginx启动进程的数量,一般设置成与逻辑cpu数量相同

#error_log logs/error.log; # 指定错误日志
#error_log logs/error.log notice;
#error_log logs/error.log info;

# worker_rlimit_nofile 102400; # 设置一个nginx进程能打开的最大文件数
# pid /run/nginx.pid;

# 加载模块配置文件
# include conf.d/modules/*.conf;
load_module modules/ngx_stream_module.so; # 加载stream模块

events { # 事件配置
worker_connections 1024; # 设置一个进程的最大并发连接数
# use epoll; # 事件驱动类型
}

# tcp反向代理示例,与http配置同级
stream {
# include conf.d/stream/*.conf
include conf.d/stream/redis.conf
}

# http 服务相关设置
http {
include mime.types; # 关联mime类型,关联资源的媒体类型(不同的媒体类型的打开方式)
default_type application/octet-stream; # 根据文件的后缀来匹配相应的MIME类型,并写入Response header,导致浏览器播放文件而不是下载

# 访问日志格式,日志参数说明见下文
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; # 用于开启文件高效传输模式,一般设置为on,若nginx是用来进行磁盘IO负载应用时,可以设置为off,降低系统负载
tcp_nopush on; # 减少网络报文段数量,当有数据时,先别着急发送, 确保数据包已经装满数据, 避免了网络拥塞
tcp_nodelay on; # 提高I/O性能,确保数据尽快发送, 提高可数据传输效率
# gzip on; # 是否开启 gzip 压缩
keepalive_timeout 65; # 设置长连接的超时时间,请求完成之后还要保持连接多久,不是请求时间多久,目的是保持长连接,减少创建连接过程给系统带来的性能损耗,类似于线程池,数据库连接池
types_hash_max_size 4096; # 影响散列表的冲突率。types_hash_max_size 越大,就会消耗更多的内存,但散列key的冲突率会降低,检索速度就更快。types_hash_max_size越小,消耗的内存就越小,但散列key的冲突率可能上升


# 加载子配置文件
# include /usr/local/nginx-1.22.1/conf/conf.d/*.conf;

# http反向代理示例
include conf.d/http/tomcat.conf


# 虚拟服务器的相关设置
server {
listen 80; # 设置监听的端口
server_name localhost; # 设置绑定的主机名、域名或ip地址
#charset koi8-r; # 设置编码字符

#access_log logs/host.access.log main;

location / {
root html; # 设置服务器默认网站的根目录位置
index index.html index.htm; # 设置默认打开的文档
}
# error_page 404 /404.html;

error_page 500 502 503 504 /50x.html; # 设置错误信息返回页面
location = /50x.html {
root html
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}

}

# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;

# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;

# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;

# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;

# location / {
# root html;
# index index.html index.htm;
# }
#}

}
  • 日志格式参数说明

参数名 含义 示例值
remote_addr 客户端IP地址 192.168.0.1
remote_user 客户端用户(通常为“-”) -
time_local 日志记录时间(本地时区) 02/May/2023:14:43:58 +0800
request 请求行,如 “GET /index.html …” “GET / HTTP/1.1”
status HTTP状态码 200
body_bytes_sent 发送给客户端的主体字节数 612
http_referer 来源页面 “https://domain.com/”
http_user_agent 用户代理信息 “Mozilla/5.0 (…)”
http_x_forwarded_for 代理转发的原始IP地址链 192.168.0.1, 10.0.0.2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
upstream tomcats { # 定义名为 tomcats 的后端服务器组,用于负载均衡
server 192.168.0.1:8080; # 后端 Tomcat 节点 1
server 192.168.0.2:8080; # 后端 Tomcat 节点 2
}
server {
listen 8088; # 监听本机 8088 端口
server_name localhost tomcat.domain; # 匹配的域名,多个用空格分隔

location / { # 匹配所有请求路径
proxy_pass http://tomcats; # 把请求转发到上面的 upstream tomcats 组
proxy_redirect default; # 按默认规则改写后端响应里的 Location/Refresh 头,使其指向代理地址
proxy_http_version 1.1; # 与后端通信使用 HTTP/1.1,便于 keepalive

proxy_set_header Host $host; # 把原始 Host 传给后端,后端才能按域名正确响应
proxy_set_header X-Real-IP $remote_addr; # 把客户端真实 IP 传给后端
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 追加代理链路中的客户端 IP
proxy_set_header X-Forwarded-Proto $scheme; # 告诉后端原始请求是 http 还是 https
proxy_max_temp_file_size 0; # 禁止把超大响应写到临时文件,0 表示全部放内存或直接转发

# 限制客户端上传体积,防止过大请求占满磁盘/内存
client_max_body_size 10m; # 允许的最大请求体,超过返回 413
client_body_buffer_size 128k; # 请求体先放内存的缓冲区大小,超出部分写临时文件

proxy_connect_timeout 90; # 与后端建立连接的超时时间(秒)
proxy_send_timeout 90; # 向后端发送请求的超时时间(秒)
proxy_read_timeout 90; # 等待后端响应的超时时间(秒)
proxy_buffering off; # 关闭响应缓冲,后端数据立即转发给客户端
proxy_request_buffering off; # 关闭请求体缓冲,边收边转发给后端(流式上传/CLI 需要)
proxy_set_header Connection ""; # 清空 Connection 头,配合 HTTP/1.1 启用与后端的 keepalive

}
}
  • redis.conf

1
2
3
4
5
6
7
8
9
10
11
upstream redis{
server 192.168.0.1:6379;
server 192.168.0.2:6379;
}

server {
listen 8888; #监听本机所有IP的8888端口
proxy_connect_timeout 10s; #连接超时时间
proxy_timeout 10s; #转发超时时间
proxy_pass redis; #这个名称和要上面 upstream redis 对应
}
  • upstream配置说明

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
upstream还可以为每个设备设置状态值,这些状态值的含义分别如下:
- down:表示当前的server暂时不参与负载.
- weight:默认为1,weight越大,负载的权重就越大。
- max_fails:允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误.
- fail_timeout : max_fails次失败后,暂停的时间。
- backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。
- ip_hash(访问ip):每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。

示例:
upstream tomcats{
ip_hash;
server 127.0.0.1:9001 down;
server 127.0.0.1:9002 backup;
server 127.0.0.1:9003 weight=2;
server 127.0.0.1:9004 max_fails=2 fail_timeout=60s;
}

一个https配置示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
upstream jenkins {
server 192.168.0.1:8080; # 后端 节点 1
server 192.168.0.2:8080; # 后端 节点 2
}

server {
listen 80; # 监听 IPv4 的 80 端口(HTTP)
listen [::]:80; # 监听 IPv6 的 80 端口
server_name jenkins.server.com; # 匹配的域名
rewrite ^(.*)$ https://${server_name}$1 permanent; # 把所有 HTTP 请求 301 永久重定向到 HTTPS
}

server {
listen 443 ssl; # 监听 443 端口并启用 SSL/TLS
server_name jenkins.server.com; # 匹配的域名,需与证书 CN/SAN 一致

ssl_certificate /etc/nginx/ssl/jenkins.server.com.pem; # 证书文件(公钥 + 中间证书)
ssl_certificate_key /etc/nginx/ssl/jenkins.server.com.key; # 证书私钥

location / { # 匹配所有请求路径
proxy_pass http://jenkins; # 把请求转发到上面的 upstream jenkins 组
proxy_redirect off; # 不改写后端响应里的 Location/Refresh 头
proxy_set_header Host $host; # 把原始 Host 传给后端
proxy_set_header X-Real-IP $remote_addr; # 把客户端真实 IP 传给后端
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 追加代理链路中的客户端 IP
client_max_body_size 1024m; # 允许的最大请求体(Jenkins 上传插件/产物较大)
client_body_buffer_size 256k; # 请求体先放内存的缓冲区大小,超出部分写临时文件
proxy_connect_timeout 10; # 与后端建立连接的超时时间(秒)
proxy_send_timeout 15; # 向后端发送请求的超时时间(秒)
proxy_read_timeout 15; # 等待后端响应的超时时间(秒)
proxy_buffer_size 4k; # 接收后端响应头的缓冲区大小
proxy_buffers 4 32k; # 接收后端响应体:4 个缓冲区,每个 32k
proxy_busy_buffers_size 64k; # 正在向客户端发送时,允许占用的忙碌缓冲区大小
proxy_temp_file_write_size 64k; # 响应过大需写临时文件时,每次写入的大小
}
}

配置为系统服务

  • yum安装自带系统服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# 进入service服务目录
cd /etc/init.d

# 创建并编辑nginx文件
vim nginx
# 写入如下内容:
set -e
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="nginx daemon"
NAME=nginx
DAEMON=/usr/local/nginx-1.22.1/sbin/$NAME
SCRIPTNAME=/etc/init.d/$NAME
test -x $DAEMON || exit 0
d_start(){
$DAEMON || echo -n " already running"
}
d_stop() {
$DAEMON -s quit || echo -n " not running"
}
d_reload() {
$DAEMON -s reload || echo -n " counld not reload"
}
d_status() {
pid=`ps ax | grep -i 'nginx: master' | grep -v grep | awk '{print $1}'`
if [ -z "$pid" ] ; then
echo -n "No nginx-server running."
else
echo -n "The nginx-server(${pid}) is running"

fi
}
case "$1" in
start)
echo -n "Starting $DESC:$NAME"
d_start
echo "."
;;
stop)
echo -n "Stopping $DESC:$NAME"
d_stop
echo "."
;;
reload)
echo -n "Reloading $DESC configuration..."
d_reload
echo "reloaded."
;;
restart)
echo -n "Restarting $DESC: $NAME"
d_stop
sleep 2
d_start
echo "."
;;
status)
echo -n "Check $DESC status..."
d_status
echo "."
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart|reload|status}" >&2
exit 3
;;
esac
exit 0

# 授予执行权限
chmod +x nginx

# 服务管理
service nginx start
service nginx stop
service nginx status
service nginx reload
service nginx restart

# 这里使用 systemctl 同样可以,因为systemctl兼容service
systemctl start nginx

# 加入开机启动
chkconfig nginx on
systemctl enable nginx

# 关闭开启启动
chkconfig nginx off
systemctl disable nginx