编译安装Nginx及所需的依赖包以及常用插件

Nginx 全能优化版部署文档

1. 下载 Nginx 源码包

具体版本查看 Nginx 官方下载地址:http://nginx.org/download

wget http://nginx.org/download/nginx-1.17.10.tar.gz
tar xf nginx-1.17.10.tar.gz
cd nginx-1.17.10

2. 安装编译依赖包

根据你的系统执行对应命令:
CentOS 环境:
yum -y install gcc gcc-c++ make pcre-devel zlib-devel openssl-devel libtool
Ubuntu 环境:
apt-get install libpcre3 libpcre3-dev zlib1g-dev libssl-dev

3. 配置运行权限

创建禁止登录的系统用户运行 Nginx Worker:

useradd -r -s /sbin/nologin nginx

4. 优化版编译参数并编译安装(全能整合)

此配置整合了内容替换、HTTPS、四层转发、多核 CPU 及大硬盘 IO 优化:

./configure --prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_sub_module \
--with-http_realip_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gzip_static_module \
--with-threads \
--with-file-aio \
--with-stream \
--with-stream_realip_module \
--with-stream_ssl_module && make -j $(nproc) && make install

5. 高速编译与安装,上部分已包含

利用 nproc 自动获取 CPU 核心数进行并行编译,极大缩短时间:

make -j $(nproc) && make install

5.1 权限初始化 (关键步骤)

确保 nginx 用户对安装目录有完整的读写权限,防止启动或运行报错
chown -R nginx:nginx /usr/local/nginx

6. 环境变量设置

echo 'export PATH=$PATH:/usr/local/nginx/sbin' >> /etc/profile
source /etc/profile

检查并启动

/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx

最好用绝对路径的方式启动,后续需要升级nginx的话可以直接make upgrade;
当以普通用户执行sudo nginx的话会报找不到命令,是因为默认sudo会读取默认path安全路径,不会读取profile里的值,非root需要就需要sudo /usr/local/nginx/sbin/nginx来启动。

实战配置参考

字符串批量替换 (sub_filter):

location / {
    proxy_pass http://10.213.8.100:8070/maps/;
    proxy_set_header Accept-Encoding ""; 
    sub_filter 'http://webapi.amap.com' 'http://10.213.16.243:8081/webapi';
    sub_filter_types *;
    sub_filter_once off;
}

四层反向代理 (stream 模块):

stream {
    server {
        listen 12345 so_keepalive=on;
        proxy_pass 172.14.15.16:12345;
    }
}
posted @ 2020-10-09 17:45  开心burukku  阅读(2084)  评论(0)    收藏  举报