nginx实践应用

启动两个tomcat(161,159)做测试

nginx.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
worker_processes  1;

events {
worker_connections 1024;
}

http {
# mime.types静态资源类型对应文件
include mime.types;
default_type application/octet-stream;

sendfile on;

keepalive_timeout 65;

#gzip on; 压缩

#引入外部化配置文件
include extra/*.conf
}

1 反向代理

extra/proxy_demo.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
server {
listen 80;
server_name localhost;
location / {
# 转发路径
proxy_pass http://192.168.11.161:8080;
# 将nginx的host信息传到后台
proxy_set_header Host $host;
# 将nginx的远程地址传到后台
proxy_set_header X-Real_IP $remote_addr;
# 将nginx的所有的代理服务器地址传到后台
proxy_set_header X-Forwarded_For $proxy_add_x_forwarded_for;

}
}

2 负载均衡

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
# 1. weight权重
upstream tomcat {
# 服务地址
# 159访问失败2次后60s内不再访问
server 192.168.11.159:8080 max_fails=2 fail_timeout=60s weight=1;
server 192.168.11.161:8080 weight=2;
}
# 2. ip_hash
#upstream tomcat {
# 服务地址
# ip_hash;
# server 192.168.11.159:8080;
# server 192.168.11.161:8080;
#}

server {
listen 80;
server_name localhost;
location / {
# 转发路径
proxy_pass http://tomcat; #upstream的名称
# 将nginx的host信息传到后台
proxy_set_header Host $host;
# 将nginx的远程地址传到后台
proxy_set_header X-Real_IP $remote_addr;
# 将nginx的所有的代理服务器地址传到后台
proxy_set_header X-Forwarded_For $proxy_add_x_forwarded_for;
# 访问服务器发生以下配置错误时,会自动转发到下一个
proxy_next_upstream error timeout http_500 http_503;
# nginx与服务器的连接超时时间
proxy_connect_timeout 60s;
proxy_send_timout 60s;
proxy_read_timout 60s;
}
}

3 动静分离

1
2
3
4
5
6
7
8
9
10
11
12
13
server {
listen 80;
server_name localhost;

location ~ .*\.(js|css|png|svg|ico|jpg)$ {
root static-resource;
# 静态资源缓存时间
expired 1d;
}
}

# 创建static-resource文件夹
# 将静态文件放入文件夹,即可访问

4 缓存

1
2
3
4
5
6
7
8
9
10
server {
listen 80;
server_name localhost;

location ~ .*\.(js|css|png|svg|ico|jpg)$ {
root static-resource;
# 静态资源缓存时间
expired 1d;
}
}

5 压缩

nginx.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;

# 开启压缩
gzip on;
# 超过限制后才压缩
gzip_min_length 5k;
# 压缩等级
gzip_comp_level 3;
# 对哪些资源进行压缩
gzip_types applcation/javascript image/jpeg;
# 设置缓冲区
gzip_buffers 4 32k;
# 是否传输gzip的压缩标志
gzip_vary on;

#引入外部化配置文件
include extra/*.conf
}

6 防盗链

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
server {
listen 80;
server_name localhost;

location ~ .*\.(js|css|png|svg|ico|jpg)$ {
# 限制访问静态资源,可以配置域名,正则表达式
# 允许160访问
vlid_referers none blocked 192.168.11.160;
if ($invalid_referer) {
# 重定向
return 404;
}
root static-resource;
expired 1d;
}
}

7 跨域

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://tomcat;
proxy_set_header Host $host;
proxy_set_header X-Real_IP $remote_addr;
proxy_set_header X-Forwarded_For $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout http_500 http_503;
proxy_connect_timeout 60s;
proxy_send_timout 60s;
proxy_read_timout 60s;

# 允许所有地址访问
add_header 'Access-Control-Allow-Origin' '*';
# 允许支持的方法访问
add_header 'Access-Control-Allow-Method' 'GET,POST,DELETE';
# 允许支持的媒体类型
add_header 'Access-Control-Allow-Header' 'Content-Type, *';
}
}

nginx实践应用
http://www.zivjie.cn/2023/05/21/中间件/nginx/nginx实践应用/
作者
Francis
发布于
2023年5月21日
许可协议