centos7 編譯安裝nginx+tcp+grpc轉發

1、依賴html

1. gcc 安裝
安裝 nginx 須要先將官網下載的源碼進行編譯,編譯依賴 gcc 環境,若是沒有 gcc 環境,則須要安裝:linux

yum install gcc-c++

2. PCRE pcre-devel 安裝
PCRE(Perl Compatible Regular Expressions) 是一個Perl庫,包括 perl 兼容的正則表達式庫。nginx 的 http 模塊使用 pcre 來解析正則表達式,因此須要在 linux 上安裝 pcre 庫,pcre-devel 是使用 pcre 開發的一個二次開發庫。nginx也須要此庫。命令:nginx

yum install -y pcre pcre-devel

3. zlib 安裝
zlib 庫提供了不少種壓縮和解壓縮的方式, nginx 使用 zlib 對 http 包的內容進行 gzip ,因此須要在 Centos 上安裝 zlib 庫。c++

yum install -y zlib zlib-devel

4. OpenSSL 安裝
OpenSSL 是一個強大的安全套接字層密碼庫,囊括主要的密碼算法、經常使用的密鑰和證書封裝管理功能及 SSL 協議,並提供豐富的應用程序供測試或其它目的使用。
nginx 不只支持 http 協議,還支持 https(即在ssl協議上傳輸http),因此須要在 Centos 安裝 OpenSSL 庫。git

yum install -y openssl openssl-devel
yum install -y gcc-c++  pcre pcre-devel zlib zlib-devel openssl openssl-devel 

2、安裝包下載github

直接下載.tar.gz安裝包,地址:https://nginx.org/en/download.html正則表達式

wget -c https://nginx.org/download/nginx-1.14.0.tar.gz

3、解壓算法

tar zxvf nginx-1.14.0.tar.gz

4、配置安裝sql

cd nginx-1.14.0
./configure --prefix=/usr/local/nginx  --with-stream
make && make install
mkdir -p /var/cache/nginx /etc/nginx/http.d /etc/nginx/stream.d

5、配置nginx轉發api

stream {
    upstream proxy_card {
        # simple round-robin  轉發IP和端口
        server 192.168.1.12:12340;
        server 192.168.1.13:12340;
        #check interval=3000 rise=2 fall=5 timeout=1000;
        #check interval=3000 rise=2 fall=5timeout=1000
        #check interval=3000 rise=2 fall=5timeout=1000
        #check_http_send "GET /HTTP/1.0\r\n\r\n";
        #check_http_expect_alive http_2xxhttp_3xx;
    }
    server {
        listen 12340; #監聽端口
        proxy_pass proxy_card;  #轉發請求
    }
}

6、工具

查找安裝路徑:

whereis nginx
啓動中止
cd /usr/local/nginx/sbin/
./nginx 
./nginx -s stop
./nginx -s quit
./nginx -s reload
./nginx -s quit:此方式中止步驟是待nginx進程處理任務完畢進行中止。
./nginx -s stop:此方式至關於先查出nginx進程id再使用kill命令強制殺掉進程。

查詢nginx進程:

ps aux|grep nginx

重啓

1.先中止再啓動(推薦):
對 nginx 進行重啓至關於先中止再啓動,即先執行中止命令再執行啓動命令。以下:

./nginx -s quit
./nginx
2.從新加載配置文件:
當 ngin x的配置文件 nginx.conf 修改後,要想讓配置生效須要重啓 nginx,使用-s reload不用先中止 ngin x再啓動 nginx 便可將配置信息在 nginx 中生效,以下:
./nginx -s reload

自啓動:

即在rc.local增長啓動代碼就能夠了。

vi /etc/rc.local
增長一行 /usr/local/nginx/sbin/nginx
設置執行權限:

chmod 755 rc.local

7、tcp轉發測試

 編輯/etc/nginx/nginx.conf文件,追加

stream {
    log_format proxy '$remote_addr [$time_local] '
                     '$protocol $status $bytes_sent $bytes_received '
                     '$session_time "$upstream_addr" '
                     '"$upstream_bytes_sent" "$upstream_bytes_received" "$upstream_connect_time"';
    access_log /var/log/nginx/stream.access.log proxy;


    upstream slb_test_apiserver_local {
        server 172.31.185.139:6443 weight=5 max_fails=3 fail_timeout=30s;
        server 172.31.185.138:6443 weight=5 max_fails=3 fail_timeout=30s;
        server 172.31.185.137:6443 weight=5 max_fails=3 fail_timeout=30s;
    }

    server {
        listen 6443;
        proxy_pass slb_test_apiserver_local;
        proxy_connect_timeout 1s;
        proxy_timeout 3s;
        access_log /var/log/nginx/slb_test_apiserver_local.log proxy;
    }
}

重啓nginx,測試成功

8、快速編譯配置

快速編譯能夠應付大部分狀況

./configure --prefix=/etc/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 --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --add-module=/tmp/echo-nginx-module

其中,如不須要echo模塊,將最後一個選項去掉,如須要echo模塊,從github上面拉到指定位置

 

9、grpc協議轉發

upstream myservice {
    server 10.0.0.1:8888 weight=5 max_fails=3 fail_timeout=30s;
    server 10.0.0.2:8888 weight=5 max_fails=3 fail_timeout=30s;
}
server {
    listen   9999 http2 ;
    server_name www.myhost.com;
    if ( $host != "www.myhost.com"){
        return 404;
    }
    location / {
        grpc_pass grpc://myservice;
        proxy_connect_timeout 1s;
    }
    access_log  /var/log/nginx/myservice.log  main;
}

 

10、nginx.conf配置文件以下

user  nobody;

#Single core
worker_processes  2;

#Multicore
#worker_processes     8;
#worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  10240;
}


http {
    include       /etc/nginx/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" "$http_host"'
                      '$request_time $upstream_response_time $pipe - $upstream_addr';

    log_format  post_format $request_body;

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  120;
    proxy_connect_timeout 600;
    proxy_send_timeout 600s;
    proxy_read_timeout 600s;

    #gzip  on;

    include /etc/nginx/http.d/*.conf;
}


stream {
    log_format proxy '$remote_addr [$time_local] '
                     '$protocol $status $bytes_sent $bytes_received '
                     '$session_time "$upstream_addr" '
                     '"$upstream_bytes_sent" "$upstream_bytes_received" "$upstream_connect_time"';
    access_log /var/log/nginx/stream.access.log proxy;

    include /etc/nginx/stream.d/*.conf;
}
相關文章
相關標籤/搜索