nginx负载均衡

一、环境准备

1、vim /etc/hosts

      192.168.40.132   web2        192.168.40.211   web1     192.168.40.155   web3 (静态)
      192.168.40.129   nginx        192.168.40.200  php(动态)
2、安装nginx, 防火墙,selinux全关

3、vim /var/www/html/{index.html,index.php}

二、192.168.40.129  nginx端 配置

jspgou时的配置如下

在容器云下配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# cat nginx.conf
worker_processes 4;
worker_rlimit_nofile 40000;
 
events {
    worker_connections 8192;
}
 
stream {
    upstream rancher_servers_http {
        least_conn;
        server 192.168.10.12:80 max_fails=3 fail_timeout=5s;
        server 192.168.10.13:80 max_fails=3 fail_timeout=5s;
        server 192.168.10.14:80 max_fails=3 fail_timeout=5s;
    }
    server {
        listen     80;
        proxy_pass rancher_servers_http;
    }
    upstream rancher_servers_https {
        least_conn;
        server 192.168.10.12:443 max_fails=3 fail_timeout=5s;
        server 192.168.10.13:443 max_fails=3 fail_timeout=5s;
        server 192.168.10.14:443 max_fails=3 fail_timeout=5s;
    }
    server {
        listen     443;
        proxy_pass rancher_servers_https;
    }
}

# vim /etc/nginx/nginx.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
    worker_connections 1024;
}
http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
 
    access_log  /var/log/nginx/access.log  main;
 
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;
 
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    
 
include /etc/nginx/conf.d/*.conf;
upstream  html {
        server 192.168.40.132:80;
        server 192.168.40.211:80;
        server 192.168.40.155:80;
}
 
upstream  php {
        server 192.168.40.200:80;
}
    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;
 
        include /etc/nginx/default.d/*.conf;
        location / {
proxy_pass http://html;
        }
 
       location  ~ \.php$ {
    proxy_pass http://php;
}
        error_page 404 /404.html;
            location = /40x.html {
        }
 
        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
}

三、结果测试

结果如图所示

 

 

 

注意:nginx启动不了,考虑端口被占用情况

# netstat   -tunlp  |   grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      15606/haproxy    

# systemctl stop haproxy
# netstat -tunlp|grep 80

四、Nginx+Tomcat动静分离及Nginx优化

https://blog.51cto.com/lizhenliang/1343497

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
user  nginx;
worker_processes  1;
error_log  logs/error.log;
pid       logs/nginx.pid;
events {
    use epoll;
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    #日志格式定义
    log_format main  '$remote_addr - $remote_user[$time_local] "$request" '
                      '$status $body_bytes_sent"$http_referer" '
                     '"$http_user_agent" "$http_x_forwarded_for"';
    access_log logs/access.log  main;
    sendfile        on;
    keepalive_timeout  65;
    #gzip压缩功能设置
    gzip on;
    gzip_min_length 1k;
    gzip_buffers    4 16k;
    gzip_http_version 1.0;
    gzip_comp_level 2;
    gzip_types text/plain application/x-javascripttext/css application/xml;
    gzip_vary on;
    server {
        listen       80;
        server_name www.test.com;
        location / {
    #jsp网站程序根目录,一般nginx与tomcat在同一个目录
            root  /usr/local/tomcat/webapps/ROOT;
            index  index.html index.jsp index.html;
        }
        location ~ .*.jsp$ {
            index index.jsp;
            proxy_pass http://127.0.0.1:8080;   #来自jsp请求交给tomcat处理
            proxy_redirect off;
            proxy_set_header Host $host;    #后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            client_max_body_size 10m;   #允许客户端请求的最大单文件字节数
            client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数
            proxy_connect_timeout 90;   #nginx跟后端服务器连接超时时间
            proxy_read_timeout 90;      #连接成功后,后端服务器响应时间
            proxy_buffer_size 4k;       #设置代理服务器(nginx)保存用户头信息的缓冲区大小
            proxy_buffers 6 32k;        #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
            proxy_busy_buffers_size 64k;#高负荷下缓冲大小(proxy_buffers*2)
            proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传
        }
        location ~ .*\.(gif|jpg|png|bmp|swf)$   #由nginx处理静态页面
        {
            expires 30d;   #使用expires缓存模块,缓存到客户端30天
        }
        location ~ .*\.(jsp|js|css)?$
        {
            expires 1d;
        }
            error_page  404         /404.html;   #错误页面
            error_page  500 502 503 504  /50x.html;
            location = /50x.html {
            root html;
        }
    }
}

五、 nginx状态检查脚本(源码安装)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/bin/bash
 
HOST="127.0.0.1"
PORT="80"
 
# 检测nginx进程是否存在
function ping {
    /sbin/pidof nginx | wc -l
}
# 检测nginx性能
function active {
    /usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null| grep 'Active' | awk '{print $NF}'
}
function reading {
    /usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null| grep 'Reading' | awk '{print $2}'
}
function writing {
    /usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null| grep 'Writing' | awk '{print $4}'
}
function waiting {
    /usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null| grep 'Waiting' | awk '{print $6}'
}
function accepts {
    /usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null| awk NR==3 | awk '{print $1}'
}
function handled {
    /usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null| awk NR==3 | awk '{print $2}'
}
function requests {
    /usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null| awk NR==3 | awk '{print $3}'
}
# 执行function
$1

 

posted @   凡人半睁眼  阅读(457)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具
· Manus的开源复刻OpenManus初探

阅读目录(Content)

此页目录为空

点击右上角即可分享
微信分享提示