分布式 - 服务器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,您将增加更多的开销,并且连接和临时端口都效率低下。

相关推荐
萧鼎4 分钟前
Python pyzmq 库详解:从入门到高性能分布式通信
开发语言·分布式·python
卡拉叽里呱啦3 小时前
缓存-变更事件捕捉、更新策略、本地缓存和热key问题
分布式·后端·缓存
繁星¹⁸⁹⁵3 小时前
通过update-alternatives可以实现cuda的多版本切换
服务器
BD_Marathon5 小时前
Kafka文件存储机制
分布式·kafka
开航母的李大5 小时前
软件系统运维常见问题
运维·服务器·系统架构·运维开发
哈哈很哈哈6 小时前
Spark 运行流程核心组件(三)任务执行
大数据·分布式·spark
Clownseven9 小时前
Docker+Nginx+Node.js实战教程:从零搭建高可用的前后端分离项目
nginx·docker·node.js
phoenix09819 小时前
Linux入门DAY27
linux·运维·服务器
xw510 小时前
免费的个人网站托管-PinMe篇
服务器·前端
egoist202312 小时前
【Linux仓库】进程创建与进程终止【进程·柒】
linux·运维·服务器·进程创建·写时拷贝·进程终止