反向代理 - Nginx

虚拟主机

1
2
3
4
5
6
7
8
9
server {
listen 80;
server_name lugavin.io www.lugavin.io;

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

location

语法:

1
2
3
location [=|^~|~|~*] uri {

}

模式 描述
location = /uri = 表示精确匹配(只有完全匹配上才能生效)
location ^~ /uri 前缀匹配(在正则之前)
location ~ pattern 区分大小写的正则匹配
location ~* pattern 不区分大小写的正则匹配
location /uri 前缀匹配(在正则之后)
location / 通用匹配(相当于switch中的default)

说明:使用正则定义的location在配置文件中出现的顺序很重要,因为找到第一个匹配的正则后,查找就停止了。

rewrite

语法:

1
rewrite regex replacement [flag];

flag标志位:

  • last: 停止当前这个请求,并根据rewrite匹配的规则重新发起一个请求,新请求又从第一阶段开始执行(nginx运行分十一个执行阶段)
  • break: 相对last,break并不会重新发起一个请求,只是跳过当前的rewrite阶段,并执行本请求后续的执行阶段
  • redirect: 返回302临时重定向(地址栏会显示跳转后的地址)
  • permanent: 返回301永久重定向(地址栏会显示跳转后的地址)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
server {
listen 80;
server_name lugavin.io;

rewrite ^/(.*) http://www.lugavin.io/$1 last;
}

server {
listen 80;
server_name www.lugavin.io;

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

}

缓存

1
2
3
4
5
6
7
location ~ \.(ico|gif|jpg|jpeg|png)$ {
#生产环境启用缓存
expires 30d;
access_log off;
#开发环境禁用缓存
#add_header Cache-Control no-store;
}

压缩

1
2
3
4
5
6
7
8
http {
gzip on;
gzip_min_length 1k;
gzip_comp_level 2;
gzip_types text/plain text/css text/javascript application/javascript application/xml;
#根据客户端的HTTP头来判断是否需要压缩(有的浏览器不支持压缩)
gzip_vary on;
}

防盗链

1
2
3
4
5
6
7
8
9
10
11
12
location ~ \.(ico|gif|jpg|jpeg|png)$ {
#生产环境启用缓存
expires 30d;
access_log off;
#开发环境禁用缓存
#add_header Cache-Control no-store;
#防盗链
valid_referers none blocked lugavin.io *.lugavin.io;
if ($invalid_referer) {
return 403;
}
}

反向代理

1
2
3
4
5
6
7
location ^~ /api/ {
proxy_pass http://localhost:3000;
proxy_redirect off;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

负载均衡

负载均衡策略

在nginx中默认支持三种负载均衡策略:轮询(round-robin)、最少连接数(least-connected)、IP哈希(ip-hash)。

  • 轮询(默认):对应用服务器的请求以循环方式分发
  • 最少连接数:下一个请求被分配给具有最少活动连接数的服务器
  • IP哈希:用基于客户端IP地址的哈希函数确定应为下一个请求选择哪个服务器

负载均衡配置

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
http {

upstream express_cluster {
#weight: 设置权重(值越大被访问到的几率越大)
#max_fails: 与服务器通信失败后进行几次重试(默认为1)
#fail_timeout: 超过失败重试次数后在多长时间内不可用(默认为10秒)
server localhost:3001 weight=5 max_fails=1 fail_timeout=10;
server localhost:3002 weight=1 max_fails=1 fail_timeout=10;
#备份服务器(当所有主服务器全都不可用时才会起作用)
server localhost:3000 backup;
}

server {
listen 80;
server_name lugavin.io www.lugavin.io;

location ^~ /api/ {
proxy_pass http://express_cluster;
proxy_redirect off;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#proxy_connect_timeout: 与代理服务器建立连接的超时时间,注意,此超时通常不会超过75秒,默认为60秒
#proxy_send_timeout: 将请求传输到代理服务器的超时时间,是设置两个连续写操作之间的超时而不是整个请求的传输超时,默认为60秒
#proxy_read_timeout: 从代理服务器读取响应的超时时间,是设置两个连续读操作之间的超时而不是整个响应的传输超时,默认为60秒
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
}
}

}

HTTPS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
server {
listen 80;
server_name lugavin.io www.lugavin.io;
#配置http强制跳转到https
rewrite ^/(.*) https://$host/$1 last;
}

server {
listen 443;
server_name lugavin.io www.lugavin.io;

ssl on;
ssl_certificate ./conf/ca/cert.pem;
ssl_certificate_key ./conf/ca/key.pem;

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

解决Windows下443端口被其他进程占用导致Nginx无法启动的问题

1
2
3
4
REM 需要以管理员身份在命令窗口下执行以下dos命令
C:\> netstat -aon|findstr 443
TCP 0.0.0.0:443 0.0.0.0:0 LISTENING 3092
C:\> taskkill /F /pid 3092