nginx+sticky+SSL安装及负载均衡使用

sticky的工作原理

Sticky是nginx的一个模块,它是基于cookie的一种nginx的负载均衡解决方案,通过分发和识别cookie,来使同一个客户端的请求落在同一台服务器上,默认标识名为route

客户端首次发起访问请求,nginx接收后,发现请求头没有cookie,则以轮询方式将请求分发给后端服务器。
后端服务器处理完请求,将响应数据返回给nginx。
此时nginx生成带route的cookie,返回给客户端。route的值与后端服务器对应,可能是明文,也可能是md5、sha1等Hash值
客户端接收请求,并保存带route的cookie。
当客户端下一次发送请求时,会带上route,nginx根据接收到的cookie中的route值,转发给对应的后端服务器。

一、安装必要的依赖包

yum -y install gcc-c++  pcre pcre-devel  zlib zlib-devel openssl openssl-devel --setopt=protected_multilib=false libxml2 libxml2-dev  libxslt-devel  gd-devel perl-devel perl-ExtUtils-Embed gperftools

二、获取安装包

wget https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/get/master.tar.gz
wget http://nginx.org/download/nginx-1.19.8.tar.gz

三、解压安装包

tar -zxvf master.tar.gz
mv nginx-goodies-nginx-sticky-module-ng-08a395c66e42/  /usr/local/nginx-sticky-module
tar -zxvf   nginx-1.19.8.tar.gz

四、编译安装

cd  nginx-1.19.8

./configure  --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-stream_ssl_preread_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-http_auth_request_module --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-google_perftools_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E' --add-module=/usr/local/nginx-sticky-module

make && make install

五、将nginx 加入系统服务

vim /lib/systemd/system/nginx.service

[Unit]  
Description=The nginx HTTP and reverse proxy server  
After=syslog.target network.target remote-fs.target nss-lookup.target  


[Service]  
Type=forking  
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t  
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID  
ExecStop=/bin/kill -s QUIT $MAINPID  
PrivateTmp=true  


[Install]  
WantedBy=multi-user.target

#修改权限
chmod 745 /lib/systemd/system/nginx.service
#开机自启
systemctl enable nginx.service

六、配置nginx


# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/


user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;


# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;


events {
    worker_connections 1024;
}


http {


    upstream xxx.com{
        sticky;
        server 0.0.0.0:8080 weight=1;
        server 0.0.0.0:8080 weight=1;
        keepalive 256;
    }
    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;


    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;


    server {
        listen       80;
#        listen       [::]:80;
        server_name  xxx.com;
        rewrite ^ https://$http_host$request_uri? permanent;
        #root         /usr/share/nginx/html;


        # Load configuration files for the default server block.
        #include /etc/nginx/default.d/*.conf;


        #location / {
        #}


       # error_page 404 /404.html;
        #    location = /40x.html {
        #}


        #error_page 500 502 503 504 /50x.html;
         #   location = /50x.html {
        #}
    }


# Settings for a TLS enabled server.


    server {
        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
        server_name  xxx.com;
#        root         /usr/share/nginx/html;


        ssl_certificate "/etc/nginx/cert/server.crt";
        ssl_certificate_key "/etc/nginx/cert/server.key";
        ssl_session_cache shared:SSL:1m;
        ssl_session_timeout  5m;
        ssl_ciphers HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers on;


        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;


        location / {
             proxy_pass http://xxx.com;
            # proxy_set_header Host "xxx.com";
            # proxy_redirect default;
             proxy_set_header   Host             $host;
             proxy_http_version 1.1;                     #websocket
             proxy_set_header Upgrade $http_upgrade;     #websocket
             proxy_set_header Connection "upgrade";      #websocket
             proxy_set_header   X-Real-IP        $remote_addr;
             proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        }


        error_page 404 /404.html;
            location = /40x.html {
        }


        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }


}

七、证书上传

错误解决方法

在/etc/nginx/目录下新建cert文件夹、上传证书到此文件夹下

错误1:./configure: error: the invalid value in --with-ld-opt="-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E"

解决方法:yum -y install redhat-rpm-config.noarch

错误2:./configure: error: the HTTP image filter module requires the GD library.

解决方法: yum -y install gd-devel

错误3:./configure: error: perl module ExtUtils::Embed is required

解决方法: yum -y install perl-devel perl-ExtUtils-Embed

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值