Nginx安装与配置

本文遵循BY-SA版权协议,转载请附上原文出处链接。


本文作者: 黑伴白

本文链接: http://heibanbai.com.cn/posts/194ab05f/

Nginx 是一个高性能的 Web 和反向代理服务器, 它具有有很多非常优越的特性:

作为 Web 服务器:相比 Apache,Nginx 使用更少的资源,支持更多的并发连接,体现更高的效率,这点使 Nginx 尤其受到虚拟主机提供商的欢迎。能够支持高达 50,000 个并发连接数的响应,感谢 Nginx 为我们选择了 epoll and kqueue 作为开发模型.

作为负载均衡服务器:Nginx 既可以在内部直接支持 Rails 和 PHP,也可以支持作为 HTTP代理服务器 对外进行服务。Nginx 用 C 编写, 不论是系统资源开销还是 CPU 使用效率都比 Perlbal 要好的多。

作为邮件代理服务器: Nginx 同时也是一个非常优秀的邮件代理服务器(最早开发这个产品的目的之一也是作为邮件代理服务器),Last.fm 描述了成功并且美妙的使用经验。

本文的操作除非特殊说明,都是用的非root用户进行的操作

因为一般情况下,管理员是不给提供root用户进行操作的,贴合实际,能用非root就不用root

环境依赖

下面的环境依赖需要视系统情况而定,没有的环境安装下就行。

gcc环境

一般情况下gcc环境是都有的,如若没有,需要使用root用户进行安装:

1
# yum install gcc-c++

PCRE

PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库。

注:

  • pcre-devel是使用pcre开发的一个二次开发库。nginx也需要此库
  • 源码下载地址:PCRE - Browse Files at SourceForge.net
  • 可以不安装,只解压源码即可,后续nginx编译时指定pcre源码目录,避免了使用root用户操作
1
2
3
4
5
6
7
8
9
# 命令安装
yum install -y pcre pcre-devel

# 解压
tar -zxvf pcre-8.34.tar.gz
# cd pcre-8.34
# ./configure
# make
# make install

Zlib

Zlib是一个用于数据压缩的开源库。

注:

可以不安装,只解压源码即可,后续nginx编译时指定zlib源码目录,避免了使用root用户操作

1
2
3
4
5
tar -zxvf zlib-1.2.8.tar.gz
# cd zlib-1.2.8
# ./configure
# make
# make install

安装Nginx

Nginx 一般有两个版本,分别是稳定版和开发版,可以根据需要选择其中之一。

下载地址:nginx: download

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
cd /home/nginx

tar -zxvf nginx-1.21.1.tar.gz

cd nginx-1.21.1

# 编译 选项可以根据实际需要进行删减
./configure \
--prefix=/home/nginx \
--with-pcre=/home/nginx/pcre-8.34 \
--with-zlib=/home/nginx/zlib-1.2.8 \
--with-openssl=/home/nginx/openssl-1.0.2t \
--user=nginx \
--group=nginx \
--with-file-aio \
--with-http_v2_module \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_sub_module \
--with-http_gzip_static_module \
--with-http_stub_status_module

# 安装
make && make install

编译选项说明

仅列出了部分,其他可根据需要网上查询

选项 说明
–prefix=path Nginx安装的根路径,所有其它路径都要依赖该选项
–with-pcre=path pcre库的源码位置
–with-zlib=path zlib库的源码位置
–with-openssl=path openssl库的源码位置
–user= worker进程运行的用户
–group= worker进程运行的组
–with-file-aio 为freeBSD4.3+和linux2.6.22+系统启用异步io
–with-http_v2_module 支持http2协议
–with-http_ssl_module 如果需要对流量加密.可使用此选项,在urls中开始部分将会是https(需要openssl库)
–with-http_realip_module 允许ngx_http_realip_module模块(mod_rpaf)
此模块支持显示真实来源IP地址,主要用于NGINX做前端负载均衡服务器使用
如果你的nginx在七层负载均衡器或者其它设备之后,它们将Http头中的客户端ip地址传递,这时需要启用此模块,在多个客户处于一个ip地址的情况下使用
–with-http_sub_module 允许ngx_http_sub_module模块,这个模块可以能够在nginx的应答中搜索并替换文本
-with-http_gzip_static_module 允许ngx_http_gzip_static_module模块(mod_dflate),这个模块在一个预压缩文件传送到开启Gzip压缩的客户端之前检查是否已经存在以“.gz”结尾的压缩文件,这样可以防止文件被重复压缩
–with-http_stub_status_module 这个模块可以取得一些nginx的运行状态,如果是工业状况,可以直接取消,输出的状态信息科使用RRDtool或类似的工具绘制成图

