抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

1.nginx 简介

nginx 是一个高性能的 HTTP 和反向代理服务器,也是一个 IMAP/POP3/SMTP 代理服务器。

2.nginx 的常用功能

  • 1.Http代理,反向代理:作为web服务器最常用的功能之一,尤其是反向代理。
  • 2.负载均衡:将服务器产生的流量分配到不同的服务器上,从而达到负载均衡。Nginx提供的负载均衡策略有2种:内置策略和扩展策略。内置策略为轮询,加权轮询,Ip hash;扩展策略 编写过滤函数,自定义实现功能。

3.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
...              #全局块

events { #events块
...
}

http #http块
{
... #http全局块
server #server块
{
... #server全局块
location [PATTERN] #location块
{
...
}
location [PATTERN]
{
...
}
}
server
{
...
}
... #http全局块
}

配置详细解读:

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
########### 每个指令必须有分号结束。#################
#user administrator administrators; #配置用户或者组,默认为nobody nobody。
#worker_processes 2; #允许生成的进程数,默认为1
#pid /nginx/pid/nginx.pid; #指定nginx进程运行文件存放地址
error_log log/error.log debug; #制定日志路径,级别。这个设置可以放入全局块,http块,server块,级别以此为:debug|info|notice|warn|error|crit|alert|emerg
events {
accept_mutex on; #设置网路连接序列化,防止惊群现象发生,默认为on
multi_accept on; #设置一个进程是否同时接受多个网络连接,默认为off
#use epoll; #事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
worker_connections 1024; #最大连接数,默认为512
}
http {
include mime.types; #文件扩展名与文件类型映射表
default_type application/octet-stream; #默认文件类型,默认为text/plain
#access_log off; #取消服务日志
log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #自定义格式
access_log log/access.log myFormat; #combined为日志格式的默认值
sendfile on; #允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。
sendfile_max_chunk 100k; #每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。
keepalive_timeout 65; #连接超时时间,默认为75s,可以在http,server,location块。

upstream mysvr {
server 127.0.0.1:7878;
server 192.168.10.121:3333 backup; #热备
}
error_page 404 https://www.baidu.com; #错误页
server {
keepalive_requests 120; #单连接请求上限次数。
listen 4545; #监听端口
server_name 127.0.0.1; #监听地址
location ~*^.+$ { #请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。
#root path; #根目录
#index vv.txt; #设置默认页
proxy_pass http://mysvr; #请求转向mysvr 定义的服务器列表
deny 127.0.0.1; #拒绝的ip
allow 172.18.5.54; #允许的ip
}
}
}

4.实际案例

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
worker_processes  1;

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;



