nginx负载均衡和http代理与tcp-udp代理

nginx自1.9.0开始提供tcp/udp的反向代理功能

1、下载 nginx1.9以上版本
2、nginx安装绿色版本,方便下次部署。
./configure  --prefix=/home/my/nginx --with-stream 
3、然后 make 和 make install
4、--with-stream 为tcp/udp使用
5、--prefix=/home/my/nginx 安装位置,如果复制到其他服务器使用路径要保持一致

web负载和代理

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;
    
    #代理服务器 负载使用
    upstream  myserver{
     #某个ip的请求定向到同一台后端web机器中
      ip_hash;
      server  127.0.0.1:8086;
      server  127.0.0.1:8081;
    }
    
    server {
        #监听80端口
        listen       80;
        #根据路径转发
        location /abc/ {
         proxy_pass         http://127.0.0.1:8189/;
         proxy_set_header   Host             $host; 
         proxy_set_header   X-Real-IP        $remote_addr; 
         proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for; 
        }

        location / {
         proxy_pass         http://myserver;
         proxy_set_header   Host             $host;
         proxy_set_header   X-Real-IP        $remote_addr;
         proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    #根据端口转发
    server {
        #监听80端口
        listen       8080;
        location / {
         proxy_pass         http://172.16.0.183:8080/;
         #增加真实ip地址
         add_header backendIp  $upstream_addr;
         proxy_set_header   Host             $host;
         proxy_set_header   X-Real-IP        $remote_addr;
         proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    
}
lsof -i:8080
查看端口监听是否启动
curl -i localhost:8080
curl -i localhost/abc/
curl -i localhost/
查看返回结果

tcp/udp端口代理

    #stream 和 http 模块是同级别的 有http代理的时候不要放错位置
    stream{
         upstream abc{
            server 172.16.0.164:22 weight=5 max_fails=3 fail_timeout=30s;
         }
         server{
            listen 10000;
            proxy_connect_timeout 1s;
            proxy_timeout 3s;
            proxy_pass abc;
        }
    }
测试:
ssh -p 10000 [email protected]
提示输入密码则表示成功

你可能感兴趣的:(nginx负载均衡和http代理与tcp-udp代理)