启动服务

1
2
3
4
5
cd ~

cd nginx

./sbin/nginx

注:

以非root权限启动时,会出现 nginx: [emerg] bind() to 0.0.0.0:80 failed (13: Permission denied) 错误。

Linux只有root用户可以使用1024以下的端口

将nginx.conf文件中的80端口改为1024以上

登录web出现以下页面即为安装成功:

image-20220928144130352

服务停止

1
2
3
./nginx -s stop
# 或者
./nginx -s quit

其他命令

查看帮助提示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[nginx@node1 ~]$ nginx -h
nginx version: nginx/1.21.1
Usage: nginx [-?hvVtTq] [-s signal] [-p prefix]
[-e filename] [-c filename] [-g directives]

Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-T : test configuration, dump it and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /home/nginx/)
-e filename : set error log file (default: logs/error.log)
-c filename : set configuration file (default: conf/nginx.conf)
-g directives : set global directives out of configuration file

命令示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
nginx -s reopen #重启Nginx

nginx -s reload #重新加载Nginx配置文件,然后以优雅的方式重启Nginx

nginx -s stop #强制停止Nginx服务

nginx -s quit #优雅地停止Nginx服务(即处理完所有请求后再停止服务)

nginx -t #检测配置文件是否有语法错误,然后退出

nginx -v #显示版本信息并退出

nginx -V #显示版本和配置选项信息,然后退出

nginx -t #检测配置文件是否有语法错误,然后退出

nginx -T #检测配置文件是否有语法错误,转储并退出

nginx -q #在检测配置文件期间屏蔽非错误信息

nginx -c filename #设置配置文件(默认是:/etc/nginx/nginx.conf)

Nginx配置

image-20220929152041200

初始配置

删除了部分注释信息:

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
[nginx@node1 conf]$ cat nginx.conf

#user nobody;
worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/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 logs/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

#gzip on;

server {
listen 80;
server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;

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

#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;
}
}

}

MoiaControl V6 Web配置示例

MoiaControl V6是北京先进数通信息技术股份公司自主研发的一款企业级调度产品,在业界具有良好的口碑和市场,MoiaControl定位于企业统一调度管理平台。致力于为企业的批处理作业制定统一的开发规范、运维方法,对各系统的批量作业进行统一管理、调度和监控。在多个行业积累了大量用户,拥有众多的成功案例。在产品的容错、高可用、异常处理机制等方面积累了丰富的经验。

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
[nginx@node1 conf]$ cat nginx.conf
# 注意要指定用户,否则可能导致权限问题错误
user nginx;
worker_processes 1;

# 打开error_log
error_log logs/error.log;
error_log logs/error.log notice;
error_log logs/error.log info;
# 记录pid打开
pid logs/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 logs/access.log main;

sendfile on;
#tcp_nopush on;

# 链接超时时间,自动断开
keepalive_timeout 65;

gzip on;

