• Nginx - 反向代理与负载均衡


    目录

    一、Nginx

    1.1、Nginx 下载

    1.2、nginx 基础配置的认识

    a)第一部分:全局块

    b)第二部分:events 块

    c)第三部分:http 块

    http 块中 内嵌的 server 块

    1.3、一些常用配置

    1.3.1、location 匹配级别

    a)location /

    b)location =

    c)location ^~ 

    1.3.2、实现反向代理

     1.3.3、nginx 配置负载均衡

    a)weight 权重

    b)ip_hash

    c)fair

    1.3.4、nginx 引入模块


    一、Nginx


    1.1、Nginx 下载

    a)打开官方网站连接:nginx news

    Ps:这里以 nginx-1.25.0 为例 

    b)解压缩后,进入以下文件路径.

    c)打开,输入 nginx.exe,回车即可,如下:

    d)打开网页,访问 localhost:80,即可看到 Nginx 页面(OpenResty 就是 Nginx)

    1.2、nginx 基础配置的认识

    在 nginx-1.24.0\conf\ 路径下有一个 nginx.conf 文件,打开如下:

    1. #user nobody;
    2. worker_processes 1;
    3. #error_log logs/error.log;
    4. #error_log logs/error.log notice;
    5. #error_log logs/error.log info;
    6. #pid logs/nginx.pid;
    7. events {
    8. worker_connections 1024;
    9. }
    10. http {
    11. include mime.types;
    12. default_type application/octet-stream;
    13. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
    14. # '$status $body_bytes_sent "$http_referer" '
    15. # '"$http_user_agent" "$http_x_forwarded_for"';
    16. #access_log logs/access.log main;
    17. sendfile on;
    18. #tcp_nopush on;
    19. #keepalive_timeout 0;
    20. keepalive_timeout 65;
    21. #gzip on;
    22. server {
    23. listen 80;
    24. server_name localhost;
    25. #charset koi8-r;
    26. #access_log logs/host.access.log main;
    27. location / {
    28. root html;
    29. index index.html index.htm;
    30. }
    31. #error_page 404 /404.html;
    32. # redirect server error pages to the static page /50x.html
    33. #
    34. error_page 500 502 503 504 /50x.html;
    35. location = /50x.html {
    36. root html;
    37. }
    38. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    39. #
    40. #location ~ \.php$ {
    41. # proxy_pass http://127.0.0.1;
    42. #}
    43. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    44. #
    45. #location ~ \.php$ {
    46. # root html;
    47. # fastcgi_pass 127.0.0.1:9000;
    48. # fastcgi_index index.php;
    49. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
    50. # include fastcgi_params;
    51. #}
    52. # deny access to .htaccess files, if Apache's document root
    53. # concurs with nginx's one
    54. #
    55. #location ~ /\.ht {
    56. # deny all;
    57. #}
    58. }
    59. # another virtual host using mix of IP-, name-, and port-based configuration
    60. #
    61. #server {
    62. # listen 8000;
    63. # listen somename:8080;
    64. # server_name somename alias another.alias;
    65. # location / {
    66. # root html;
    67. # index index.html index.htm;
    68. # }
    69. #}
    70. # HTTPS server
    71. #
    72. #server {
    73. # listen 443 ssl;
    74. # server_name localhost;
    75. # ssl_certificate cert.pem;
    76. # ssl_certificate_key cert.key;
    77. # ssl_session_cache shared:SSL:1m;
    78. # ssl_session_timeout 5m;
    79. # ssl_ciphers HIGH:!aNULL:!MD5;
    80. # ssl_prefer_server_ciphers on;
    81. # location / {
    82. # root html;
    83. # index index.html index.htm;
    84. # }
    85. #}
    86. }

    这里有很多注释,删除掉以后,如下:

    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. server {
    11. listen 80;
    12. server_name localhost;
    13. location / {
    14. root html;
    15. index index.html index.htm;
    16. }
    17. error_page 500 502 503 504 /50x.html;
    18. location = /50x.html {
    19. root html;
    20. }
    21. }
    22. }

    这里分开来看,如下:

    nginx 配置文件主要有三部分组成:

    a)第一部分:全局块

      全局块:nginx 服务器全局生效的配置命令

    worker_processes  1;   # 服务器并发处理能力,值越大并发能力越强(受自身配置限制)
    

    b)第二部分:events 块

    events:影响 nginx 和用户网络的连接.

    1. events {
    2. worker_connections 1024; #最大连接数1024个,需灵活配置
    3. }

    c)第三部分:http

    http块:包括文件引入、MIME-TYPE 定义,日志自定义、连接超时等.

    1. http {
    2. include mime.types; # 文件扩展名与文件类型映射表
    3. default_type application/octet-stream; # 访问到未定义的扩展名的时候,就默认为下载该文件
    4. sendfile on; # 日志自定义
    5. keepalive_timeout 65; # 超时时间

    http 块中 内嵌的 server 块

    和虚拟主机有关系,主要是未来节省硬件成本.

    一个 http 块可以包含多个 server 块,而一个 server 块就等于一个虚拟主机.

    server 块中又抱哈了 server 块 和 location 块

    全局 server 块:

    1. server {
    2. listen 80; # 目前监听的端口号
    3. server_name localhost; # 主机名称

    location 块:

    1. location / { #表示默认首页
    2. root html;
    3. index index.html index.htm;

    root  html 就是根路径,也就是通过 openresty-1.21.4.2-win64\html\ 路径下,去找页面(默认是 index.html 页面,也就是文章开头展示的页面).

    最后是对错误页面的描述

    1. error_page 500 502 503 504 /50x.html;
    2. location = /50x.html {
    3. root html;
    4. }

    如果请求出现了 500、502、503、504 错误,就会进入到 50x.html 页面中(一般不会用这些默认的错误页,因此可以将这个配置也删除掉)。

    1.3、一些常用配置

    1.3.1、location 匹配级别

    拿 echo 插件来举例:echo 就是会像网页上输出一些东西.

    这里需要先在 server 中配置 "default_type  text/html" ,否则会走默认的 http 块中的下载,如下:

    1. server {
    2. listen 80;
    3. server_name localhost;
    4. default_type text/html;
    5. location / {
    6. echo "hello nginx";
    7. }
    8. }

    在 openresty-1.21.4.2-win64\ 路径下重新打开一个 cmd 窗口,输入 nginx.exe -s reload 进行重启.

     打开浏览器输入 localhost:80 即可访问.

    a)location /

    / 就表示匹配以 "/" 开头的所有请求,例如 location/a、location/ajfdioabgua .......

    b)location =

    = 优先级最高,表示完全匹配,例如 location = /a 就表示只匹配路由 location/a,其他的都不可以.

    配置如下:

    1. server {
    2. listen 80;
    3. server_name localhost;
    4. default_type text/html;
    5. location / {
    6. echo "hello nginx";
    7. }
    8. location = /a {
    9. echo "=/a";
    10. }
    11. }

    cmd 窗口,输入 nginx.exe -s reload 进行重启.

    c)location ^~ 

    ^~ 优先级比 location / 高,但是匹配规则和 location / 类似.   例如 location ^~ /a 就表示匹配以 /a 开头的所有路由.

     例如配置如下:

    1. server {
    2. listen 80;
    3. server_name localhost;
    4. default_type text/html;
    5. location / {
    6. echo "hello nginx";
    7. }
    8. location = /a {
    9. echo "=/a";
    10. }
    11. location ^~ /a {
    12. echo "^~ /a";
    13. }
    14. }

    重启 nginx 服务.

    1.3.2、实现反向代理

    修改 nginx.conf 如下:

    1. server {
    2. listen 80;
    3. server_name localhost;
    4. default_type text/html;
    5. location / {
    6. proxy_pass "https://www.baidu.com";
    7. }
    8. }

    重启 nginx

    访问 localhost 即可转到 "https://www.baidu.com".

    有一些额外需要注意的如下:

    1. location /a {
    2. proxy_pass http://ip;
    3. }
    4. 上述配置会导致你在浏览器中输入以下网址:
    5. localhost/a/xxx => http://ip/a/xxx
    1. location /a/ {
    2. proxy_pass http://ip/;
    3. }
    4. 上述配置会导致你在浏览器中输入以下网址:
    5. localhost/a/xxx => http://ip/xxx

     

     1.3.3、nginx 配置负载均衡

    通过 upstream 来创建一组需要负载均衡服务(默认是轮询策略,如果某个服务器挂了,自动剔除).

    1. upstream group1 {
    2. server 192.168.0.12:80;
    3. server 192.168.0.12:81;
    4. }
    5. server {
    6. listen 80;
    7. server_name localhost;
    8. default_type text/html;
    9. location /a {
    10. proxy_pass "https://group1";
    11. }
    12. }
    a)weight 权重

    另外可以通过 weight 来控制需要负载均衡的权重.  权重越大,访问到的概率越大.

    比如将权重都配置为 1,表示两者访问到的概率相同.

    1. upstream group1 {
    2. server 192.168.0.12:80 weight=1;
    3. server 192.168.0.12:81 weight=1;
    4. }
    5. server {
    6. listen 80;
    7. server_name localhost;
    8. default_type text/html;
    9. location /a {
    10. proxy_pass "https://group1";
    11. }
    12. }

     或者将 80 端口的权重改为 10,让其访问到的概率大一些.

    1. upstream group1 {
    2. server 192.168.0.12:80 weight=10;
    3. server 192.168.0.12:81 weight=1;
    4. }
    5. server {
    6. listen 80;
    7. server_name localhost;
    8. default_type text/html;
    9. location /a {
    10. proxy_pass "https://group1";
    11. }
    12. }
    b)ip_hash

    每个请求按访问 ip 的hash 结果分配,这样子访客固定访问一个后端服务器,可以解决session问题

    举个例子:

    A用户固定ip,第一次访问到8080 tomcat,那么后面就都是访问到这台机器.

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

    c)fair

    根据后端响应时间来分配请求,处理时间短的优先分配.

    1. upstream myserver {
    2. server 127.0.0.1:8080;
    3. server 127.0.0.1:8081;
    4. fair;
    5. }

    1.3.4、nginx 引入模块

    在 http 块中通过 include 可以引入其他指定的 conf 配置.

    也就意味着,你可以创建一个文件夹,在这个文件夹下创建很多配置文件,一并引入. 例如,引入 web 目录下的所有 conf 文件.

    1. http {
    2. include mime.types;
    3. default_type text/html;
    4. sendfile on;
    5. keepalive_timeout 65;
    6. # 引入自定义配置文件
    7. include web/*.conf;
    8. }

    web 目录下的 conf 就可以配置反向代理、负载均衡...

    1. upstream demo1 {
    2. server localhost:10010;
    3. }
    4. server {
    5. listen 8079;
    6. location / {
    7. root D:/webapps/;
    8. index login.html;
    9. }
    10. location /api {
    11. proxy_pass http://demo1;
    12. # proxy_set_header HOST $host; # 不改变源请求头的值
    13. # proxy_pass_request_body on; #开启获取请求体
    14. # proxy_pass_request_headers on; #开启获取请求头
    15. # proxy_set_header X-Real-IP $remote_addr; # 记录真实发出请求的客户端IP
    16. # proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #记录代理信息
    17. }
    18. }

  • 相关阅读:
    工厂方法在Spring源码中的运用
    UE4 调用声音
    html 常见兼容性问题
    超详细全面 spring 复习总结笔记
    前端技能树,面试复习—— 风中劲草:知识要点精讲精炼手册(二)
    HTML 速查列表
    全球约有 150 亿台设备在运行 Java,收费后还能用吗?
    阿里怎么用DDD来拆分微服务?
    CSS笔记(黑马程序员pink老师前端)圆角边框
    HTML5期末考核大作业、HTML个人主页界面设计源码
  • 原文地址:https://blog.csdn.net/CYK_byte/article/details/133816042