编译安装Nginx

编译安装Nginx

Nginx简介

Nginx是一个高性能的HTTP和反向代理Web服务器,同时也提供IMAP/POP3/SMTP服务。Nginx是一款轻量级的Web服务器/反向代理服务器及电子邮件(IMAP/POP3/SMTP)代理服务器。Nginx的特点是:占有内存少,并发能力强。Nginx专门为性能优化而开发,性能是最重要的考量,非常注重效率,事实上Nginx的并发能力在同类型的网页服务器种表现较好,能够支持高达50000个并发连接响应数。Nginx的官网下载地址https://nginx.org/en/download.html

编译安装

1.1、编译前准备

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
# 统一软件放在/app/soft下
root@VM-0-14-ubuntu:/app/tomcat# cd /app/soft/
root@VM-0-14-ubuntu:/app/soft# ls
jdk1.8.0_201 kc_tomcat_all.tar.xz mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz redis-5.0.14
jdk-8u201-linux-x64.tar.gz mysql-5.7.28-linux-glibc2.12-x86_64 nginx-1.20.2.tar.gz redis-5.0.14.tar.gz
root@VM-0-14-ubuntu:/app/soft# tar xf nginx-1.20.2.tar.gz
root@VM-0-14-ubuntu:/app/soft# cd nginx-1.20.2/
root@VM-0-14-ubuntu:/app/soft/nginx-1.20.2#
# 安装Nginx需要的依赖组件
root@VM-0-14-ubuntu:/app/soft/nginx-1.20.2# apt-get -y install zlib1g-dev libpcre3 libpcre3-dev openssl libssl-dev gcc make
# 自定义nginx显示名称
root@VM-0-14-ubuntu:/app/soft/nginx-1.20.2# vim src/http/ngx_http_header_filter_module.c
...
# 默认是nginx改成你自己想要的名称
static u_char ngx_http_server_string[] = "Server: gzcloud" CRLF;
..
# 自定义版本号
root@VM-0-14-ubuntu:/app/soft/nginx-1.20.2# vim src/core/nginx.h
#define nginx_version 4622062 # 需要修改
#define NGINX_VERSION "6.25.4" # 需要修改
#define NGINX_VER "gzcloud/" NGINX_VERSION # 需要修改

#ifdef NGX_BUILD
#define NGINX_VER_BUILD NGINX_VER " (" NGX_BUILD ")"
#else
#define NGINX_VER_BUILD NGINX_VER
#endif

#define NGINX_VAR "GZCLOUD" # 需要修改
#define NGX_OLDPID_EXT ".oldbin"


#endif /* _NGINX_H_INCLUDED_ */

# 修改错误页面显示的版本信息
root@VM-0-14-ubuntu:/app/soft/nginx-1.20.2# vim src/http/ngx_http_special_response.c
...
36 "<hr><center>gzcloud</center>" CRLF
...

1.2、正式编译

前面准备工作都完成后接下来就开始正式编译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
root@VM-0-14-ubuntu:/app/soft/nginx-1.20.2# ./configure --prefix=/app/nginx  --user=nginx --group=nginx \
--error-log-path=/app/nginx/logs/error.log \
--http-log-path=/app/nginx/logs/access.log \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--with-http_ssl_module \
--with-stream \
--with-http_v2_module \
--with-http_auth_request_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_realip_module \
--with-stream_realip_module \
--with-stream_ssl_module \
--with-file-aio \
--with-threads
...
Configuration summary
+ using threads
+ using system PCRE library
+ using system OpenSSL library
+ using system zlib library

nginx path prefix: "/app/nginx"
nginx binary file: "/app/nginx/sbin/nginx"
nginx modules path: "/app/nginx/modules"
nginx configuration prefix: "/app/nginx/conf"
nginx configuration file: "/app/nginx/conf/nginx.conf"
nginx pid file: "/var/run/nginx/nginx.pid"
nginx error log file: "/app/nginx/logs/error.log"
nginx http access log file: "/app/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"
# 编译安装
root@VM-0-14-ubuntu:/app/soft/nginx-1.20.2# make -j 4 && make install
...
test -d '/app/nginx/html' \
|| cp -R html '/app/nginx'
test -d '/app/nginx/logs' \
|| mkdir -p '/app/nginx/logs'
make[1]: Leaving directory '/app/soft/nginx-1.20.2'

1.3、配置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
70
# 设置nginx的环境变量
root@VM-0-14-ubuntu:/app/soft/nginx-1.20.2# echo 'export PATH=/app/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
root@VM-0-14-ubuntu:/app/soft/nginx-1.20.2# source /etc/profile.d/nginx.sh
# 配置nginx主配置文件
root@VM-0-14-ubuntu:/app/nginx/conf# vim nginx.conf
user root;
worker_processes 2;
worker_cpu_affinity 0001 0010;
worker_rlimit_nofile 65535;

events {
use epoll;
worker_connections 100000;
multi_accept on;
}

http {
include mime.types;
default_type application/octet-stream;
log_format access_json '{"@timestamp":"$time_iso8601",'
'"host":"$server_addr",'
'"clientip":"$remote_addr",'
'"size":$body_bytes_sent,'
'"responsetime":$request_time,'
'"upstreamtime":"$upstream_response_time",'
'"upstreamhost":"$upstream_addr",'
'"http_host":"$host",'
'"url":"$uri",'
'"domain":"$host",'
'"xff":"$http_x_forwarded_for",'
'"referer":"$http_referer",'
'"tcp_xff":"$proxy_protocol_addr",'
'"status":"$status"}';
access_log /app/nginx/logs/access.log access_json;
include /app/nginx/conf/conf.d/*.conf;

sendfile on;
tcp_nopush on;
server_tokens off;
client_max_body_size 50m;
keepalive_timeout 360;

server {
listen 80 default_server;
server_name _;
return 444;
location / {
root html;
index index.html index.htm;
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
# 创建虚拟主机配置文件的存放路径
root@VM-0-14-ubuntu:/app/nginx/conf# mkdir conf.d
# 配置一个测试页面
root@VM-0-14-ubuntu:/app/nginx/conf/conf.d# vim web.conf
server {
listen 80;
server_name www.xxxx.com;

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

1.4、启动Nginx

启动Nginx服务就很简单了 上一步配置好了环境变量后直接就可以在命令行的任意一个路径下输入nginx启动

1
2
3
4
5
6
7
8
9
10
# nginx配置配置好后一定要记得先检查配置文件是否有语法错误
root@VM-0-14-ubuntu:/app/nginx/conf# nginx -t
nginx: the configuration file /app/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /app/nginx/conf/nginx.conf test is successful
root@VM-0-14-ubuntu:/app/nginx/conf# nginx
root@VM-0-14-ubuntu:/app/nginx/conf# lsof -i:80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 124077 root 6u IPv4 411998 0t0 TCP *:http (LISTEN)
nginx 124078 root 6u IPv4 411998 0t0 TCP *:http (LISTEN)
nginx 124079 root 6u IPv4 411998 0t0 TCP *:http (LISTEN)

1.5、验证访问


编译安装Nginx
https://www.dklwj.com/2023/03/Compile-and-install-Nginx.html
作者
阿伟
发布于
2023年3月8日
许可协议