美文网首页
nginx-1.18.0 安装

nginx-1.18.0 安装

作者: _大叔_ | 来源:发表于2020-07-09 19:55 被阅读0次

    一、安装编译工具及库文件

    yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel
    

    二、首先要安装 PCRE

    PCRE 作用是让 Nginx 支持 Rewrite 功能。
    下载 PCRE 安装包

     wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
    

    解压

     tar zxvf pcre-8.35.tar.gz
    

    安装

    ./configure
    make && make install
    pcre-config --version
    

    三、安装 Nginx

    下载

    wget http://nginx.org/download/nginx-1.18.0.tar.gz
    

    解压

    tar -xvf nginx-1.18.0.tar.gz
    

    cd nginx-1.18.0 进入nginx模块 并安装

    ./configure --prefix=/home/nginx-1.18.0 --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-pcre=/home/pcre-8.35
    
    // 安装地址
    --prefix=/home/nginx-1.18.0
    //  监控模块
    --with-http_stub_status_module 
    // https
    --with-http_ssl_module
    // http2 
    --with-http_v2_module
    // pcre地址
    --with-pcre=/home/pcre-8.35
    

    安装

    make && make install
    

    四 nginx TCP连接(只支持1.9或以上)

    TCP 4层协议负载均衡:使用长连接的方式,只要clink与server保持链接,下个请求nginx不会轮询到其他server。
    http 7层协议协议:当你每请求一次nginx,就会轮询到下一台server上。当然 http是基于TCP的,TCP的性质是需要经过3次握手,4次分手操作。

    3次握手:

    1. 建立连接时,client发送SYN(SYN=J)到server,并进入SYN_SEND状态,等待server确认。
    2. server收到SYN包,必须确认client的SYN(ACK=SYN+1),同时自己也发送一个SYN包(SYN=K),即SYN+ACK包,此时server状态为V;
    3. client收到server的SYN+ACK包,向server发送确认包ACK(ACK=K+1),此包发送完毕,client和server进入 ESTABLISHED 状态,完成三次握手。完成握手clink向server传送数据。

    4次分手:
    由于TCP链接是全双工的,因此每个方向都必须单独进行关闭。这个原则是当一方完成它的数据发送任务后技能发送一个FIN来终止这个方向的链接。收到一个FIN只意味着这一方向上没有数据流动,一个TCP链接在收到一个FIN后任能发送数据。首先进行关闭的一方将执行主动关闭,而另一方执行被动关闭。

    1. clientA发送一个FIN,用来关闭clientA到serverA的数据传送。
    2. serverA收到这个FIN,它发回一个ACK,确认序号为收到的序号+1.和SYN一样,一个FIN将占用一个序号。
    3. serverA关闭与clientA的链接,发送一个FIN给clientA。
    4. clientA发回ACK报文确认,并将确认序号设置为收到的序号+1.

    区分tcp和http:
    TCP是传输层,而http是应用层,知道了 http是要基于TCP连接基础上的,简单的说,TCP就是单纯建立连接,不涉及任何我们需要请求的实际数据,简单的传输。http是用来收发数据,即实际应用上来的。

    下载 nginx_tcp_proxy_module

    wget https://github.com/yaoweibin/nginx_tcp_proxy_module/tarball/master
    tar -xvf yaoweibin-nginx_tcp_proxy_module-v0.4.5-40-gb8a3028.tar.gz
    # 自己先要创建个 package
    mv yaoweibin-nginx_tcp_proxy_module-b8a3028/ /home/nginx-1.18.0/package
    
    cd /home/nginx-1.18.0
    patch -p1 < /home/nginx-1.18.0/package/yaoweibin-nginx_tcp_proxy_module-b8a3028/tcp.patch
    ./configure --add-module=/home/nginx-1.18.0/package/yaoweibin-nginx_tcp_proxy_module-b8a3028
    make && make install
    

    -bash: patch: command not found 需要 yum -y install patch 安装即可
    官网地址: https://github.com/yaoweibin/nginx_tcp_proxy_module

    出现问题1

    http_request_parser.c:1096:1: note: here
    http_request_parser.c:1112:5: error: this statement may fall through [-Werror=implicit-fallthrough=]
    

    解决方式

    vim /home/nginx-1.18.0/objs/Makefile
    修改如下:
    #CFLAGS =  -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g 
    CFLAGS =  -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -g
    

    出现问题2

    /home/nginx-1.18.0/package/yaoweibin-nginx_tcp_proxy_module-b8a3028/modules/ngx_tcp_ssl_module.c:407:9: error: too few arguments to function ‘ngx_ssl_session_cache’
      407 |     if (ngx_ssl_session_cache(&conf->ssl, &ngx_tcp_ssl_sess_id_ctx,
          |         ^~~~~~~~~~~~~~~~~~~~~
    In file included from src/core/ngx_core.h:84,
                     from /home/nginx-1.18.0/package/yaoweibin-nginx_tcp_proxy_module-b8a3028/modules/ngx_tcp_ssl_module.c:3:
    src/event/ngx_event_openssl.h:194:11: note: declared here
      194 | ngx_int_t ngx_ssl_session_cache(ngx_ssl_t *ssl, ngx_str_t *sess_ctx,
    

    解决方式

    vim /home/nginx-1.18.0/package/yaoweibin-nginx_tcp_proxy_module-b8a3028/modules/ngx_tcp_ssl_module.c
    找到调用 ngx_ssl_session_cache 的地方,添加一个参数 conf->passwords,整体如下:
    
        if (ngx_ssl_session_cache(&conf->ssl, &ngx_tcp_ssl_sess_id_ctx,
                                  conf->passwords,
                                  conf->builtin_session_cache,
                                  conf->shm_zone, conf->session_timeout)
            != NGX_OK)
        {
            return NGX_CONF_ERROR;
        }
    

    重新

    make && make install
    

    实际使用

    # 修改支持TCP协议
    tcp{
        # 负载均衡
        upstream test{
            server 192.168.81.102:8080;
            server 192.168.81.102:8081;
        }
    
        server{
            listen 9999;
            server_name localhost;
            # 反向代理到 upstream
            proxy_pass test;
        }
    }
    
    # 允许的 http请求
    http{
    
    }
    

    相关文章

      网友评论

          本文标题:nginx-1.18.0 安装

          本文链接:https://www.haomeiwen.com/subject/vwhscktx.html