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 

你可能感兴趣的:(Nginx安装、配置TCP请求转发)