当前位置: 首页 > news >正文

nginx使用手册<一>

nginx初试

  1. 安装
  2. 配置系统管理
  3. 启动
  4. 配置配置文件
  5. 访问网站

错误

  1. 403 forbidden的原因
  • 权限问题,查看当前文件夹的用户。使用ll,使用id查看当前用户,另外nginx.conf中的user字段应该对应当前文件夹的用户,使用chown -R nginx:nginx /data/blgs来改变
  • SELinux的权限认证关闭,修改配置文件,然后rboot,别忘了设置电脑的有线开机自动连接
vi /etc/selinux/config
#SELINUX=enforcing
SELINUX=disabled   #注释之前,替换为这
  1. 访问不了
  • 防火墙关上systemctl stop firewalld
  • curl ip地址,查看是否能通,如果不行,就是nginx没有启动成功

nginx功能

  1. 路由(站点路由,负载均衡,流量管理)-- 主要功能
  2. 性能:高可用,高并发 – 核心功能
  3. 权限(认证、授权)、网关:一般Java使用spring gateway,C#使用自己的网关 – 扩展功能

网关功能:

  • 过滤请求
  • 跨域问题
  • 验证token信息
  • 接口服务的保护(熔断,限流,服务降级)

为什么网关可以做的事情,nginx都可以做,还需要网关呢?

  • 简化开发,快速上手。不同语言的有不同语言的网关,如果使用nginx需要学习lua脚本,但是如果使用网关,就可以直接使用当前语言进行开发,比如Java的网关spring gateway,是使用Java语言编写的,Java开发者可以快速上手进行网关的编写。而如果使用nginx,还需要学习lua脚本。

nginx配置文件

需要配置的基本元素

  • http:最外层阔号,基本不动
  • server:站点:每一个server的listenserver_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";

http://www.mrgr.cn/news/49637.html

相关文章:

  • Oracle漏洞修复 19.3 补丁包 升级为19.22
  • 噪声分布 双峰,模拟函数 或者模拟方法 python人工智能 深度神经网络
  • 大数据开发工程师的岗位技能要求及自我介绍参考(持续更新)
  • RTOS实时系统-信号量如何确保一次只有一个任务可以访问某个资源
  • 目标检测系统【环境详细配置过程】(CPU版本)
  • 架构师备考-背诵精华(架构开发方法)
  • 城域网——IP城域网、城域以太网、光城域网
  • 谈谈软件开发可行性研究的主要内容
  • Unity DOTS中的Archetype与Chunk
  • 【Redis】缓存预热、雪崩、击穿、穿透、过期删除策略、内存淘汰策略
  • 关于WPF(Windows Presentation Foundation)中Grid控件
  • [Linux网络编程]深入了解TCP通信API,看看底层做了啥(看了这篇文章,你也算是读了Linux源码的高手!)
  • 【开源物联网平台】Fastbee系统稳定性和压测报告
  • 在wsl2下将Ubuntu从一个盘移动到其他盘
  • 微服务之间是如何独立通讯的?
  • spring 注解
  • manjaro kde 24 应该如何设置才能上网(2024-10-13亲测)
  • Git上传命令汇总
  • 基于springboot Vue3的两种图形验证码工具——vue3-puzzle-vcode纯前端防人机图形滑动验证码和kaptcha图片文字验证码
  • 查找和最小的 K 对数字