分布式 - 服务器Nginx:一小时入门系列之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 服务器,包括监听端口、负载均衡、代理等功能。

#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

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

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服务:

[root@nginx-dev ~]# nginx -s reload

④ 利用13306端口连接MySQL:

在这里插入图片描述

4. TCP 负载均衡

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

猜你喜欢

转载自blog.csdn.net/qq_42764468/article/details/132511072