Nginx 配置 - 学习笔记

- 日理万妓 2023-06-19 09:39 175阅读 0赞

Nginx 配置 - 学习笔记

  • 目录结果
  • 虚拟主机配置
  • 反向代理 + 负载均衡
  • 负载均衡策略
    • 轮训(默认)
    • 权重
    • ip_hash
  • 网关
  • 参考资料

目录结果

  1. D:\nginx-1.16.1\html
  2. | 50x.html
  3. | index.html
  4. |
  5. +---a
  6. | index.html
  7. |
  8. +---b
  9. | index.html
  10. |
  11. +---c
  12. | index.html
  13. |
  14. \---d
  15. index.html

虚拟主机配置

nginx作为web服务器跑起来
只设置了server 块下的 listenserver_namelocation 其它都没动
server可以配多个。

  1. worker_processes 1; # 处理并发的数量,越大处理的并且量越大
  2. events {
  3. worker_connections 1024;# 支持最大连接数
  4. }
  5. http {
  6. include mime.types;
  7. default_type application/octet-stream;
  8. proxy_intercept_errors on; # 开启自定义错误页面
  9. sendfile on;
  10. keepalive_timeout 65;
  11. # 定义一个 web 服务
  12. server {
  13. # 监听端口
  14. listen 80;
  15. # 服务的域名或IP,多个用空格分隔
  16. server_name localhost 127.0.0.1;
  17. location / {
  18. # web服务根路径对应的目录
  19. root html;
  20. # 文件夹下默认页面,多个用空格分隔,优先级从左高右低
  21. index index.html index.htm;
  22. }
  23. # 自定义错误页面
  24. error_page 500 502 503 504 /50x.html;
  25. location = /50x.html {
  26. root html;
  27. }
  28. # 自定义错误页面
  29. error_page 404 https://blog.csdn.net/jx520;
  30. }
  31. }

因为上面的配置所以访问 localhost127.0.0.1 皆可。
在这里插入图片描述

反向代理 + 负载均衡

负载均衡使用默认的轮训策略
先定义3个web服务作集群。开放端口 8080 - 8082 ,最后定义反向代理服务开放80端口

  1. worker_processes 1; # 处理并发的数量,越大处理的并且量越大
  2. events {
  3. worker_connections 1024;# 支持最大连接数
  4. }
  5. http {
  6. include mime.types;
  7. default_type application/octet-stream;
  8. sendfile on;
  9. keepalive_timeout 65;
  10. # 服务器 1 端口 8080
  11. server {
  12. listen 8080;
  13. server_name localhost;
  14. location / {
  15. root html/a/;
  16. index index.html index.htm;
  17. }
  18. error_page 500 502 503 504 /50x.html;
  19. location = /50x.html {
  20. root html;
  21. }
  22. }
  23. # 服务器 2 端口 8081
  24. server {
  25. listen 8081;
  26. server_name localhost;
  27. location / {
  28. root html/b/;
  29. index index.html index.htm;
  30. }
  31. error_page 500 502 503 504 /50x.html;
  32. location = /50x.html {
  33. root html;
  34. }
  35. }
  36. # 服务器 3 端口 8082
  37. server {
  38. listen 8082;
  39. server_name localhost;
  40. location / {
  41. root html/c/;
  42. index index.html index.htm;
  43. }
  44. error_page 500 502 503 504 /50x.html;
  45. location = /50x.html {
  46. root html;
  47. }
  48. }
  49. # 用于负载均衡的集群(默认轮训,从上到下每台一次)。 集群名称:myserver
  50. upstream myserver {
  51. server 127.0.0.1:8080;
  52. server 127.0.0.1:8081;
  53. server 127.0.0.1:8082;
  54. }
  55. server {
  56. listen 80; # 监听端口
  57. server_name localhost; # 服务的域名或IP,多个用空格分隔
  58. location / {
  59. proxy_pass http://myserver; # 代理指定上面定义的集群 myserver
  60. }
  61. # 定义50x 错误页面
  62. error_page 500 502 503 504 /50x.html;
  63. location = /50x.html {
  64. root html;
  65. }
  66. # 定义40x 错误页面
  67. error_page 400 401 402 403 404 /40x.html;
  68. location = /40x.html {
  69. root html;
  70. }
  71. }
  72. }

负载均衡策略

  • max_fails
  • fail_timeout

轮训(默认)

按顺序来,每人一次。

权重

  1. upstream backend {
  2. server backend1.example.com weight=5;
  3. server 127.0.0.1:8080 max_fails=3 fail_timeout=30s;
  4. server unix:/tmp/backend3;
  5. server backup1.example.com backup;
  6. }
  • weight
    权重大的,能者多劳
    上面总共3个服务器在跑。(backup是后补,只有其它服务器都奔溃了,会才启用它)
    第一个权重5,另外两个没写则默认1 。权重总数是7,那就在7次访问中:
    backend1.example.com 被访问5
    127.0.0.1:8080 被访问1
    unix:/tmp/backend3 被访问1
  • max_fails = 3
  • fail_timeout = 30
    127.0.0.1:8080节点30秒内出现3次访问失败,判定节点不可用。此后10秒内请求不会向其转发到请求,10秒后重新检测其是否可用。

ip_hash

同一个ip始终被分配到同一台服器上

  1. upstream backend {
  2. ip_hash;
  3. server 127.0.0.1:8080;
  4. server 127.0.0.1:8081;
  5. server 127.0.0.1:8082;
  6. }

网关

定义用户服务8081

  1. server {
  2. listen 8081;
  3. server_name localhost;
  4. location / {
  5. root gateway/user;
  6. index index.html index.htm;
  7. }
  8. }

定义产品服务8082

  1. server {
  2. listen 8082;
  3. server_name localhost;
  4. location / {
  5. root gateway/product;
  6. index index.html index.htm;
  7. }
  8. }

定义网关80 通过路径 /api-user/api-product 访问

  1. server {
  2. listen 80;
  3. server_name localhost;
  4. # 路由到用户
  5. location /api-user {
  6. proxy_pass http://localhost:8081/;
  7. }
  8. # 路由到产品
  9. location /api-product {
  10. proxy_pass http://localhost:8082/;
  11. }
  12. }




















服务 不走网关直接访问 走网关访问
用户服务 http://localhost:8081/ http://localhost/api-user
产品服务 http://localhost:8082/ http://localhost/api-product

参考资料

1、proxy_intercept_errors 开启后nginx才会拦截错误,error_page才有反应
2、error_page 说明
3、location 说明
4、Nginx入门到精通

发表评论

表情:
评论列表 (有 0 条评论,175人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Nginx学习笔记

    下载nginx后文件放置 ![解压安装后生成这样一个文件,我将打包后的项目文件(生成的dist文件)放在了html文件夹中,nginx默认的是80端口,所以点击文件中的n