nginx安装和负载均衡
1. nginx安装
(1)安装依赖项:
yum -y install gcc gcc-c++ make libtool zlib zlib-devel openssl openssl-devel pcre pcre-devel
(2)下载Nginx源代码:
http://nginx.org/en/download.html
https://nginx.org/download/
上传到/usr/local
解压缩
tar -zxvf nginx-1.24.0.tar.gz
(3)安装
cd /usr/local/nginx-1.24.0/
./configure --prefix=/usr/local/nginx
make && make install
(4)启动
/usr/local/nginx/sbin/nginx
# 查看版本
/usr/local/nginx/sbin/nginx -V
参考:
https://blog.csdn.net/Da_zhenzai/article/details/140456495
https://www.runoob.com/w3cnote/nginx-install-and-config.html
https://blog.csdn.net/weixin_64157795/article/details/142146103
2. 负载均衡
2.1 /usr/local/nginx/conf/my_server.conf
仿照/usr/local/nginx/conf/nginx.conf
新建 my_server.conf
#user nobody;
worker_processes 1;#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;#pid logs/nginx.pid;events {worker_connections 1024;
}http {include 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 logs/access.log main;sendfile on;#tcp_nopush on;#keepalive_timeout 0;keepalive_timeout 65;#gzip on;upstream MyServerTest {server 10.0.0.1:8111; server 10.0.0.2:8111;} server {listen 8080;server_name 10.0.0.1;#charset koi8-r;#access_log logs/host.access.log main;location / {proxy_pass http://MyServerTest;}
监听 10.0.0.1 的 8080 端口,转发到 10.0.0.1:8111,10.0.0.2:8111
2.2 重新加载 Nginx 配置
# 重新加载 nginx 配置 xxx.conf
/usr/local/nginx/sbin/nginx -s reload
# 重启 加载 新的 my_server.conf
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/my_server.conf
查看进程
ps -aux | grep nginx
检查配置
/usr/local/nginx/sbin/nginx -t
2.3 client_max_body_size
请求报错:
<Response [413]>
2024/11/29 10:08:57 [error] 15103#0: *62 client intended to send too large body: 2986226 bytes, client: 101.0.0.5, server: 10.0.0.1, request: “POST /xxxx/test/1.0 HTTP/1.1”, host: “10.0.0.1:8080”
默认情况下,Nginx 限制客户端请求体(包括文件上传)为1m(1兆字节)。
可以通过在Nginx配置文件中设置client_max_body_size来修改此限制。
http {...client_max_body_size 10M;...
}
如果只想对特定的server或location进行设置
# 只对特定的server进行设置
server {...client_max_body_size 10M;...
}
# 只对特定的location进行设置
location /upload {client_max_body_size 10M;
}
重新加载 Nginx 配置
/usr/local/nginx/sbin/nginx -s reload
3. 转发
server {listen 8000; #监听 8000 端口server_name 10.0.0.1; # 匹配请求的地址location /api/v1 {proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_pass http://10.0.0.1:8801;# 转发到目标服务}location / {proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_pass http://10.0.0.1:8802; # 转发到目标服务}
}
功能:
(1)listen 8000
Nginx 监听服务器 10.0.0.1 的 8000 端口,所有向该端口发起的 HTTP 请求将由 Nginx 处理。
(2)路径 /api/v1 的转发:
当访问 http://10.0.0.1:8000/api/v1 时,请求会被转发到 http://10.0.0.1:8801/api/v1
(3)其他转发
除 /api/v1 外的所有请求都会转发到 http://10.0.0.1:8802,且路径保持一致。
头部信息的设置:
proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
保留客户端的原始请求头信息:
$host: 请求的主机名。
$remote_addr: 客户端的真实IP 地址
$proxy_add_x_forwarded_for: 客户端经过的代理链
示例:
- 请求: GET http://10.0.0.1:8000/api/v1
转发: GET http://10.0.0.1:8801/api/v1 - 请求: GET http://10.0.0.1:8000/api/test
转发: GET http://10.0.0.1:8802/api/test - 请求: GET http://10.0.0.1:8000/some/other/path
转发: GET http://10.0.0.1:8802/some/other/path
路径匹配的优先级
Nginx 按配置文件中 location 的顺序进行路径匹配,具体逻辑如下:
(1)优先匹配 /api/v1。
(2)若路径不匹配 /api/v1,则转到默认的 /配置
检查 Nginx的访问日志或错误日志以确认请求是否正常转发:
tail -f /usr/local/nginx/access.log
tail -f /usr/local/nginx/error.log