Nginx负载均衡应用

CentOS7安装Nginx

一.安装所需环境

Nginx 是 C 语言开发,建议在 Linux 上运行。

1.1 gcc 安装

安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装:

yum install gcc-c++

1.2 PCRE pcre-devel 安装

PCRE(Perl Compatible Regular Expressions) 是一个 Perl 库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。

yum install -y pcre pcre-devel

1.3 zlib 安装

zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。

yum install -y zlib zlib-devel

1.4 OpenSSL 安装

OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。
nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。

yum install -y openssl openssl-devel

二.Nginx下载

481

安装下载之后解压进入nginx根目录。结构如下:

nginx-1.18.0
├── auto
├── CHANGES
├── CHANGES.ru
├── conf
├── configure
├── contrib
├── html
├── LICENSE
├── Makefile
├── man
├── objs
├── README
└── src

使用默认配置,执行命令

./configure

2.1 编译安装

make
make install

上述命令执行完成之后,可以在路径 /usr/local/nginx 下找到编译完成的 nginx 目录

将路径 /usr/local/nginx/sbin 下的可执行文件 nginx 复制到路径 /usr/local/bin 下方便直接执行命令

至此,安装大功告成!

三.应用:负载均衡

Nginx负载均衡的详细配置及使用案例详解.

The request signature we calculated does not match the signature you provided. Check your key and signing method解决方法

Nginx 之 转发数据时请保持 Host

MinIO 有对 Java 的 SDK 支持,可是在初始化 MinIO 对象的时候会把 ip 写死,对于某个节点来说访问压力过大,因此希望通过 Nginx 的反向代理和负载均衡,将访问的请求分配到每个节点上。

3.1.Nacos 中的 MinIO 相关配置

minio:
  # url: http://10.8.108.105:9001
  # url: http://ybsface.bgyfoodom.com:8081/minio
  url: http://192.168.133.112:8099
  # url: http://192.168.133.112:9000
  access-key: minioadmin
  secret-key: minioadmin

3.2.Nginx 中关于 MinIO 的配置

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    //nginx 日志输出相关格式
    log_format  main  '$remote_addr - $remote_user [$time_local] $http_x_Forwarded_for $remote_addr "$request" '
		      'http_x_forwarded_for:$http_x_forwarded_for '
		      'upstream_addr:$upstream_addr '
		      'http_host:$http_host '
		      'ups_resp_time:$upstream_response_time '
		      'request_time:$request_time '		
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for" ';

    access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    upstream minio-server{
    	
	#ip_hash;
        server 192.168.133.112:9000 weight=1 max_fails=2 fail_timeout=30;
        server 192.168.133.113:9000 weight=2 max_fails=2 fail_timeout=30;
    }

    server {
        
    	listen 8099;
    	location / {
            
            #这个配置是保持请求主机不变,例如:访问 192.168.133.112:8099,如果不添加以下配置,host将会被替换为 http://minio-server,minio对象会报签名错误(因为当初初始化的host不同)
            proxy_set_header Host $http_host;
            # 下面这个配置可以在浏览器控制台获取到真实的ip    
            #add_header backendIP $upstream_addr;
            #add_header backendCode $upstream_status;
            proxy_pass http://minio-server;
        }
    }

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

3.3.日志打印格式化输出

log_format  main  '$remote_addr - $remote_user [$time_local] $http_x_Forwarded_for $remote_addr "$request" '
                      'http_x_forwarded_for:$http_x_forwarded_for '
                      'upstream_addr:$upstream_addr ' #输出上游服务器的ip,也就真实转发的ip
                      'http_host:$http_host ' #输出请求的ip
                      'ups_resp_time:$upstream_response_time '
                      'request_time:$request_time '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for" ';

    access_log  logs/access.log  main;

以上配置在 http 模块里

3.4.请求接口,日志输出

我在 nginx 中配置的权重是 113:112 = 2:1,日志输出如下

