nginx安装和负载均衡

1. nginx安装

(1)安装依赖项:

bash 复制代码
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

解压缩

bash 复制代码
tar -zxvf nginx-1.24.0.tar.gz

(3)安装

bash 复制代码
cd /usr/local/nginx-1.24.0/
./configure --prefix=/usr/local/nginx
make && make install

(4)启动

bash 复制代码
/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

bash 复制代码
#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 配置

bash 复制代码
# 重新加载 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

查看进程

bash 复制代码
ps -aux | grep nginx

检查配置

bash 复制代码
/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来修改此限制。

bash 复制代码
http {
    ...
    client_max_body_size 10M;
    ...
}

如果只想对特定的server或location进行设置

bash 复制代码
# 只对特定的server进行设置
server {
    ...
    client_max_body_size 10M;
    ...
}
# 只对特定的location进行设置
location /upload {
    client_max_body_size 10M;
}

重新加载 Nginx 配置

bash 复制代码
/usr/local/nginx/sbin/nginx -s reload

3. 转发

bash 复制代码
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,且路径保持一致。

头部信息的设置:

bash 复制代码
        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: 客户端经过的代理链

示例:

路径匹配的优先级

Nginx 按配置文件中 location 的顺序进行路径匹配,具体逻辑如下:

(1)优先匹配 /api/v1。

(2)若路径不匹配 /api/v1,则转到默认的 /配置

检查 Nginx的访问日志或错误日志以确认请求是否正常转发:

bash 复制代码
tail -f /usr/local/nginx/access.log
tail -f /usr/local/nginx/error.log
相关推荐
IT小哥哥呀1 小时前
Nginx高可用配置实战:负载均衡 + 健康检查 + 动态扩展
运维·nginx·负载均衡·devops·日志分析·openresty·动态扩展
刘某的Cloud1 小时前
ceph osd down排查
linux·运维·ceph·系统·osd
喜欢你,还有大家5 小时前
Docker-仓库-镜像制作
运维·docker·容器
安审若无6 小时前
图数据库neoj4安装部署使用
linux·运维·数据库
做运维的阿瑞7 小时前
CentOS DNS故障排查完整解决方案:从症状到根因的系统化诊断
linux·运维·centos
深圳市恒讯科技8 小时前
英国服务器Windows系统远程桌面安装与优化
运维·服务器·windows
itachi-uchiha8 小时前
head和tail命令使用
linux·运维·服务器
violet-lz8 小时前
Socket编程实战:从基础API到多线程服务器
运维·服务器
初学者_xuan8 小时前
零基础新手小白快速了解掌握服务集群与自动化运维(十六)集群部署模块——LVS-DR&TUN模式配置
运维·自动化·lvs
toooooop88 小时前
Nginx 反向代理 HTTPS CDN 配置检查清单(避坑版)
运维·nginx·https·cdn