nginx使用手册<一>
nginx初试
- 安装
- 配置系统管理
- 启动
- 配置配置文件
- 访问网站
错误
- 403 forbidden的原因
- 权限问题,查看当前文件夹的用户。使用
ll
,使用id
查看当前用户,另外nginx.conf
中的user
字段应该对应当前文件夹的用户,使用chown -R nginx:nginx /data/blgs
来改变 - SELinux的权限认证关闭,修改配置文件,然后
rboot
,别忘了设置电脑的有线开机自动连接
vi /etc/selinux/config
#SELINUX=enforcing
SELINUX=disabled #注释之前,替换为这
- 访问不了
- 防火墙关上
systemctl stop firewalld
curl ip地址
,查看是否能通,如果不行,就是nginx没有启动成功
nginx功能
- 路由(站点路由,负载均衡,流量管理)-- 主要功能
- 性能:高可用,高并发 – 核心功能
- 权限(认证、授权)、网关:一般Java使用spring gateway,C#使用自己的网关 – 扩展功能
网关功能:
- 过滤请求
- 跨域问题
- 验证token信息
- 接口服务的保护(熔断,限流,服务降级)
为什么网关可以做的事情,nginx都可以做,还需要网关呢?
- 简化开发,快速上手。不同语言的有不同语言的网关,如果使用nginx需要学习lua脚本,但是如果使用网关,就可以直接使用当前语言进行开发,比如Java的网关spring gateway,是使用Java语言编写的,Java开发者可以快速上手进行网关的编写。而如果使用nginx,还需要学习lua脚本。
nginx配置文件
需要配置的基本元素
- http:最外层阔号,基本不动
- server:站点:每一个server的
listen
和server_name
是联合主键,不允许重复。对应着192.168.10.128:4000/home/
中的192.168.10.128:4000
- location:站点内的文件夹,比如
192.168.10.128:4000/home/
,这个/home/
就会被解析 - alias: 将上面的地址,映射到本机的文件夹上,比如值为:
/usr/share/datas/
,上面的/home/
就会访问,这个地址 - root:和alias不同是,这个是拼接,上面的是映射。比如
root /home/
,上面的uri就会访问/home/usr/share/datas
这个地址 - proxy_pass:和
alias
语义相同,只不过alias
访问的是本机地址,proxy_pass
代理别的服务器的地址 - upstream:负载均衡,放到http阔号中
站点设置
- 二级域名。比如blgs.fuzekun.top,vedio.fuzekun.top,music.fuzekun.top,games.fuzekun.top。
- nginx配置站点
- 虚拟主机配置
- 不同端口号映射
- 不同主机名称的映射
文件访问映射 - location的设置
-
loacation中proxy_pass,root,alias的规则
-
root是直接拼接上
-
alias是直接映射过去,相当于直接去掉
-
proxy_pass和alias是相同的,但是有个bug,如果访问根路径,会带上proxy_pass的路径,当时访问相对路径,就会去掉。
- 比如
location /remote/ {proxy_pass https://fuzekun.top/; } 访问路径为:ip/remote/; 会跳转到https://fuzekun/top/remote # 带上了remote 访问路径为: ip/remote/index.html; 会跳转到https://fuzekun.top/index.html; # 去掉了remote
-
网关功能
负载均衡、流量控制、缓存
http负载均衡配置 – location中配置proxy_pass
使用upstream进行负载均衡的设置
upstream backend {server 10.10.12.45:80 weight=1;server app.example.com:80 weight=2;server spare.example.com:80 backup;
}
server {location / {proxy_pass http://backend;}
}
tcp负载均衡 server中配置proxy_pass
stream {upstream mysql_read {server read1.example.com:3306 weight=5;# 服务器2server read2.example.com:3306; # 服务器1server 10.10.12.34:3306 backup; # 容灾服务器}server {# 和http没有什么不同,只不过http监听的端口是80,并且需要加上http://进行访问backendlisten 3306;proxy_pass mysql_read;}
}
udp负载均衡
stream {upstream ntp {server ntp1.example.com:123 weight=2;server ntp2.example.com:123;}server {# 就是在这里加上udp就实现了listen 123 udp;proxy_pass ntp;}
}
负载均衡的方式
最少连接、最短时间、通用哈希、随机算法或 IP 哈希
# hash
upstream backend {hash $remote_addr;server backend.example.com;server backend1.example.com;
}
# 最小连接时间
upstream backend {least_conn;server backend.example.com;server backend1.example.com;
}
其他额外的功能
- cookies绑定
- 健康检查
- 不停机增加和删除服务器,水平扩容
流量控制
- hash算法的流量控制
split_clients "${remote_addr}AAA" $variant {20.0% "backendv2";*
- 限制连接数量
limit_conn_zone $binary_remote_addr zone=limitbyaddr:10m;limit_conn_status 429;# ...server {# ...limit_conn limitbyaddr 40;# ...}
- 限制国家和地区
load_module modules/ngx_http_geoip2_module.so;
http {geoip2 /etc/maxmind-country.mmdb {auto_reload 5m;$geoip2_metadata_country_build metadata build_epoch;$geoip2_data_country_code default=Chinasource=$variable_with_ip country iso_code;$geoip2_data_country_name country names en;}map $geoip2_data_country_code $country_access {"China" 0;default 1;}# ...
}# 限制除了美国的所有国家不允许访问
server {if ($country_access = '1') {return 403;}# ...
- 限制速率
# 使用limit_req_zone进行速率限制
http {limit_req_zone $binary_remote_addrzone=limitbyaddr:10m rate=3r/s;limit_req_status 429;# ...server {# ...limit_req zone=limitbyaddr;# ...}
}# 使用limit_req进行两级速率限制
server {location / {limit_req zone=limitbyaddr burst=12 delay=9;}
- 限制带宽
location /download/ {limit_rate_after 10m;limit_rate 1m;
}
缓存
- 使用缓存
proxy_cache_path /var/nginx/cachekeys_zone=main_content:60mlevels=1:2inactive=3hmax_size=20gmin_free=500m;
proxy_cache CACHE;
- 绕过缓存
proxy_cache_bypass $http_cache_bypass;
- 使用陈旧缓存
proxy_cache_use_stale error timeout invalid_header updatinghttp_500 http_502 http_503 http_504http_403 http_404 http_429;
- 锁定缓存
proxy_cache_lock on;
proxy_cache_lock_age 10s;
proxy_cache_lock_timeout 3s;
- 缓存键值对,缓存的内容和检索方式
proxy_cache_key "$host$request_uri $cookie_user";