Nginx 其他技巧
前置文章:Nginx 入门配置
GZIP 压缩
1 | gzip on; |
访问控制
从上到下的顺序,匹配到了便跳出:deny 拒绝,allow 允许
1 | location / { |
请求限制
对于大流量恶意的访问,会造成带宽的浪费,给服务器增加压力;可以通过 nginx 对于同一 IP 的连接数以及并发数进行限制;合理的控制还可以用来防止 DDos 和 CC 攻击
关于请求限制主要使用 nginx 默认集成的 2 个模块:
- limit_conn_module:连接频率限制模块
- limit_req_module:请求频率限制模块
limit_zone 只能定义在 http 作用域,limit_conn 可以定义在 http、server、location 作用域;涉及到的配置主要是:
- limit_req_zone:限制请求数
- limit_conn_zone:限制并发连接数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#通过 limit_req_zone 限制请求数
http{
limit_conn_zone $binary_remote_addr zone=limit:10m; //设置共享内存空间大
server{
location /{
limit_conn addr 5; # 同一用户地址同一时间只允许有5个连接。
}
}
}
#通过 limit_conn_zone 限制并发连接数
http{
limit_req_zone $binary_remote_addr zone=creq:10 mrate=10r/s;
server{
location /{
limit_req zone=creq burst=5;
}
}
}上传大文件
nginx
1
2
3
4
5
6
7
8http {
proxy_connect_timeout 300s;
proxy_send_timeout 300s;
proxy_read_timeout 300s;
client_max_body_size 100m;
client_header_timeout 300s;
client_body_timeout 300s;
}1
2
3
4
5
6location ~ \.php(.*)$ {
fastcgi_read_timeout 300s;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}php
1
2
3
4
5post_max_size = 100M
upload_max_filesize = 100M
memory_limit = 1024M
max_execution_time = 300
max_input_time = 300PHP 线程监控
查看 php-fpm 启动时间(可以得出执行了多长时间)nginx
在相应的虚拟主机配置中添加以下内容1
2
3
4
5location ~ ^/status$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
}php-fpm
1
pm.status_path = /status
配置多个域名
1
2
3
4server {
listen 80;
server_name yuwei.cc www.yuwei.cc;
}配置多个站点
借用 Nginx 虚拟主机配置来实现,Nginx 有三种类型的虚拟主机: - 基于 IP:需要服务器上有多个地址,每个站点对应不同的地址
- 基于端口:每个站点对应不同的端口,访问的时候需要后接
:port
,可以修改 listen 忽略 - 基于域名:server_name 填写不同的域名即可
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24server {
listen 80;
server_name w.yuwei.cc;
location / {
root html;
index index.html;
}
}
server {
listen 80;
server_name ww.yuwei.cc;
location / {
root html;
index index.html;
}
}
server {
listen 80;
server_name www.yuwei.cc;
location / {
root html;
index index.html;
}
}开启列目录
Nginx 作为文件下载服务器存在时,需要开启 nginx 列目录1
2
3
4
5
6
7
8
9
10
11
12server {
#指定格式文件直接下载而不是打开
if ($request_filename ~* ^.*?\.(txt|pdf|jpg|png)$) {
add_header Content-Disposition 'attachment';
}
location /ftp {
alias /ftp;
autoindex on;
autoindex_exact_size off;#默认 on,显示文件 byte 值,off 显示为 K、M、GB
autoindex_localtime on;#默认 off,文件时间为 GMT,on 显示文件服务器时间
}
}禁止 IP 访问
禁止 IP 或未配置的域名访问,可以利用上边所说的default
规则,将它们都转到 404 页面去
1 | server { |
上述方法较粗暴,可以将它们直接301重定向到指定的域名,相当于给站点增加了外链1
2
3server {
rewrite ^/(.*)$ https://yuwei.cc/$1 permanent;
}
配置默认站点
如果 Nginx 服务器上创建了多个虚拟主机,默认会从上到下查找,匹配不到虚拟主机则会返回第一个虚拟主机;如果想指定一个默认站点时,可以将这个站点的虚拟主机放在配置文件中第一个虚拟主机的位置,或者在这个站点的虚拟主机上配置 listen port default
1 | server { |
404 跳转到首页
网站出现 404页面不是特别友好,我们可以通过上边的配置在出现404之后给自动跳转到首页去1
2
3
4
5
6
7
8server {
location / {
error_page 404 = @wei;
}
location @wei {
rewrite .* / permanent;
}
}
返回验证文件
微信支付等服务需要放一个 txt 文件到服务器上进行验证,可以直接修改配置而无需把文件传到服务器指定位置1
2
3
4location = /XGFyic8tZB.txt {
default_type text/plain;
return 200 'd6399a83657vb225615c3x`1c10684e6ac';
}
图片防盗链
1 | server { |
适配 PC 或移动设备
1 | server { |
单页面项目 history 路由配置
1 | server { |
禁止指定 user_agent
1 | # http_user_agent 为浏览器标识 |