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

相关推荐
闲云一鹤1 天前
nginx 快速入门教程 - 写给前端的你
前端·nginx·前端工程化
Sinclair3 天前
简单几步,安卓手机秒变服务器,安装 CMS 程序
android·服务器
初次攀爬者4 天前
ZooKeeper 实现分布式锁的两种方式
分布式·后端·zookeeper
Rockbean4 天前
用40行代码搭建自己的无服务器OCR
服务器·python·deepseek
茶杯梦轩4 天前
CompletableFuture 在 项目实战 中 创建异步任务 的核心优势及使用场景
服务器·后端·面试
何中应4 天前
Nginx转发请求错误
前端·后端·nginx
海天鹰5 天前
【免费】PHP主机=域名+解析+主机
服务器
不是二师兄的八戒5 天前
Linux服务器挂载OSS存储的完整实践指南
linux·运维·服务器
芝士雪豹只抽瑞克五5 天前
Nginx 高性能Web服务器笔记
服务器·nginx