Nginx安裝、配置TCP請求轉發

一、下載nginx

管網鏡像
http://nginx.org/download/

下載時需要注意:
nginx1.9.0版本后,才支持--with-stream 模塊

獲取nginx,官方地址

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

二、環境準備

安裝之前必先確認是否安裝 gcc 環境(gcc -v),如果沒有安裝,用root用戶執行以下命令進行安裝

yum install -y gcc 

安裝依賴

yum -y install pcre-devel openssl openssl-devel library

本文采用非root用戶編譯安裝,新建安裝用戶fym

三、編譯安裝

1.解壓

tar -zxvf nginx-1.17.9.tar.gz

2.配置
進入到解壓目錄,開啟端口轉發:--with-stream --with-stream_ssl_module

./configure  --prefix=/home/fym/nginx  --sbin-path=/home/fym/nginx/sbin/nginx --conf-path=/home/fym/nginx/conf/nginx.conf --error-log-path=/home/fym/nginx/log/error.log  --http-log-path=/home/fym/nginx/log/access.log  --pid-path=/home/fym/nginx/nginx.pid --lock-path=/home/fym/nginx/nginx.lock  --user=fym --group=fym --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --http-client-body-temp-path=/home/fym/nginx/tmp/client/ --http-proxy-temp-path=/home/fym/nginx/tmp/proxy/ --http-fastcgi-temp-path=/home/fym/nginx/tmp/fcgi/ --http-uwsgi-temp-path=/home/fym/nginx/tmp/uwsgi --http-scgi-temp-path=/home/fym/nginx/tmp/scgi --with-pcre --with-stream --with-stream_ssl_module

3.編譯安裝

make & make install

四、配置

worker_processes  1;

events {
    worker_connections  1024;
}

# 此為TCP轉發請求
stream {
    # 后端指向 server 的 28017 端口 stream_mongo 組
    upstream stream_mongo {
         server 192.168.62.15:28017;
         server 192.168.62.37:28017;
    }
    
    # 后端指向 server 的 8090 端口 cns組
    upstream stream_mysql {
         server 192.168.62.15:3306;
         server 192.168.62.37:3306;
    }
    
    #server {
    #    listen                443 ssl;
    #    proxy_pass            stream_backend;
    #    # 指定key 和 crt 地址
    #    ssl_certificate       /etc/ssl/certs/my.crt;
    #    ssl_certificate_key   /etc/ssl/certs/my.key;
    #    ssl_protocols         SSLv3 TLSv1 TLSv1.1 TLSv1.2;
    #    ssl_ciphers           HIGH:!aNULL:!MD5;
    #    ssl_session_cache     shared:SSL:20m;
    #    ssl_session_timeout   4h;
    #    ssl_handshake_timeout 30s;
    #}
    
    server {
        # 本機監聽端口 28017 
        listen                28017;
        
        # 請求拋給 stream_mongo 組
        proxy_pass            stream_mongo;
    }
    server {
        # 本機監聽端口 3306 
        listen                3306;
        
        # 請求拋給 stream_mysql 組
        proxy_pass            stream_mysql;
    }      
}

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;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    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;
        #}
    }
}

五、常用指令

#啟動
./sbin/nginx -c ./conf/nginx.conf
#停止
./sbin/nginx -s stop 
#檢查配置文件
./sbin/nginx -t
#動態加載配置文件
./sbin/nginx -s reload 
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容