Linux(CentOS8、Ubuntu作为服务器)对nginx的学习:反向代理,负载均衡,动静分离,高可用

一、 所有配置文件

1、nginx.conf

  1 #user  nobody;
  2 worker_processes  1;
  3 
  4 #error_log  logs/error.log;
  5 #error_log  logs/error.log  notice;
  6 #error_log  logs/error.log  info;
  7 
  8 #pid        logs/nginx.pid;
  9 
 10 
 11 events {
 12     worker_connections  1024;
 13 }   
 14 
 15 
 16 http {
 17     include       mime.types;
 18     default_type  application/octet-stream;
 19     
 20     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
 21     #                  '$status $body_bytes_sent "$http_referer" '
 22     #                  '"$http_user_agent" "$http_x_forwarded_for"';
 23 
 24     #access_log  logs/access.log  main;
 25 
 26     sendfile        on;
 27     #tcp_nopush     on;
 28 
 29     #keepalive_timeout  0;
 30     keepalive_timeout  65;
 31 
 32     #gzip  on;
 33     #负载均衡, weight: 权重,值越大,分配越多
 34 #    upstream myserver{
 35 #       server  192.168.88.128:8080 weight=5;
 36 #       server  192.168.88.128:8081 weight=10;
 37 #    }
 38     server {
 39         listen       80;
 40         server_name  192.168.88.128;
 41 
 42         #charset koi8-r;
 43 
 44         #access_log  logs/host.access.log  main;
 45         #动静分离
 46         location /www/ {
 47             root   /data/;
 48             #反向代理
 49             #proxy_pass http://127.0.0.1:8080;
 50             #负载均衡
 51 #           proxy_pass http://myserver;
 52             index  index.html index.htm;
 53         }
 54         location /image/ {
 55             root   /data/;
 56             autoindex   on;
 57         }
 58         #error_page  404              /404.html;
 59 
 60         # redirect server error pages to the static page /50x.html
 61         #
 62         error_page   500 502 503 504  /50x.html;
 63         location = /50x.html {
 64             root   html;
 65         }
 66 
 67         # proxy the PHP scripts to Apache listening on 127.0.0.1:80
 68         #
 69         #location ~ \.php$ {
 70         #    proxy_pass   http://127.0.0.1;
 71         #}
 72 
 73         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
 74         #
 75         #location ~ \.php$ {
 76         #    root           html;
 77         #    fastcgi_pass   127.0.0.1:9000;
 78         #    fastcgi_index  index.php;
 79         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
 80         #    include        fastcgi_params;
 81         #}
 82 
 83         # deny access to .htaccess files, if Apache's document root
 84         # concurs with nginx's one
 85         #
 86         #location ~ /\.ht {
 87         #    deny  all;
 88         #}
 89     }
 90 
 91 
 92     # another virtual host using mix of IP-, name-, and port-based configuration
 93     #反向代理
 94 #    server {
 95 #        listen       9001;
 96 #        server_name 192.168.88.128;
 97 #                      #
 98 #        location ~ /edu/ {   
 99 #            proxy_pass http://127.0.0.1:8080;
100 #        }             #
101 #        location ~ /vod/ {   
102 #            proxy_pass http://127.0.0.1:8081;
103 #        }             #
104 #    }                 #
105                       #
106 
107     # HTTPS server
108     #
109     #server {
110     #    listen       443 ssl;
111     #    server_name  localhost;
112 
113     #    ssl_certificate      cert.pem;
114     #    ssl_certificate_key  cert.key;
115 
116     #    ssl_session_cache    shared:SSL:1m;
117     #    ssl_session_timeout  5m;
118 
119     #    ssl_ciphers  HIGH:!aNULL:!MD5;
120     #    ssl_prefer_server_ciphers  on;
121 
122     #    location / {
123     #        root   html;
124     #        index  index.html index.htm;
125     #    }
126     #}
127 
128 }

2、keepalived.conf 主服务器的配置文件

