nginx安装-wip

Nginx 安装、配置文档

开始之前

本文档基于CentOS Linux 7 (Core)编写

  • Nginx源码包
  • 互联网连接

编译安装

nginx-1.20.1 的源码包均存放至 /usr/local/src/ 目录下

路径参数

根据实际使用需要修改

释义 参数名 本文配置 备注
服务目录 –prefix /usr/local/nginx
NGINX 可执行文件的位置与名字 –sbin-path /usr/sbin/nginx 非必须,默认为[prefix]/sbin/nginx
存放主进程PID文件位置 –pid-path /usr/local/nginx/nginx.pid 非必须,默认为[prefix]/logs/nginx.pid
错误日志位置 –error-log-path /var/log/nginx/error.log 非必须,默认为[prefix]/logs/error.log
访问日志位置 –http-log-path /var/log/nginx/access.log 非必须,默认为[prefix]/logs/access.log
1
2
3
4
5
6
7
8
9
10
11
12
13
# 解压源码包
tar -xvf /usr/local/src/nginx-1.20.1.tar.gz

# 配置编译基本信息及
cd /usr/local/src/nginx-1.20.1

./configure --prefix=/usr/local/nginx --sbin-path=/usr/sbin/nginx --pid-path=/usr/local/nginx/nginx.pid --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --with-http_gzip_static_module --with-http_stub_status_module --with-http_ssl_module --with-file-aio --with-http_realip_module

# 编译安装
make && make install

# 为nginx创建软链接
ln -s /usr/sbin/nginx /bin/nginx

测试安装

1
2
3
4
5
# 测试
nginx -t
# 若输出以下信息,即表示nginx配置无误
$ nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
$ nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

配置Nginx

目录准备

1
2
3
4
5
6
# 创建站点配置文件夹
mkdir /usr/local/nginx/vhost -p
# 创建SSL证书存放文件夹
mkdir /usr/local/nginx/cert -p
# 创建站点存放文件夹
mkdir /usr/local/web -p

主配置修改

1
2
# 编辑Nginx主配置文件
vim /usr/local/nginx/conf/nginx.conf

无备注版配置文件详见同级目录

nginx.conf

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
# 使用www用户运行Nginx
# user www;

# 工作进程数 (与CPU核心数量一致)
worker_processes 4;

# 错误日志存放
error_log /var/log/nginx/error.log;

# 进程PID
pid /usr/local/nginx/nginx.pid;

events {
# 单个工作线程最大并发连接数
worker_connections 1024;
}


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 /var/log/nginx/access.log main;

# 设置代理请求头 (反向代理携带客户端实际IP至后端...)
proxy_set_header Host $host;
proxy_set_header Cookie $http_cookie;
proxy_set_header X-Forwarded-For $remote_addr;

# 使用sendfile()函数提高Nginx静态资源托管效率
sendfile on;

# 数据包会累计到一定大小之后才会发送,减小了额外开销,提高网络效率 (需sendfile启用)
tcp_nopush on;

# 禁用 Nagle 算法,尽快发送数据,降低时间消耗 (针对处于keep-alive状态的TCP连接启用)
tcp_nodelay on;

# keep-alive客户端连接在服务器端保持开启的超时值
keepalive_timeout 300s 300s;
# keep-alive连接上可以服务的最大请求数
keepalive_requests 10000;

# 客户端建立连接后发送请求体的超时时间
client_body_timeout 20s;
# 客户端建立连接后发送请求头的超时时间
client_header_timeout 10s;
# 客户端传输数据的超时时间
send_timeout 120s;

# 单个IP、会话同时存在的连接数的限制
limit_conn_zone $binary_remote_addr zone=addr:10m;
# 单个IP、会话同时存在的请求数的限制
limit_req_zone $binary_remote_addr zone=one:10m rate=5r/s;
# 限制并发连接数
limit_conn perip 20;
# 限制下载带宽
limit_rate 10m;

# 开启gzip模块
gzip on;

# 防止页面被其他网站嵌套
add_header X-Frame-Options SAMEORIGIN;

# 停用自动目录索引
autoindex off;

# 错误页配置
include /usr/local/nginx/tools/errorPage.conf;

# 导入站点配置文件
include /usr/local/nginx/vhost/*;
}

站点配置模板

此处以项目名为web-manager,域名为admin.aeroz.cn作为案例

在此之前

将构建后的前端页面放入/usr/local/web/web-manager目录下

HTTP
1
2
3
4
# 进入站点配置文件目录
cd /usr/local/nginx/vhost
# 使用HTTP协议的配置文件
vi web-manager.conf

web-manager.conf

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
server {
listen 80;
listen [::]:80; #ipv6 可选的
server_name admin.aeroz.cn;

# 域名根目录
root /usr/local/web/web-manager;
# 入口文件
index index.html;
# 适用于使用history路由模式的Vue项目
try_files $uri $uri/ /index.html;


# (可选) 域名子站点,有多种配置方式,此处仅列举其中一种
location /info {
alias /usr/local/web/web-manager/info;
index info.html;
}

# 反向代理
location /api {
proxy_pass http://127.0.0.1:8080;
client_max_body_size 512m;
}
}

启动/重载/停止

1
2
3
4
5
6
7
8
9
10
# 启动Nginx
nginx -s start

# 重载Nginx配置文件
# 重载之前,务必使用nginx -t进行测试
nginx -t
nginx -s reload

# 停止Nginx
nginx -s stop

将Nginx注册为系统服务

1
vi /lib/systemd/system/nginx.service

nginx.service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/usr/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target
1
2
3
4
5
6
7
8
9
10
# 重载配置
systemctl daemon-reload

# 设置为开机启动
systemctl enable nginx.service
systemctl enable nginx

# 取消开机启动
systemctl disable nginx.service
systemctl disable nginx

nginx安装-wip
http://example.com/2022/09/15/nginx安装-wip/
作者
TK
发布于
2022年9月15日
许可协议