Nginx服务器基本配置及在模块化项目中的转发应用

目录

nginx概述

  • 来源
      Nginx (“engine x”) 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。Nginx是由Igor Sysoev为俄罗斯访问量第二的Rambler.ru站点开发的,第一个公开版本0.1.0发布于2004年10月4日。2011年6月1日,nginx 1.0.4发布。
  • 特点
      占有内存少,并发能力强。Nginx可以在大多数Unix like OS上编译运行,并有Windows移植版。windows下的nginx使用的是native Win32 API编写的,使用select处理连接,所以windows下的nginx性能不会太高且伸缩性(scalability,意思是通过数量上的扩展满足系统业务的增长)也差些。另外windows下的nginx缺少一些模块:XSLT filter, image filter, GeoIP module, 和 embedded Perl language。同时需要注意的是:
  1. windows下nginx配置文件中的目录请使用“/”,而不是“”做目录分隔
  2. windows下的nginx只有一个有效的工作进程
  3. windows vista以后系统的不支持nginx的cache模块和需要共享内存支持的模块
  4. widows下的nginx支持最大1024个并发连接

基本目录结构

在window系统中,nginx目录结构如下(本文以nginx-1.11.5为例):

其中:
conf
存放默认配置文件。

contrib
存放一些实用工具,如geo配置生成工具(geo2nginx.pl)。

docs
存放一些说明文档。

html
存放默认的网页文件。


logs
存放nginx运行的访问接入日志和错误日志。

temp
存放一些静态缓存数据,用户访问的时候,如果有就直接从缓存里取。

nginx.exe
nginx启动方式:双击nginx.exe或者在当前目录打开cmd窗口,使用命令nginx即可:

如果要关闭nginx,则需要再打开一个cmd窗口,使用命令nginx -s stop:


配置文件nginx.conf

基本介绍

Nginx配置文件主要分为4部分:main(全局设置)、server(主机设置)、upstream(负载均衡服务器设置)和location(URL匹配特定位置的设置)。
nginx.conf中的配置信息,根据其逻辑上的意义,对它们进行了分类,也就是分成了多个作用域,或者称之为配置指令上下文。不同的作用域含有一个或者多个配置项。

当前nginx支持的几个指令上下文:
main
nginx在运行时与具体业务功能(比如http服务或者email服务代理)无关的一些参数,比如工作进程数,运行的身份等。
http
与提供http服务相关的一些配置参数。例如:是否使用keepalive,是否使用gzip进行压缩等。
server
http服务上支持若干虚拟主机。每个虚拟主机一个对应的server配置项,配置项里面包含该虚拟主机相关的配置。在提供mail服务的代理时,也可以建立若干server.每个server通过监听的地址来区分。
location
http服务中,某些特定的URL对应的一系列配置项。
mail
实现email相关的SMTP/IMAP/POP3代理时,共享的一些配置项(因为可能实现多个代理,工作在多个监听地址上)。

一个实际使用的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
user nobody;
worker_processes 4;

events {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;

sendfile on;

keepalive_timeout 65;
#gzip on;

server {
listen 80;
server_name localhost;

#charset utf-8;

#access_log logs/host.access.log main;

location / {
proxy_pass http://127.0.0.1:8089/;
}

location /rest/monitor/ {
proxy_pass http://127.0.0.1:8080/dw-monitorTask/rest/monitor/;
#反向代理配置
}

#error_page 404 /404.html;

error_page 500 502 503 504 /50x.html;
#定义错误页面
location = /50x.html {
root html;
}
}

# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {

#}

# HTTPS server
server {

}
}

相应含义见每行注释。

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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
####################################################################################################
user nobody;
#指定nginx worker进程运行用户以及用户组,默认nobody。

worker_processes 4;
#指定nginx要开启的进程数,最好与CPU个数相同。

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#用来定义全局错误日志文件。级别有:debug、info、notice、warn、error和crit。debug输出日志最为详细,crit输出日志最少。

#pid logs/nginx.pid;
#用来指定进程id的存储文件位置。

events {
worker_connections 1024;
}
#设定nginx的工作模式及连接上限。

####################################################################################################
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"';
#指定nginx日志的输出格式。main为此日志文件输出格式的名称,可以在下面access_log指令中引用。

