Nginx 其他技巧
# GZIP 压缩
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_comp_level 5;
gzip_http_version 1.0;
gzip_proxied any;
gzip_vary off;
gzip_disable "MSIE [1-6]\.";
gzip_types text/css application/javascript application/xml text/plain application/x-font-ttf ;
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# 访问控制
从上到下的顺序,匹配到了便跳出:deny 拒绝,allow 允许
location / {
deny 192.168.1.1;
allow 127.0.0.0/24;
allow 192.168.0.0/16;
allow 10.10.0.0/16;
deny all;
}
1
2
3
4
5
6
7
2
3
4
5
6
7
# 请求限制
对于大流量恶意的访问,会造成带宽的浪费,给服务器增加压力;可以通过 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:限制并发连接数
#通过 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;
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 上传大文件
# nginx
http {
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
6
7
8
2
3
4
5
6
7
8
location ~ \.php(.*)$ {
fastcgi_read_timeout 300s;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
1
2
3
4
5
6
2
3
4
5
6
# php
post_max_size = 100M
upload_max_filesize = 100M
memory_limit = 1024M
max_execution_time = 300
max_input_time = 300
1
2
3
4
5
2
3
4
5
# PHP 线程监控
查看 php-fpm 启动时间(可以得出执行了多长时间)
# nginx
在相应的虚拟主机配置中添加以下内容
location ~ ^/status$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
}
1
2
3
4
5
2
3
4
5
# php-fpm
pm.status_path = /status
1
# 配置多个域名
server {
listen 80;
server_name yuwei.cc www.yuwei.cc;
}
1
2
3
4
2
3
4
# 配置多个站点
借用 Nginx 虚拟主机配置来实现,Nginx 有三种类型的虚拟主机:
- 基于 IP:需要服务器上有多个地址,每个站点对应不同的地址
- 基于端口:每个站点对应不同的端口,访问的时候需要后接
:port
,可以修改 listen 忽略 - 基于域名:server_name 填写不同的域名即可
server {
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;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 开启列目录
Nginx 作为文件下载服务器存在时,需要开启 nginx 列目录
server {
#指定格式文件直接下载而不是打开
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 显示文件服务器时间
}
}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
# 禁止 IP 访问
禁止 IP 或未配置的域名访问,可以利用上边所说的default
规则,将它们都转到 404 页面去
server {
listen 80 default;
server_name _;
return 404;
}
1
2
3
4
5
2
3
4
5
上述方法较粗暴,可以将它们直接301重定向到指定的域名,相当于给站点增加了外链
server {
rewrite ^/(.*)$ https://yuwei.cc/$1 permanent;
}
1
2
3
2
3
# 配置默认站点
如果 Nginx 服务器上创建了多个虚拟主机,默认会从上到下查找,匹配不到虚拟主机则会返回第一个虚拟主机;如果想指定一个默认站点时,可以将这个站点的虚拟主机放在配置文件中第一个虚拟主机的位置,或者在这个站点的虚拟主机上配置listen port default
server {
listen 80 default;
}
1
2
3
2
3
# 404 跳转到首页
网站出现 404页面不是特别友好,我们可以通过上边的配置在出现404之后给自动跳转到首页去
server {
location / {
error_page 404 = @wei;
}
location @wei {
rewrite .* / permanent;
}
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 返回验证文件
微信支付等服务需要放一个txt 文件到服务器上进行验证,可以直接修改配置而无需把文件传到服务器指定位置
location = /XGFyic8tZB.txt {
default_type text/plain;
return 200 'd6399a83657vb225615c3x`1c10684e6ac';
}
1
2
3
4
2
3
4
# 图片防盗链
server {
listen 80;
server_name *.test;
# 图片防盗链
location ~* \.(gif|jpg|jpeg|png|bmp|swf)$ {
valid_referers none blocked server_names ~\.google\. ~\.baidu\. *.qq.com; #只允许本机 IP 外链引用,将百度和谷歌也加入白名单有利于 SEO
if ($invalid_referer){
return 403;
}
}
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 适配 PC 或移动设备
server {
listen 80;
server_name yuwei.cc;
location / {
root /usr/local/app/pc; # pc 的 html 路径
if ($http_user_agent ~* '(Android | webOS | iPhone | iPod | BlackBerry)') {
root /usr/local/app/mobile; # mobile 的 html 路径
}
index index.html;
}
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 单页面项目 history 路由配置
server {
listen 80;
server_name singe.yuwei.cc;
location / {
root /usr/local/app/dist; # vue 打包后的文件夹
index index.html index.htm;
try_files $uri $uri/ /index.html @rewrites; # 默认目录下的 index.html,如果都不存在则重定向
expires -1; # 首页一般没有强制缓存
add_header Cache-Control no-cache;
}
location @rewrites { // 重定向设置
rewrite ^(.+)$ /index.html break;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
# 禁止指定 user_agent
# http_user_agent 为浏览器标识
# 禁止 user_agent 为baidu、360和sohu,~*表示不区分大小写匹配
if ($http_user_agent ~* 'baidu|360|sohu') {
return 404;
}
# 禁止 Scrapy 等工具的抓取
if ($http_user_agent ~* (Scrapy|Curl|HttpClient)) {
return 403;
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# 日志切割
#定义切割日志文件的方式
log_rotate daily;
#定义保存日志文件的天数
log_rotate_age 30;
1
2
3
4
2
3
4
网页编辑 (opens new window)
最近提交: 2023/03/22, 11:52:35