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
相关推荐
乘云数字DATABUFF4 天前
5分钟部署开源APM Databuff:OpenTelemetry全链路追踪入门实战
运维·后端
荣--6 天前
一键部署不是为了省时间 —— 它是把"买来的 PaaS"变成"自己的平台"的拐点
运维·zabbix·工程化·一键部署·平台化·边界设计
江华森6 天前
动手实战学 Docker — 从零到集群编排完全指南
运维
Avan_菜菜6 天前
FRP 内网穿透完整实战:从 HTTP 映射到 HTTPS 自签代理
运维·nginx·https
SelectDB7 天前
Litefuse 开源并推出单进程轻量模式,25 秒就能跑起来的 Agent 可观测与评估平台
运维·后端·自动化运维
XIAOHEZIcode9 天前
Linux系统鼠标偏移常见原因以及修复方案
linux·运维·游戏
用户0328472220709 天前
如何搭建本地yum源(上)
运维
ping某10 天前
为什么 Nginx 明明监听了 80,转发后端时却用了 4xxxx 端口?
后端·nginx
大树8812 天前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai
摇滚侠12 天前
Linux CentOS7 rpm 安装 MySQL 5.7
linux·运维·mysql