nginx配置文件
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
autoindex on;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php$ {
如果要找具体的后端代码,在/code/去找
root /code;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
}
目录索引、下载服务
mkdir /download
yum install python3 python3-devel --downloadonly --downloaddir=/download
vim /etc/nginx/conf.d/autoindex.conf
server {
listen 11111;
server_name _;
location / {
autoindex on;
autoindex_localtime on;
autoindex_exact_size off;
root /download;
}
}
链接数监控
该功能需要用到http_stub_status_module模块,默认nginx不支持这个功能,可通过nginx -V去查看是否安装
创建nginx的虚拟主机文件,单独的去测试这个status状态功能即可
如果你的nginx默认不支持这个status功能,
重新编译nginx二进制命令,把这个模块添加进去
vim status.conf
server{
listen 9999;
server_name _;
stub_status on;
access_log off; # 因为它不是一个基于http请求响应的网站,仅仅是展示连接的信息,都不需要写location。
}
测试访问
[root@test /etc/nginx/conf.d]#curl 10.0.0.200:9999
Active connections: 1
server accepts handled requests
10134 10134 10388
Reading: 0 Writing: 1 Waiting: 0
Active connections 前活动客户端连接数,包括Waiting连接数。
accepts 接受的客户端连接总数。
handled 处理的连接总数。
accepts 通常,除非已达到某些资源限制(例如worker_connections限制),否则该参数值相同。
requests 客户端请求的总数。
Reading nginx正在读取请求标头的当前连接数。
Writing nginx将响应写回客户端的当前连接数。
Waiting 当前等待请求的空闲客户端连接数。
# 注意
一个tcp连接,可以发起多个http请求,可以通过修改保持连接参数修改
keepalive_timeout 0; 表示关闭长连接
利用ab命令测试
ab -c 100 -n 100000 http:
允许和限制访问
基于ip地址的访问限制
匹配规则是自上而下的匹配,匹配上一条规则后则不再继续匹配后续规则,allow和deny的顺序要先理清楚
- 限制只允许10.0.0.0~10.0.0.255范围的IP访问(禁止其他网段的访问)
server {
listen 22334;
server_name _;
location / {
allow 10.0.0.0/24;
deny all;
root /www/deny-allow;
index index.html;
}
}
- 限制拒绝10.0.0.0~10.0.0.255范围的IP访问(得允许其他网段访问)
server {
listen 22334;
server_name _;
location / {
deny 10.0.0.0/24;
root /www/deny-allow;
index index.html;
}
}
设置网页密码访问
1、先下载密码生成文件工具
yum install -y httpd-tools
2、生成密码
htpasswd -c ./passwd.db lnmp
htpasswd -b -c /etc/nginx/auth_passwd lnmp 123456
生成之后通过 cat ./passwd 查看用户名和密码是否生成成功
3、打开nginx配置文件
可以在server或location下配置
auth_basic "login";
auth_basic_user_file /usr/local/nginx/passwd.db;
限流配置
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
limit_req_zone $binary_remote_addr zone=two:10m rate=1r/s;
参数解释
limit_req_zone
binary_remote_addr
zone
rate
limit_req zone=two burst=5;
limit_req
burst=5
nodelay
针对客户端ip判断的,以及针对请求访问速率的限制模块
vim limit_req.conf
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
server {
listen 33555;
server_name _;
charset utf-8;
access_log /var/log/nginx/limit_req.log;
limit_req zone=one burst=3 nodelay;
location / {
root /www/limit-req;
index index.html index.htm;
}
}
location匹配规则
精确匹配
location = / {
#规则
}
~ 大小写敏感 区分大小写
location ~ /Example/ {
}
~* 大小写忽略
location ~* /Example/ {
}
^~ 只匹配以 uri 开头
location ^~ /img/ {
}
/ 其他匹配都不成功 就匹配此项
location / {
}
location匹配优先级
(location =) > (location ^~ 路径) > (location ~,~* 正则顺序) > (location 部分起始路径) > (/)
匹配符 |
匹配规则 |
优先级 |
= |
定义 URI 和位置的精确匹配。 |
1 |
^~ |
以某个字符串开头(不检查正则,区分大小写) |
2 |
~ |
区分大小写的正则匹配 (认识正则,区分url大小写) |
3 |
~* |
不区分大小写的正则匹配(认识正则,不区分url大小写) |
4 |
location中的root和alias实战
server {
listen 33555;
server_name _;
# 需要访问 10.0.0.8:33555/static/caixukun.jpg
# 你有什么写法,可以返回这个数据,给用户看到呢?
# 当前有一个代码目录,叫做 /huya/ 要求静态数据放在这个目录下
# 已知有一个静态图片,放在如下的目录中 /huya/static/caixukun.jpg
# 要求,要进行静态请求匹配,匹配/static/ url开头
# 等于匹配用户访问的url形式是 10.0.0.8:33555/static/caixukun.jpg
location ^~ /static/ {
# 两种写法第一个,写root,root特点是会将该url(/static)填充到网页根目录下,认为它也是一个目录
# root /huya/static/;
root /huya/;# 正确写法
# 第二种写法,alias别名用法
alias /huya/static/;
}
}
隐藏nginx版本号
在http中加入一行 server_tokens off;
防盗链
location ~* \.(jpg|png|jpeg|gif|bmp) {
valid_referers www.shop.com;
if ($invalid_referer) {
return 404;
}
配置访问PHP后端
location ~ \.php$ {
如果要找具体的后端代码,在/code/去找
root /code;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
php的代码是放在/code目录下,每次更新只需要将php写的代码解压放到/code目录下即可访问新的服务
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性