分布式 - 服务器Nginx:一小时入门系列之TCP反向代理和负载均衡

文章目录

      • [1. HTTP反向代理和TCP反向代理](#1. HTTP反向代理和TCP反向代理)
      • [2. http 块和 stream 块](#2. http 块和 stream 块)
      • [3. TCP反向代理配置](#3. TCP反向代理配置)
      • [4. TCP 负载均衡](#4. TCP 负载均衡)

1. HTTP反向代理和TCP反向代理

Nginx可以作为HTTP反向代理和TCP反向代理。

HTTP反向代理是指Nginx作为Web服务器的代理服务器,接收客户端的HTTP请求,然后将请求转发给后端的Web服务器,最后将Web服务器的响应返回给客户端。这种方式可以实现负载均衡、缓存、SSL终止等功能。

TCP反向代理是指Nginx作为TCP服务器的代理服务器,接收客户端的TCP连接请求,然后将请求转发给后端的TCP服务器,最后将TCP服务器的响应返回给客户端。这种方式可以实现负载均衡、高可用性、SSL终止等功能。TCP反向代理可以用于代理各种TCP协议,如SMTP、POP3、IMAP、FTP等。

2. http 块和 stream 块

在 Nginx 配置文件中,http 块和 stream 块是两种不同的块类型,它们分别用于处理 HTTP 和 TCP/UDP 流量。http块用于配置 HTTP 服务器,包括监听端口、虚拟主机、反向代理、负载均衡、缓存等功能。stream 块用于配置 TCP/UDP 服务器,包括监听端口、负载均衡、代理等功能。

nginx 复制代码
#HTTP代理
http {
  server {
    listen 8002;
    proxy_pass http://localhost:8080/;
  }
}

#TCP代理
stream {
  server {
    listen 13306;
    proxy_pass localhost:3306;
  }
}

3. TCP反向代理配置

① Nginx 配置文件:/etc/nginx/nginx.conf

nginx 复制代码
user  nginx;
worker_processes  auto;
error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;
events {
    worker_connections  1024;
}

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"';

    access_log  /var/log/nginx/access.log  main;
    sendfile        on;
    keepalive_timeout  65;
    include /etc/nginx/conf.d/*.conf;
}

/etc/nginx/conf.d 目录是包含在 http 块中的,所以TCP负载均衡不能再配置在 /etc/nginx/conf.d 目录下的配置文件中了,需要和在 nginx.conf 中配置,并且和 http 块同级。

② Nginx配置文件: /etc/nginx/nginx.conf

nginx 复制代码
user  nginx;
worker_processes  auto;
error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

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"';

    access_log  /var/log/nginx/access.log  main;
    sendfile        on;
    keepalive_timeout  65;
    include /etc/nginx/conf.d/*.conf;
}
        
stream {
  server {
    # 监听13306端口
    listen 13306;
    # 将流量代理到本地的3306端口(即MySQL数据库的默认端口)
    proxy_pass localhost:3306;
  }
}

MySQL使用TCP协议来实现客户端和服务器之间的通信,而默认的端口号是3306。 Nginx配置用于将来自13306端口的MySQL流量代理到本地的3306端口。这种配置通常用于将MySQL服务器隐藏在Nginx服务器后面,以提高安全性并允许更好的负载均衡。

③ 重启Nginx服务:

shell 复制代码
[root@nginx-dev ~]# nginx -s reload

④ 利用13306端口连接MySQL:

4. TCP 负载均衡

nginx 复制代码
user  nginx;
worker_processes  auto;
error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

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"';

    access_log  /var/log/nginx/access.log  main;
    sendfile        on;
    keepalive_timeout  65;
    include /etc/nginx/conf.d/*.conf;
}
        
stream {
  
  upstream backend-mysql {
    server localhost:3306;
    server localhost:3307;
    keepalive 8;
  }
  
  server {
    listen 13306;
    proxy_pass backend-mysql;
  }
}

使用keepalive定义连接池里空闲连接的数量。keepalive_timeout 默认60s。如果连接池里的连接空闲时间超过这个值,则连接关闭。

使用 keepalive 指令启用从 NGINX Plus 到上游服务器的保持活动连接,定义在每个工作进程的缓存中保留的与上游服务器的空闲保持活动连接的最大数量。当超过此数字时,将关闭最近最少使用的连接。如果没有 keepalives,您将增加更多的开销,并且连接和临时端口都效率低下。

相关推荐
翼龙云_cloud2 小时前
阿里云渠道商:如何手动一键扩缩容ECS实例?
运维·服务器·阿里云·云计算
墨风如雪2 小时前
拒绝被找回!MJJ必修课:Outlook邮箱交易后的“防回手”安全设置全攻略
服务器
DX_水位流量监测3 小时前
大坝安全监测之渗流渗压位移监测设备技术解析
大数据·运维·服务器·网络·人工智能·安全
电商API&Tina3 小时前
京东 API 数据采集接口接入与行业分析
运维·服务器·网络·数据库·django·php
Yeats_Liao3 小时前
MindSpore开发之路(二十四):MindSpore Hub:快速复用预训练模型
人工智能·分布式·神经网络·机器学习·个人开发
Mr_Xuhhh3 小时前
博客标题:深入理解Shell:从进程控制到自主实现一个微型Shell
linux·运维·服务器
864记忆4 小时前
Qt创建连接注意事项
数据库·qt·nginx
IT大白4 小时前
2、Kafka原理-Producer
分布式·kafka
m0_738120727 小时前
应急响应——知攻善防靶场Linux-1详细应急过程
linux·运维·服务器·网络·web安全·ssh
GHL2842710907 小时前
无法连接服务端socket
linux·服务器·网络