1 ! Configuration File for keepalived
  2 
  3 global_defs {
  4    notification_email {
  5      acassen@firewall.loc
  6      failover@firewall.loc
  7      sysadmin@firewall.loc
  8    }
  9    notification_email_from Alexandre.Cassen@firewall.loc
 10    smtp_server 192.168.88.128
 11    smtp_connect_timeout 30
 12    router_id LVS_DEVEL
 13    vrrp_skip_check_adv_addr
 14    vrrp_strict
 15    vrrp_garp_interval 0
 16    vrrp_gna_interval 0
 17 }
 18 vrrp_script chk_http_port {
 19    script "/usr/local/src/nginx_check.sh"
 20    #检测脚本执行的间隔
 21    interval 2
 22 
 23    weight 4
 24 }
 25 vrrp_instance VI_1 {
 26     state MASTER
 27     interface ens160
 28     virtual_router_id 51
 29     priority 100
 30     advert_int 1
 31     authentication {
 32         auth_type PASS
 33         auth_pass 1111
 34     }
 35     virtual_ipaddress {
 36         192.168.200.16
 37     }
 38     track_script {
 39         chk_http_port
 40     }
 41 
 42 }
 43 
 44 virtual_server 192.168.200.100 443 {
 45     delay_loop 6
 46     lb_algo rr
 47     lb_kind NAT
 48     persistence_timeout 50
 49     protocol TCP
 50 
 51     real_server 192.168.201.100 443 {
 52         weight 1
 53         SSL_GET {
 54             url {
 55               path /
 56               digest ff20ad2481f97b1754ef3e12ecd3a9cc
 57             }
 58             url {
 59               path /mrtg/
 60               digest 9b3a0c85a887a256d6939da88aabd8cd
 61             }
 62             connect_timeout 3
 63             retry 3
 64             delay_before_retry 3
 65         }
 66     }
 67 }
 68 
 69 virtual_server 10.10.10.2 1358 {
 70     delay_loop 6
 71     lb_algo rr
 72     lb_kind NAT
 73     persistence_timeout 50
 74     protocol TCP
 75 
 76     sorry_server 192.168.200.200 1358
 77 
 78     real_server 192.168.200.2 1358 {
 79         weight 1
 80         HTTP_GET {
 81             url {
 82               path /testurl/test.jsp
 83               digest 640205b7b0fc66c1ea91c463fac6334d
 84             }
 85             url {
 86               path /testurl2/test.jsp
 87               digest 640205b7b0fc66c1ea91c463fac6334d
 88             }
 89             url {
 90               path /testurl3/test.jsp
 91               digest 640205b7b0fc66c1ea91c463fac6334d
 92             }
 93             connect_timeout 3
 94             retry 3
 95             delay_before_retry 3
 96         }
 97     }
 98 
 99     real_server 192.168.200.3 1358 {
100         weight 1
101         HTTP_GET {
102             url {
103               path /testurl/test.jsp
104               digest 640205b7b0fc66c1ea91c463fac6334c
105             }
106             url {
107               path /testurl2/test.jsp
108               digest 640205b7b0fc66c1ea91c463fac6334c
109             }
110             connect_timeout 3
111             retry 3
112             delay_before_retry 3
113         }
114     }
115 }
116 
117 virtual_server 10.10.10.3 1358 {
118     delay_loop 3
119     lb_algo rr
120     lb_kind NAT
121     persistence_timeout 50
122     protocol TCP
123 
124     real_server 192.168.200.4 1358 {
125         weight 1
126         HTTP_GET {
127             url {
128               path /testurl/test.jsp
129               digest 640205b7b0fc66c1ea91c463fac6334d
130             }
131             url {
132               path /testurl2/test.jsp
133               digest 640205b7b0fc66c1ea91c463fac6334d
134             }
135             url {
136               path /testurl3/test.jsp
137               digest 640205b7b0fc66c1ea91c463fac6334d
138             }
139             connect_timeout 3
140             retry 3
141             delay_before_retry 3
142         }
143     }
144 
145     real_server 192.168.200.5 1358 {
146         weight 1
147         HTTP_GET {
148             url {
149               path /testurl/test.jsp
150               digest 640205b7b0fc66c1ea91c463fac6334d
151             }
152             url {
153               path /testurl2/test.jsp
154               digest 640205b7b0fc66c1ea91c463fac6334d
155             }
156             url {
157               path /testurl3/test.jsp
158               digest 640205b7b0fc66c1ea91c463fac6334d
159             }
160             connect_timeout 3
161             retry 3
162             delay_before_retry 3
163         }
164     }

3、keepalived.conf 备份服务器的配置文件

! Configuration File for keepalived
  2 
  3 global_defs {
  4     smtp_server 192.168.88.129
  5     router_id LVS_DEVELBACK #访问到主机
  6 }
  7 
  8 vrrp_script check_k8s {
  9     # 自身状态检测
 10     script "/usr/local/src/nginx_check.sh"
 11     interval 3
 12     weight 5
 13 }
 14 
 15 vrrp_script chk_http_port {
 16    script "/usr/local/src/nginx_check.sh"
 17    #检测脚本执行的间隔
 18    interval 2
 19 
 20    weight 2
 21 }
 22 
 23 vrrp_instance VI_1 {
 24 
 25     # 初始化状态: MASTER;备份服务器修改为:BACKUP
 26     state BACKUP
 27 
 28     # 虚拟 ip 绑定的网卡
 29     interface ens33
 30 
 31     # 此 ID 要与 Backup 配置一致
 32     virtual_router_id 51
 33 
 34     # 默认启动优先级,要比 Backup 大点,但要控制量,保证自身状态监测生效
 35     priority 90
 36     advert_int 1
 37     authentication {
 38         auth_type PASS
 39         auth_pass 1111
 40     }
 41 
 42     virtual_ipaddress {
 43         # 虚拟 ip 地址
 44         192.168.200.16
 45     }
 46 
 47     track_script {
 48         check_k8s
 49         chk_http_port
 50     }
 51 }

二、 使用

看配置文件的注释

nginx.conf是适用于所有的,keepalived.conf文件只是用于高可用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值