#access_log logs/access.log main;
#指定nginx的access日志输出,使用上面声明的格式。

sendfile on;
#用于开启高效文件传输模式。

#tcp_nopush on;
#防止TCP阻塞。

keepalive_timeout 65;
#用于设置客户端连接保持活动的超时时间,超过这个时间,服务器会关闭该连接。

#gzip on;
#支持压缩传输,提高传输速度。

#server这部分指定了一个虚拟主机。
server {
listen 80;
#监听端口。

server_name localhost;
#主机名(域名)。

#charset utf-8;
#web服务器的语言编码。

#access_log logs/host.access.log main;
#虚拟主机独立的访问日志。

# ==================================================================================================================
# 匹配url地址中有"/",则执行花括号中的配置。具体在本项目中,匹配到"/",则将请求转发至http://127.0.0.1:8089/,也就是项目中管理员模块(基础模块)所运行在的端口
#(本地测试,管理员模块部署在本地tomcat中,且运行在8089端口,因此是http://127.0.0.1:8089/)
location / {
proxy_pass http://127.0.0.1:8089/;
#反向代理配置。特别注意:在nginx中配置proxy_pass时,当在后面的url加上了/(如上所写http://127.0.0.1:8089/),相当于是绝对根路径,则nginx不会把location中匹配的路径部分代理走;如果没有/,则会把匹配的路径部分也给代理走。再比如:
#1、location /test/ {
# proxy_pass http://t6:8300;
# }
#
#2、location /test/ {
# proxy_pass http://t6:8300/;
# }
#
#对于上面两种配置,区别只在于proxy_pass转发的路径后是否带 “/”
#
#针对情况1,由于proxy_pass配置最后没有加/,则会把匹配的路径部分也给代理走。即如果访问http://server/test/test.jsp,则被nginx代理后,请求路径会便问http://t6:8300/test/test.jsp,将test/ 作为根路径(是一个相对路径),请求test/路径下的资源。
#
#针对情况2,由于proxy_pass配置最后加了/,则不会把匹配的路径部分也给代理走。即如果访问http://server/test/test.jsp,则被nginx代理后,请求路径会变为 http://proxy_pass/test.jsp,直接访问server的根资源。
}

# ==================================================================================================================
# 匹配url地址中有"/rest/monitor/",则执行花括号中的配置。具体在本项目中,匹配到"/rest/monitor/",表明是具体业务子模块monitorTask的业务逻辑,则将请求转发至http://127.0.0.1:8080/dw-monitorTask/rest/monitor/
location /rest/monitor/ {
proxy_pass http://127.0.0.1:8080/dw-monitorTask/rest/monitor/;
#反向代理配置
}

#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指定文件路径有两种方式root和alias,这里是默认值:root html
}
#等号“=”表示完全一致
}

#server这部分指定了其它的虚拟主机。
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;

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

#server这部分指定了其它的虚拟主机。
# 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;
# }
#}
}

应用案例

  在模块化的web项目中,应用被合理地拆分成了多个模块。有一些通用的模块会被多个项目复用,比如上述配置文件中提到的管理员模块,也就是基础入口模块。
  具体访问一个web应用时,诸如登录鉴权,系统菜单路由这些功能统一由基础模块进行处理。具体地,这个独立的模块被部署在了本地tomcat中(8089端口),那么,项目启动后,初始访问路由”/“就会由此转发给http://127.0.0.1:8089/;
  而对于具体业务项目来说,业务模块其功能根据需求单独开发,开发完成后结合基础管理员模块,通过nginx进行反向代理来进行通信。具体地,本项目业务模块部署在了本地另一个tomcat中(端口8080)。在这里,凡是拦截到请求“/rest/monitor/”,说明是本项目具体业务逻辑的内容,则会被转发至http://127.0.0.1:8080/dw-monitorTask/rest/monitor/
  可以看出,通过上述这种方式,整个项目不但变得清晰,而且模块化特点很明显,易于灵活迁移。比如,公共模块统一部署在某个公网服务器上,那么后续开发的新业务系统就可以通过这种方式复用某些共有功能,避免了重复开发,提高了开发效率。