命令记不住

登陆

nginx 正向代理和反向代理

nginx正向代理和反向代理

1.代理类型

正向代理(内部上网) 客户端<-->代理->服务端

反向代理 客户端->代理<-->服务端

  • 正向与反向代理的区别
    • 区别在于代理的对象不一样
    • 正向代理代理的对象是客户端
    • 反向代理代理的对象是服务端

2.Nginx正向代理

Nginx正向代理配置实例
1.环境准备

角色  外网IP(NAT) 内网IP(LAN) 主机名
Proxy eth0:10.0.0.5 eth1:172.16.1.5 lb01
web01 eth0:10.0.0.7 eth1:172.16.1.7 web01

2.web01服务器, 配置一个网站,监听在8080,仅运行172网段能访问

[root@web01 ~]# cd /etc/nginx/conf.d/
[root@web01 conf.d]# vim web.conf
server {
    listen 8080;
    server_name 172.16.1.7;

    location / {
        root /code_8080;
        index index.html;
        deny 10.0.0.0/24;
        allow all;
    }
}
[root@web01 conf.d]# mkdir /code_8080
[root@web01 conf.d]# echo "web01-7...." >/code_8080/index.html
[root@web01 conf.d]# systemctl restart nginx

2.proxy代理服务器, 配置监听在80,让10.0.0.1客户端,能够通过代理访问到后端的172.16.1.7网站

[root@lb01 ~]# cd /etc/nginx/conf.d/
[root@lb01 conf.d]# cat proxy_web_node1.conf 
server {
    listen 80;
    server_name nginx.oldboy.com;

    location / {
        proxy_pass http://172.16.1.7:8080;
        include proxy_params;
    }
}

[root@lb01 conf.d]# systemctl enable nginx
[root@lb01 conf.d]# systemctl start nginx

.

3.七层反向代理

1.nginx配置HTTPS反向代理后端资源

# 定义后端资源池
upstream proxy {
 server 172.16.1.7:80 max_fails=2 fail_timeout=10s;
 server 172.16.1.8:80 max_fails=2 fail_timeout=10s;
}
# https配置
server {
     listen       443 ssl;
     server_name  opxdev.com;

     ssl_certificate   /etc/nginx/ssl_key/server.crt;
     ssl_certificate_key  /etc/nginx/ssl_key/server.key;
     ssl_session_timeout 5m;
     ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
     ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
     ssl_prefer_server_ciphers on;

     location /{
             proxy_pass https://proxy;
             proxy_set_header  Host $host:$server_port;
             proxy_set_header  X-Real-IP  $remote_addr;
             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
             client_max_body_size  100m;
     }

     error_log /var/log/proxy.log;

     error_page 404 /404.html;
     location = /404.html {
             root   /usr/share/nginx/html;
     }

     error_page 500 502 503 504 /50x.html;
     location = /50x.html {
             root   /usr/share/nginx/html;
     }

     location ~ /\.ht {
             deny  all;
     }
}
# 用户http请求跳转至https
server {
 listen 80;
 server_name opxdev.com;
 return 302 https://$server_name$request_uri;
}

重启Nginx 负载均衡

4.四层反向代理

1.需要用到proxy_pass代理模块配置

nginx 用yum源安装的,stream模块也用yum源安装

yum install -y nginx-mod-stream

安装所有模块

yum install -y nginx-all-modules
2.线上示例

主文件nginx.conf开启stream模块

worker_processes  1;
  load_module lib64/nginx/modules/ngx_stream_module.so;
events {
    worker_connections  1024;
    use epoll;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
    }
    sendfile        on;
    keepalive_timeout  65;
    include conf.d/*.conf;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   /srv/www/htdocs/;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /srv/www/htdocs/;
        }
    }

    include vhosts.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/tcp-access.log proxy ;
    open_log_file_cache off;
    include /etc/nginx/conf.d/*.stream;
}

子文件xxxx.stream配置反向代理

[admin@OPS /etc/nginx/conf.d] SUSE云端服务器
$cat hotwooolsession.stream
# 添加socket转发的代理
upstream session_proxy {
    hash $remote_addr consistent;
    # 转发的目的地址和端口
    server 192.168.49.2:32713 weight=5 max_fails=3 fail_timeout=60s;
}

# 提供转发的服务,即访问localhost:9001,会跳转至代理socket_proxy指定的转发地址
server {
    listen 20013;
    proxy_connect_timeout 3s;
    proxy_timeout 30s;
    proxy_pass session_proxy;
}

[admin@OPS /etc/nginx/conf.d] SUSE云端服务器
$cat hotwooolgateway.stream
# 添加socket转发的代理
upstream gateway_proxy {
    hash $remote_addr consistent;
    # 转发的目的地址和端口
    server 192.168.49.2:31742 weight=5 max_fails=3 fail_timeout=60s;
}

# 提供转发的服务,即访问localhost:9001,会跳转至代理socket_proxy指定的转发地址
server {
    listen 20021;
    proxy_connect_timeout 3s;
    proxy_timeout 30s;
    proxy_pass gateway_proxy;
}
3.Nginx负载均衡TCP实践
  • 配置Nginx4层负载均衡实现如下需求
    • 1.通过访问负载均衡的5555端口,实际是后端的web01的22端口在提供服务。
    • 2.通过访问负载均衡的6666端口,实际是后端的mysql的3306端口在提供服务。

1.Nginx四层负载均衡配置语法

stream {
    upstream backend {
        hash $remote_addr consistent;
        server backend1.example.com:12345 weight=5;
        server 127.0.0.1:12345 max_fails=3 fail_timeout=30s;
        server unix:/tmp/backend3;
    }
    server {
        listen 12345;
        proxy_connect_timeout 1s;
        proxy_timeout 3s;
        proxy_pass backend;
    }
}

2.Nginx四层负载均衡实战

[root@lb01 ~]# mkdir -p /etc/nginx/conf.c
[root@lb01 ~]# vim /etc/nginx/nginx.conf
# 在events层下面,http层上面配置include
include  /etc/nginx/conf.c/*.conf;

# 编写四层代理配置
[root@lb01 ~]# cd /etc/nginx/conf.c/
[root@lb01 conf.c]# cat stream.conf 
stream {
    #1.定义虚拟资源池
    upstream ssh {
        server 172.16.1.7:22;
    }

    upstream mysql {
        server 172.16.1.51:3306;
    }
    #2.调用虚拟资源池
    server {
        listen 5555;
        proxy_connect_timeout 1s;
        proxy_timeout 300s;
        proxy_pass ssh;
    }
    server {
        listen 6666;
        proxy_connect_timeout 1s;
        proxy_timeout 300s;
        proxy_pass mysql;
    }
}
[root@lb01 conf.c]# systemctl restart nginx

.

5.相关优化

参考:https://www.opsxdev.com/articles/2021/02/16/1613405124626

推荐阅读