Skip to content

Nginx 编译安装

下载源码

bash
wget -q http://nginx.org/download/nginx-1.26.3.tar.gz
# 解压
tar xf nginx-1.26.3.tar.gz -C /usr/src/

安装依赖

bash
# CentOS/RHEL系统
dnf -y install gcc gcc-c++ make cmake pcre pcre-devel zlib zlib-devel openssl openssl-devel

# Ubuntu/Debian系统
apt install -y build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev

创建程序用户

bash
useradd -Ms /sbin/nologin www

预配置

bash
cd /usr/src/nginx-1.26.3
./configure \
--prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_realip_module \
--with-http_gzip_static_module \
--with-http_v2_module \
--with-pcre \
--with-stream \
--with-stream_ssl_module \
--with-threads \
--with-file-aio \
--user=www \
--group=www

编译安装

bash
make -j$(nproc) && make install

软连接

bash
ln -s /usr/local/nginx/sbin/* /usr/local/sbin/

Nginx 启动与停止

起动

bash
# 启动
nginx
# 开机自启
echo "nginx" >> /etc/rc.local && chmod +x /etc/rc.local

重启

bash
# 平滑重启命令
nginx -s reload
# 重启
nginx -s restart

停止

bash
# 停止
nginx -s stop

检测配置文件

bash
nginx -t

配置 nginx.service

systemd服务文件位置/usr/lib/systemd/system/nginx.service

ini
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -q
ExecStart=/usr/local/nginx/sbin/nginx -g 'daemon on; master_process on;'
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
TimeoutStopSec=5
KillSignal=SIGQUIT
Restart=on-failure
RestartSec=10s
PrivateTmp=true

[Install]
WantedBy=multi-user.target

管理命令

bash
systemctl daemon-reload                    # 修改.service文件后必须执行
systemctl start nginx
systemctl stop nginx
systemctl restart nginx                         # 强制重启(会中断连接)
systemctl reload nginx                          # 平滑重载配置(推荐)
systemctl status nginx -l                       # 查看详细状态
journalctl -u nginx -f                          # 实时查看日志

Nginx 启动脚本

shell
!/bin/bash
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
    start)
        if [ -f $PIDF ];then
            echo "nginx is running"
        else
            $PROG
            # echo "nginx is successfully"
        fi
    ;;
    stop)
        if [ -f $PIDF ];then
            killall -3 nginx
            # echo "nginx is stopping ..."
            rm -rf $PIDF
            # echo "nginx is stopped"
        else
            echo "nginx is stopped"
        fi
    ;;
    restart)
        $0 stop
        $0 start
    ;;
    reload)
        if [ -f $PIDF ];then
            kill -1 $(cat $PIDF)
        else
            echo "nginx is stopped"
    fi
    ;;
    status)
        if [ -f $PIDF ];then
            echo "nginx is running"
        else
            echo "nginx is stopped"
        fi
    ;;
    *)
    	echo "Usage: $0(start|stop|restart|reload|status)"
    	exit 1
    ;;
esac

Nginx 编译配置参数

Nginx核心路径配置参数

参数说明
--prefix=指向安装目录
--sbin-path=指向可执行程序文件(nginx)
--conf-path=指向主配置文件(nginx.conf)
--error-log-path=指向错误日志目录
--pid-path=指向pid文件(nginx.pid)
--lock-path=指向lock文件(nginx.lock)
--user=指定运行时的非特权用户
--group=指定运行时的非特权用户组
--builddir=指向编译目录

事件模块配置参数

参数说明
--with-rtsig_module启用实时信号模块
--with-select_module启用select轮询模块
--without-select_module禁用select模块
--with-poll_module启用poll轮询模块
--with-file-aio启用异步文件I/O支持

HTTP模块配置参数

参数说明
--with-http_ssl_module启用HTTPS支持
--with-http_realip_module启用真实IP模块
--with-http_addition_module启用响应内容追加模块
--with-http_xslt_module启用XML转换模块
--with-http_image_filter_module启用图片过滤模块
--with-http_geoip_module启用基于IP的地理位置模块
--with-http_sub_module启用响应内容替换模块
--with-http_dav_module启用WebDAV支持
--with-http_flv_module启用FLV视频流支持
--with-http_gzip_static_module启用预压缩静态文件支持
--with-http_random_index_module启用随机索引模块
--with-http_secure_link_module启用安全链接模块
--with-http_degradation_module启用降级模块
--with-http_stub_status_module启用状态监控模块

禁用HTTP模块参数

参数说明
--without-http_charset_module禁用字符集转换模块
--without-http_gzip_module禁用gzip压缩模块
--without-http_ssi_module禁用SSI模块
--without-http_userid_module禁用用户标识模块
--without-http_access_module禁用访问控制模块
--without-http_auth_basic_module禁用基本认证模块
--without-http_autoindex_module禁用自动索引模块
--without-http_geo_module禁用地理位置变量模块
--without-http_map_module禁用映射变量模块
--without-http_split_clients_module禁用客户端分割模块
--without-http_referer_module禁用Referer控制模块
--without-http_rewrite_module禁用重写模块
--without-http_proxy_module禁用代理模块
--without-http_fastcgi_module禁用FastCGI模块
--without-http_uwsgi_module禁用uWSGI模块
--without-http_scgi_module禁用SCGI模块
--without-http_memcached_module禁用Memcached模块
--without-http_limit_zone_module禁用连接限制模块
--without-http_limit_req_module禁用请求限制模块
--without-http_empty_gif_module禁用空GIF模块
--without-http_browser_module禁用浏览器识别模块
--without-http_upstream_ip_hash_module禁用IP哈希负载均衡模块

邮件代理模块配置参数

参数说明
--with-mail启用邮件代理模块
--with-mail_ssl_module启用邮件SSL模块
--without-mail_pop3_module禁用POP3协议支持
--without-mail_imap_module禁用IMAP协议支持
--without-mail_smtp_module禁用SMTP协议支持

其他模块配置参数

参数说明
--with-google_perftools_module启用Google性能工具模块
--with-cpp_test_module启用C++测试模块
--add-module=添加外部模块

编译器相关参数

参数说明
--with-cc=指定C编译器路径
--with-cpp=指定C预处理器路径
--with-cc-opt=设置C编译器参数
--with-ld-opt=设置链接器参数
--with-cpu-opt=指定CPU优化类型

第三方库配置参数

参数说明
--with-pcre启用PCRE库支持
--with-pcre=指定PCRE库路径
--with-pcre-opt=设置PCRE编译参数
--with-md5=指定MD5库路径
--with-md5-opt=设置MD5编译参数
--with-md5-asm使用MD5汇编优化
--with-sha1=指定SHA1库路径
--with-sha1-opt=设置SHA1编译参数
--with-sha1-asm使用SHA1汇编优化
--with-zlib=指定zlib库路径
--with-zlib-opt=设置zlib编译参数
--with-zlib-asm=使用zlib汇编优化
--with-openssl=指定OpenSSL路径
--with-openssl-opt=设置OpenSSL编译参数
--with-libatomic启用原子操作库支持
--with-libatomic=指定libatomic_ops路径

调试参数

参数说明
--with-debug启用调试日志

临时文件路径配置

参数说明
--http-log-path=设置访问日志路径
--http-client-body-temp-path=设置客户端请求体临时文件路径
--http-proxy-temp-path=设置代理临时文件路径
--http-fastcgi-temp-path=设置FastCGI临时文件路径
--http-uwsgi-temp-path=设置uWSGI临时文件路径
--http-scgi-temp-path=设置SCGI临时文件路径

功能禁用参数

参数说明
--without-http禁用HTTP服务器功能
--without-http-cache禁用HTTP缓存功能