# 多moia server时配置
upstream minio {
server 199.188.166.111:2005 weight=1;
server 199.188.166.112:2005 weight=2;
}
# 可以单独将server块的信息创建一个文件,通过include引入加载
# include /home/nginx/moia.conf/*.conf
server {
# web服务监听端口
listen 8888;
# nginx服务器ip或域名
server_name 199.188.166.113;
# 配置通过web上传时文件的一些限制
client_header_buffer_size 200m;
large_client_header_buffers 4 200m;
client_max_body_size 200m;

#charset koi8-r;

#access_log logs/host.access.log main;
# 添加如下属性 解压相关
gzip on;
gzip_min_length 1k;
gzip_comp_level 9;
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
gzip_vary on;
gzip_disable "MSIE [1-6]\.";

# 前端主应用路径,必须与前端部署项目路径一致
location / {
# web端部署目录
root /home/nginx/moiaweb;
index index.html index.htm;
try_files $uri /index.html;
}
# 微应用转发地址
location /moia {
alias /home/nginx/moiaweb/moia;
try_files $uri $uri/ /index.html;
}
# 后台网关地址
location /api {
# moia server ip 多server时配置为http://minio/
# proxy_pass http://199.188.166.111:2005/;
proxy_pass http://minio/;
proxy_set_header x-real-ip $remote_addr;
proxy_connect_timeout 600000;
proxy_read_timeout 600000;
proxy_send_timeout 600000;
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}

}

SharkData Web配置示例

企业智能数据研发管理平台(SharkData)是一款用于支持企业通过构建统一的元数据和数据资产管理,并在统一的元数据和数据资产管理的基础上支持企业全程面向数据的进行内外数据统一采集,数据集成开发,统一数据服务管理,数据管控工作,同时提供统一的调度运行服务及整体的运行管理功能。SharkData提供企业进行数据采集、数据研发、数据服务、数据管控、数据运维能力的建设,支撑企业快速开展如数据交换、数据仓库和应用集市等信息应用的建设。

如有需要请留言~~

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
[nginx@node1 conf]$ cat nginx.conf
# 注意要指定用户,否则可能导致权限问题错误
user nginx;
worker_processes 1;

# 打开error_log
error_log logs/error.log;
error_log logs/error.log notice;
error_log logs/error.log info;
# 记录pid打开
pid logs/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 logs/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

#gzip on;

# 可以单独将server块的信息创建一个文件,通过include加载
# include /home/nginx/moia.conf/*.conf
server {
listen 8888;
listen [::]8888;
# nginx服务器的ip地址
server_name 199.188.166.111;
# 配置通过web上传时文件的一些限制
client_header_buffer_size 2m;
large_client_header_buffers 4 200m;
client_max_body_size 20m;

#charset koi8-r;

#access_log logs/host.access.log main;
# 添加如下属性
gzip on;
gzip_min_length 1k;
gzip_comp_level 9;
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
gzip_vary on;
gzip_disable "MSIE [1-6]\.";

location / {
root /home/nginx/sharkdata/web;
index index.html index.htm;
try_files $uri /index.html;
}
location /base {
alias /home/nginx/sharkdata/web/base;
try_files $uri $uri/ /index.html;
}
location /dgmp {
alias /home/nginx/sharkdata/web/dgmp;
try_files $uri $uri/ /index.html;
}
location /didp {
alias /home/nginx/sharkdata/web/didp;
try_files $uri $uri/ /index.html;
}
location /sharkata {
alias /home/nginx/sharkdata/web/sharkata;
try_files $uri $uri/ /index.html;
}
location /moia {
alias /home/nginx/sharkdata/web/moia;
try_files $uri $uri/ /index.html;
}
location /insight {
alias /home/nginx/sharkdata/web/moia;
try_files $uri $uri/ /index.html;
}
location /api/ {
proxy_pass http://127.0.0.1:2005/;
}

#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;
}
}

}

蚂蚁再小也是肉🥩!


Nginx安装与配置
http://heibanbai.com.cn/posts/194ab05f/
作者
黑伴白
发布于
2022年9月28日
许可协议

“您的支持,我的动力!觉得不错的话,给点打赏吧 ୧(๑•̀⌄•́๑)૭”

微信二维码

微信支付

支付宝二维码

支付宝支付