482

192.168.133.1 - - [05/Nov/2020:17:40:35 +0800] - 192.168.133.1 "HEAD /image HTTP/1.1" http_x_forwarded_for:- upstream_addr:192.168.133.113:9000 http_host:192.168.133.112:8099 ups_resp_time:0.007 request_time:0.007 200 0 "-" "Minio (amd64; amd64) minio-java/dev" "-"
192.168.133.1 - - [05/Nov/2020:17:40:36 +0800] - 192.168.133.1 "PUT /image/skilllogo/70eda297-6046-4e85-944d-8b3b96dddad3.jpg HTTP/1.1" http_x_forwarded_for:- upstream_addr:192.168.133.112:9000 http_host:192.168.133.112:8099 ups_resp_time:0.068 request_time:0.069 200 0 "-" "Minio (amd64; amd64) minio-java/dev" "-"
192.168.133.1 - - [05/Nov/2020:17:40:37 +0800] - 192.168.133.1 "HEAD /image HTTP/1.1" http_x_forwarded_for:- upstream_addr:192.168.133.113:9000 http_host:192.168.133.112:8099 ups_resp_time:0.002 request_time:0.002 200 0 "-" "Minio (amd64; amd64) minio-java/dev" "-"
192.168.133.1 - - [05/Nov/2020:17:40:37 +0800] - 192.168.133.1 "PUT /image/skilllogo/da430454-4471-4ab0-8062-c3870b951e15.jpg HTTP/1.1" http_x_forwarded_for:- upstream_addr:192.168.133.113:9000 http_host:192.168.133.112:8099 ups_resp_time:0.111 request_time:0.112 200 0 "-" "Minio (amd64; amd64) minio-java/dev" "-"
192.168.133.1 - - [05/Nov/2020:17:40:37 +0800] - 192.168.133.1 "HEAD /image HTTP/1.1" http_x_forwarded_for:- upstream_addr:192.168.133.112:9000 http_host:192.168.133.112:8099 ups_resp_time:0.001 request_time:0.000 200 0 "-" "Minio (amd64; amd64) minio-java/dev" "-"
192.168.133.1 - - [05/Nov/2020:17:40:38 +0800] - 192.168.133.1 "PUT /image/skilllogo/8cbf43bd-6f17-4baf-8b4f-783b4597b052.jpg HTTP/1.1" http_x_forwarded_for:- upstream_addr:192.168.133.113:9000 http_host:192.168.133.112:8099 ups_resp_time:0.071 request_time:0.071 200 0 "-" "Minio (amd64; amd64) minio-java/dev" "-"
192.168.133.1 - - [05/Nov/2020:17:40:38 +0800] - 192.168.133.1 "HEAD /image HTTP/1.1" http_x_forwarded_for:- upstream_addr:192.168.133.113:9000 http_host:192.168.133.112:8099 ups_resp_time:0.001 request_time:0.003 200 0 "-" "Minio (amd64; amd64) minio-java/dev" "-"
192.168.133.1 - - [05/Nov/2020:17:40:38 +0800] - 192.168.133.1 "PUT /image/skilllogo/373f45cb-4e6d-42fc-85b1-32956ee12f78.jpg HTTP/1.1" http_x_forwarded_for:- upstream_addr:192.168.133.112:9000 http_host:192.168.133.112:8099 ups_resp_time:0.078 request_time:0.078 200 0 "-" "Minio (amd64; amd64) minio-java/dev" "-"
192.168.133.1 - - [05/Nov/2020:17:40:38 +0800] - 192.168.133.1 "HEAD /image HTTP/1.1" http_x_forwarded_for:- upstream_addr:192.168.133.113:9000 http_host:192.168.133.112:8099 ups_resp_time:0.002 request_time:0.003 200 0 "-" "Minio (amd64; amd64) minio-java/dev" "-"
192.168.133.1 - - [05/Nov/2020:17:40:39 +0800] - 192.168.133.1 "PUT /image/skilllogo/d8ee147e-02f9-4389-8d76-a70bf6301bd7.jpg HTTP/1.1" http_x_forwarded_for:- upstream_addr:192.168.133.113:9000 http_host:192.168.133.112:8099 ups_resp_time:0.098 request_time:0.098 200 0 "-" "Minio (amd64; amd64) minio-java/dev" "-"
192.168.133.1 - - [05/Nov/2020:17:40:43 +0800] - 192.168.133.1 "HEAD /image HTTP/1.1" http_x_forwarded_for:- upstream_addr:192.168.133.112:9000 http_host:192.168.133.112:8099 ups_resp_time:0.001 request_time:0.002 200 0 "-" "Minio (amd64; amd64) minio-java/dev" "-"
192.168.133.1 - - [05/Nov/2020:17:40:43 +0800] - 192.168.133.1 "PUT /image/skilllogo/19ff650a-76a0-48f5-bd5d-e4ae589db110.jpg HTTP/1.1" http_x_forwarded_for:- upstream_addr:192.168.133.113:9000 http_host:192.168.133.112:8099 ups_resp_time:0.099 request_time:0.100 200 0 "-" "Minio (amd64; amd64) minio-java/dev" "-"
192.168.133.1 - - [05/Nov/2020:17:40:45 +0800] - 192.168.133.1 "HEAD /image HTTP/1.1" http_x_forwarded_for:- upstream_addr:192.168.133.113:9000 http_host:192.168.133.112:8099 ups_resp_time:0.002 request_time:0.002 200 0 "-" "Minio (amd64; amd64) minio-java/dev" "-"
192.168.133.1 - - [05/Nov/2020:17:40:45 +0800] - 192.168.133.1 "PUT /image/skilllogo/545d79bd-4a41-4421-a62a-cb4dd8534d62.jpg HTTP/1.1" http_x_forwarded_for:- upstream_addr:192.168.133.112:9000 http_host:192.168.133.112:8099 ups_resp_time:0.082 request_time:0.082 200 0 "-" "Minio (amd64; amd64) minio-java/dev" "-"
192.168.133.1 - - [05/Nov/2020:17:40:46 +0800] - 192.168.133.1 "HEAD /image HTTP/1.1" http_x_forwarded_for:- upstream_addr:192.168.133.113:9000 http_host:192.168.133.112:8099 ups_resp_time:0.002 request_time:0.002 200 0 "-" "Minio (amd64; amd64) minio-java/dev" "-"
192.168.133.1 - - [05/Nov/2020:17:40:46 +0800] - 192.168.133.1 "PUT /image/skilllogo/d549ff45-4243-47ba-864e-e40572c7983f.jpg HTTP/1.1" http_x_forwarded_for:- upstream_addr:192.168.133.113:9000 http_host:192.168.133.112:8099 ups_resp_time:0.108 request_time:0.107 200 0 "-" "Minio (amd64; amd64) minio-java/dev" "-"
192.168.133.1 - - [05/Nov/2020:17:40:47 +0800] - 192.168.133.1 "HEAD /image HTTP/1.1" http_x_forwarded_for:- upstream_addr:192.168.133.112:9000 http_host:192.168.133.112:8099 ups_resp_time:0.001 request_time:0.001 200 0 "-" "Minio (amd64; amd64) minio-java/dev" "-"
192.168.133.1 - - [05/Nov/2020:17:40:47 +0800] - 192.168.133.1 "PUT /image/skilllogo/f9c0cdef-12ad-4472-a185-d2d4316c306a.jpg HTTP/1.1" http_x_forwarded_for:- upstream_addr:192.168.133.113:9000 http_host:192.168.133.112:8099 ups_resp_time:0.102 request_time:0.102 200 0 "-" "Minio (amd64; amd64) minio-java/dev" "-"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值