Nginx 源码安装
Nginx 源码安装
Nginx官网:https://nginx.org
参考:
Nginx配置常用参数梳理
https://www.jb51.net/server/285538k8k.htm
nginx配置参数详解
https://blog.csdn.net/u013286192/article/details/136418472
Nginx 配置详解
https://www.runoob.com/w3cnote/nginx-setup-intro.html
查看nginx开启的模块功能
更新和安装所需的库
sudo apt-get update sudo apt-get install build-essential libpcre3 libpcre3-dev zlib1g-dev openssl libssl-dev
下载官网源码
wget https://nginx.org/download/nginx-1.26.0.tar.gz
解压
tar -xzvf nginx-1.26.0.tar.gz cd nginx-1.26.0
配置
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module sudo make sudo make install
创建全局链接
sudo ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
常用命令查看:
nginx -h
查看版本号:nginx -v
查看编译参数:nginx -V
查看当前工作进程:nginx -s process
停止服务:nginx -s stop
安全停止服务:nginx -s quit
检查配置文件语法错误:nginx -t
重新加载配置 (不停止服务):nginx -s reload
常用模块
./configure --prefix=/usr/local/nginx \ --with-compat \ --with-debug \ --with-pcre-jit \ --with-http_ssl_module \ --with-http_stub_status_module \ --with-http_realip_module \ --with-http_auth_request_module \ --with-http_v2_module \ --with-http_dav_module \ --with-http_slice_module \ --with-threads \ --with-http_addition_module \ --with-http_gunzip_module \ --with-http_gzip_static_module \ --with-http_sub_module
根据 ip 统计请求信息
awk '{print \$1\}' nginx.access.log | sort | uniq -c | sort -n
prefix代表nginx将要安装的位置;--add-module=path/xx_module
--add-module代表要安装的第三方模块,需要先下载,其中path代表模块的存放的目录,xx_module代表模块的名字
--add-module第一次也可以不用安装,后续需要的时候可以再下载编译
#下面两种方式都是可以的:
1、第一种直接添加第三方模块
./configure --prefix=/usr/local/nginx/ --add-module=/opt/echo-nginx-module
2、第二种暂时忽略第三方模块,后面再单独安装
./configure --prefix=/usr/local/nginx/
编译并安装
sudo make && sudo make install
nginx增加模块不停止nginx
查看nginx的版本和模块 nginx-V
sudo nginx -V
进入源码目录并配置
./configure --with-compat --with-debug --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_sub_module --with-http_stub_status_module
只用编译 make 不要安装
sudo make
注意:不要 make install
进入源码目录下面的 objs 目录,将编译好的 nginx 可执行文件拷贝到安装目录,比如
cd /home/xxx/nginx-1.26.0/objs sudo cp nginx /usr/local/nginx/sbin
重启
sudo nginx -s stop && sudo nginx
Nginx 限流配置
http { # 定义限制 limit_req_zone $binary_remote_addr zone=req:10m rate=2r/m; server { # 引用上面 req 配置 # limit_req zone=req burst=3 nodelay; limit_req zone=req burst=10; } }
配置 imei,待验证
#同一个请求同一个imei 一分钟限制请求1次 limit_req_zone $remote_addr*$arg_imei zone=zone_imei:10m rate=1r/m; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';
apt 安装的 nginx 1.8.0 默认加载模块
nginx version: nginx/1.18.0 (Ubuntu) built with OpenSSL 3.0.2 15 Mar 2022 TLS SNI support enabled configure arguments: ---------------------------------------------------------------------------------------------------- --with-cc-opt='-g -O2 -ffile-prefix-map=/build/nginx-zctdR4/nginx-1.18.0=. -flto=auto -ffat-lto-objects -flto=auto -ffat-lto-objects -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -flto=auto -ffat-lto-objects -flto=auto -Wl,-z,relro -Wl,-z,now -fPIC' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构