1. HTTP反向代理和TCP反向代理
Nginx可以作為HTTP反向代理和TCP反向代理。
HTTP反向代理是指Nginx作為Web服務(wù)器的代理服務(wù)器,接收客戶端的HTTP請求,然后將請求轉(zhuǎn)發(fā)給后端的Web服務(wù)器,最后將Web服務(wù)器的響應(yīng)返回給客戶端。這種方式可以實現(xiàn)負(fù)載均衡、緩存、SSL終止等功能。
TCP反向代理是指Nginx作為TCP服務(wù)器的代理服務(wù)器,接收客戶端的TCP連接請求,然后將請求轉(zhuǎn)發(fā)給后端的TCP服務(wù)器,最后將TCP服務(wù)器的響應(yīng)返回給客戶端。這種方式可以實現(xiàn)負(fù)載均衡、高可用性、SSL終止等功能。TCP反向代理可以用于代理各種TCP協(xié)議,如SMTP、POP3、IMAP、FTP等。
2. http 塊和 stream 塊
在 Nginx 配置文件中,http 塊和 stream 塊是兩種不同的塊類型,它們分別用于處理 HTTP 和 TCP/UDP 流量。http塊用于配置 HTTP 服務(wù)器,包括監(jiān)聽端口、虛擬主機、反向代理、負(fù)載均衡、緩存等功能。stream 塊用于配置 TCP/UDP 服務(wù)器,包括監(jiān)聽端口、負(fù)載均衡、代理等功能。
#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負(fù)載均衡不能再配置在 /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 {
# 監(jiān)聽13306端口
listen 13306;
# 將流量代理到本地的3306端口(即MySQL數(shù)據(jù)庫的默認(rèn)端口)
proxy_pass localhost:3306;
}
}
MySQL使用TCP協(xié)議來實現(xiàn)客戶端和服務(wù)器之間的通信,而默認(rèn)的端口號是3306。 Nginx配置用于將來自13306端口的MySQL流量代理到本地的3306端口。這種配置通常用于將MySQL服務(wù)器隱藏在Nginx服務(wù)器后面,以提高安全性并允許更好的負(fù)載均衡。
③ 重啟Nginx服務(wù):
[root@nginx-dev ~]# nginx -s reload
④ 利用13306端口連接MySQL:
4. TCP 負(fù)載均衡
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
定義連接池里空閑連接的數(shù)量。keepalive_timeout
默認(rèn)60s。如果連接池里的連接空閑時間超過這個值,則連接關(guān)閉。文章來源:http://www.zghlxwxcb.cn/news/detail-674953.html
使用 keepalive 指令啟用從 NGINX Plus 到上游服務(wù)器的保持活動連接,定義在每個工作進(jìn)程的緩存中保留的與上游服務(wù)器的空閑保持活動連接的最大數(shù)量。當(dāng)超過此數(shù)字時,將關(guān)閉最近最少使用的連接。如果沒有 keepalives,您將增加更多的開銷,并且連接和臨時端口都效率低下。文章來源地址http://www.zghlxwxcb.cn/news/detail-674953.html
到了這里,關(guān)于分布式 - 服務(wù)器Nginx:一小時入門系列之TCP反向代理和負(fù)載均衡的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!