include /software/nginx/conf/conf.d/*.conf;

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


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

conf.d/schedule.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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203

server {
listen 80;
server_name www.example.com;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
alias html/public-blog/;
index index.html index.htm;
}

location /cloud-console {
alias html/www/;
index index.html index.htm;
# 解决history路由,页面刷新404问题
try_files $uri $uri/ /cloud-console/index.html;
# autoindex on; # 如果需要列出目录内容,可以开启这个选项
}

location /blog {
alias html/public-blog/;
index index.html index.htm;
}

location /h5 {
# root html;
# 或者使用alias 区别是root会拼接请求路径,alias则直接替换,而且alias需要以/结尾
alias html/h5/;
index index.html index.htm;
}

location /static/ {
alias static/;
index index.html index.htm;
autoindex on; # 如果需要列出目录内容,可以开启这个选项
}

location /video-component/ {
alias html/video-component/;
index index.html index.htm;
}

# = 表示严格匹配; ~ 表示区分大小写; ~* 表示不区分大小写;^~ 表示以什么开头 (只要 uri中包含正则必须要有~ 或者 ~* 标识)
location ^~/ts/ {
rewrite ^/ts/(.*)$ /$1 break;
proxy_pass http://127.0.0.1:8080;
}

location ^~/cloud-server/ {
rewrite ^/cloud-server/(.*)$ /$1 break;
# client_max_body_size 默认大小是1M
client_max_body_size "1G";
# 响应body有的比较大需要设置下
proxy_buffer_size 10240k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffers 16 10240k; #总缓冲大小为16KB * 1024KB = 16384KB
proxy_busy_buffers_size 20480k; #高负荷下缓冲大小(proxy_buffers*2)
proxy_temp_file_write_size 20480k;#设定缓存文件夹大小,大于这个值,将从upstream服务器传
proxy_pass http://localhost:3000;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_next_upstream error timeout;
}

location ^~/discuss/ {
rewrite ^/discuss/(.*)$ /$1 break;
# client_max_body_size 默认大小是1M
client_max_body_size "1G";
# 响应body有的比较大需要设置下
proxy_buffer_size 10240k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffers 16 10240k; #总缓冲大小为16KB * 1024KB = 16384KB
proxy_busy_buffers_size 20480k; #高负荷下缓冲大小(proxy_buffers*2)
proxy_temp_file_write_size 20480k;#设定缓存文件夹大小,大于这个值,将从upstream服务器传
proxy_pass http://localhost:6870;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_next_upstream error timeout;
}

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

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

server {
listen 443 ssl;
server_name www.example.com;
# 证书的公私钥
ssl_certificate /software/nginx/conf/cert/www.example.com.pem;
ssl_certificate_key /software/nginx/conf/cert/www.example.com.key;

location / {
root html/public-blog/;
index index.html index.htm;
}

location /cloud-console { # 这样的话 https://www.example.com/cloud-console 直接就能访问!
alias html/www/;
index index.html index.htm;
# 解决history路由,页面刷新404问题
try_files $uri $uri/ /cloud-console/index.html;
# autoindex on; # 如果需要列出目录内容,可以开启这个选项
}

location /blog {
alias html/public-blog/;
index index.html index.htm;
}

location /h5 {
# root html;
# 或者使用alias
alias html/h5/;
index index.html index.htm;
}

# = 表示严格匹配; ~ 表示区分大小写; ~* 表示不区分大消息;^~ 表示以什么开头 (只要 uri中包含正则必须要有~ 或者 ~* 标识)
location ^~/ts/ {
rewrite ^/ts/(.*)$ /$1 break;
proxy_pass http://127.0.0.1:8080;
}

location ^~/cloud-server/ {
rewrite ^/cloud-server/(.*)$ /$1 break;
# client_max_body_size 默认大小是1M
client_max_body_size "1G";
# 响应body有的比较大需要设置下
proxy_buffer_size 10240k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffers 16 10240k; #总缓冲大小为16KB * 1024KB = 16384KB
proxy_busy_buffers_size 20480k; #高负荷下缓冲大小(proxy_buffers*2)
proxy_temp_file_write_size 20480k;#设定缓存文件夹大小,大于这个值,将从upstream服务器传
proxy_pass http://localhost:3000;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_next_upstream error timeout;
}

location ^~/discuss/ {
rewrite ^/discuss/(.*)$ /$1 break;
# client_max_body_size 默认大小是1M
client_max_body_size "1G";
# 响应body有的比较大需要设置下
proxy_buffer_size 10240k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffers 16 10240k; #总缓冲大小为16KB * 1024KB = 16384KB
proxy_busy_buffers_size 20480k; #高负荷下缓冲大小(proxy_buffers*2)
proxy_temp_file_write_size 20480k;#设定缓存文件夹大小,大于这个值,将从upstream服务器传
proxy_pass http://localhost:6870;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_next_upstream error timeout;
}


location /video-component/ {
alias html/video-component/;
index index.html index.htm;
}

location /static/ {
alias static/;
index index.html index.htm;
autoindex on; # 如果需要列出目录内容,可以开启这个选项
}
}

5.常用命令

1
2
重启(修改完配置后重启生效):
/software/nginx/sbin/nginx -s reload

6.参考

1
2
3
4
5
6
7
8
9
10
1、几个常见配置项:
•1.$remote_addr 与 $http_x_forwarded_for 用以记录客户端的ip地址;
•2.$remote_user :用来记录客户端用户名称;
•3.$time_local : 用来记录访问时间与时区;
•4.$request : 用来记录请求的url与http协议;
•5.$status : 用来记录请求状态;成功是200;
•6.$body_bytes_s ent :记录发送给客户端文件主体内容大小;
•7.$http_referer :用来记录从那个页面链接访问过来的;
•8.$http_user_agent :记录客户端浏览器的相关信息;
2、惊群现象:一个网路连接到来,多个睡眠的进程被同时叫醒,但只有一个进程能获得链接,这样会影响系统性能。

评论