Nginx记录1-基础安装相关

多线程架构

master进程主要用来管理worker进程,具体包括如下4个主要功能:
(1)接收来自外界的信号。
(2)向各worker进程发送信号。
(3)监控woker进程的运行状态。
(4)当woker进程退出后(异常情况下),会自动重新启动新的woker进程。
woker进程主要用来处理网络事件,各个woker进程之间是对等且相互独立的,它们同等竞争来自客户端的请求,一个请求只可能在一个woker进程中处理,woker进程个数一般设置为机器CPU核数。

参考文献

官方文档 | 在线文档
安装 | 应用实例 | 安全实例 | Nginx限流特技
反向代理-proxy_buffering
Aiod | ngx_http_core_module-日志常用参数

安装过程

编译选项:
--with-http_image_filter_module=dynamic --需要GD
--with-pcre-jit
--with-file-aio
--with-http_v2_module
--with-http_realip_module
--with-http_addition_module
--with-http_xslt_module=dynamic
--with-http_sub_module
--with-http_dav_module
--with-http_flv_module
--with-http_mp4_module
--with-http_gunzip_module
--with-http_gzip_static_module
--with-http_random_index_module
--with-http_secure_link_module
--with-http_slice_module
--需要pcre JIT
--with-http_geoip_module=dynamic --需要GeoIP GeoIP-devel

编译参数
--with-stream 支持TCP代理以及负载均衡功能 官网

错误记录:参考错误记录
配置文件:参考配置文件

功能:安装nginx、编译sticky模块、添加service服务

#!/bin/bash
nginxdir=/usr/local/nginx
nginxver=nginx-1.12.2

#funtion
nginx_init () {
curl -L https://raw.githubusercontent.com/mainiubaba/One/master/bash/nginx > /etc/init.d/nginx
if [ $? -eq '0' ];
then
 chmod +x /etc/init.d/nginx
else
 echo "add /etc/init.d/nginx filed."
 exit
fi
}

if [ -d ${nginxdir} ];
then
 echo "${nginxdir} directory exists"
else
 mkdir ${nginxdir}
fi
#yum
yum -y install cmake make gcc gcc-c++ libevent nss zlib zlib-devel openssl openssl-devel glibc glibc-devel compat-expat1 glibc.i686 procps procmail  ncurses-devel ncurses-libs ncurses-base ncurses  libuuid-devel pcre pcre-devel  libxslt libxml2 libxml2-devel gd-devel perl-ExtUtils-Embed perl-devel libxslt-devel
#wget tar
if [ -f ${nginxdir}/${nginxver}.tar.gz ];
then
 rm -r ${nginxdir}/${nginxver}.tar.gz
 rm -rf ${nginxdir}/${nginxver}
 wget -P $nginxdir http://nginx.org/download/${nginxver}.tar.gz
else
 wget -P $nginxdir http://nginx.org/download/${nginxver}.tar.gz
fi
wget -P $nginxdir https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/get/08a395c66e42.zip
#unzip
cd $nginxdir
tar -zxf nginx-1.12.2.tar.gz
unzip 08a395c66e42.zip
mv nginx-goodies-nginx-sticky-module-ng-08a395c66e42 nginx-sticky-module
#configure
cd ${nginxdir}/nginx-1.12.2
./configure \
--prefix=/usr/local/nginx/ \
--user=nginx \
--group=nginx \
--conf-path=/etc/nginx/nginx.conf \
--pid-path=/var/run/nginx.pid \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_stub_status_module \
--with-http_ssl_module \
--sbin-path=/usr/sbin/nginx \
--modules-path=/usr/lib64/nginx/modules \
--http-client-body-temp-path=/var/lib/nginx/tmp/client_body  \
--http-proxy-temp-path=/var/lib/nginx/tmp/proxy  \
--http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi  \
--http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi  \
--http-scgi-temp-path=/var/lib/nginx/tmp/scgi  \
--lock-path=/var/lock/subsys/nginx  \
--with-file-aio  \
--with-http_v2_module  \
--with-http_realip_module  \
--with-http_addition_module  \
--with-http_xslt_module=dynamic  \
--with-http_sub_module  \
--with-http_dav_module  \
--with-http_flv_module \
--with-http_mp4_module  \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module  \
--with-http_slice_module \
--add-module=${nginxdir}/nginx-sticky-module

make -j 4
make install

if [ $? -eq '0' ];
then
  nginx_init
fi

功能:安装nginx、编译sticky模块、添加service服务、编译modsecurity(2.9.2)模块、添加owasp规则

#!/bin/bash
nginxdir=/usr/local/nginx
nginxver=nginx-1.12.2
modsecurity_path=/etc/nginx

# funtion
nginx_init () {
    curl -L https://raw.githubusercontent.com/guaiguaile/One/master/bash/nginx > /etc/init.d/nginx
    if [ $? -eq '0' ];
    then
        chmod +x /etc/init.d/nginx
    else
        echo "add /etc/init.d/nginx filed."
        exit
    fi
}


if [ -d ${nginxdir} ];
then
    echo "${nginxdir} directory exists"
else
    mkdir ${nginxdir}
fi
# yum
yum -y install wget unzip cmake make gcc gcc-c++ libevent nss zlib zlib-devel openssl openssl-devel glibc glibc-devel compat-expat1 glibc.i686 procps procmail  ncurses-devel ncurses-libs ncurses-base ncurses  libuuid-devel pcre pcre-devel  libxslt libxml2 libxml2-devel gd-devel perl-ExtUtils-Embed perl-devel libxslt-devel
# wget tar
if [ -f ${nginxdir}/${nginxver}.tar.gz ];
then
    rm -r ${nginxdir}/${nginxver}.tar.gz
    rm -rf ${nginxdir}/${nginxver}
    wget -P $nginxdir http://nginx.org/download/${nginxver}.tar.gz
else
    wget -P $nginxdir http://nginx.org/download/${nginxver}.tar.gz
fi
# wget sticky
wget -P $nginxdir https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/get/08a395c66e42.zip
# unzip
cd $nginxdir
tar -zxf nginx-1.12.2.tar.gz
unzip 08a395c66e42.zip
mv nginx-goodies-nginx-sticky-module-ng-08a395c66e42 nginx-sticky-module
#  install modsecurity2.9.2
if [ $? -eq '0' ];
then
    echo "Install modsecurity2"
    yum install -y git gcc make automake autoconf libtool
    yum install -y pcre pcre-devel libxml2 libxml2-devel curl curl-devel httpd-devel
    if [ $? -eq '0' ];
    then
        cd ${nginxdir} && git clone https://github.com/SpiderLabs/ModSecurity.git  mod_security
    else
        exit
    fi
    cd mod_security && \
    git checkout v2.9.2 && \
    chmod 777 autogen.sh && \
    ./autogen.sh && \
    ./configure --enable-standalone-module && \
    make
fi



# configure
cd ${nginxdir}/nginx-1.12.2
./configure \
--prefix=/usr/local/nginx/ \
--user=nginx \
--group=nginx \
--conf-path=/etc/nginx/nginx.conf \
--pid-path=/var/run/nginx.pid \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_stub_status_module \
--with-http_ssl_module \
--sbin-path=/usr/sbin/nginx \
--modules-path=/usr/lib64/nginx/modules \
--http-client-body-temp-path=/var/lib/nginx/tmp/client_body  \
--http-proxy-temp-path=/var/lib/nginx/tmp/proxy  \
--http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi  \
--http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi  \
--http-scgi-temp-path=/var/lib/nginx/tmp/scgi  \
--lock-path=/var/lock/subsys/nginx  \
--with-file-aio  \
--with-http_v2_module  \
--with-http_realip_module  \
--with-http_addition_module  \
--with-http_xslt_module=dynamic  \
--with-http_sub_module  \
--with-http_dav_module  \
--with-http_flv_module \
--with-http_mp4_module  \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module  \
--with-http_slice_module \
--add-module=${nginxdir}/nginx-sticky-module \
--add-module=${nginxdir}/mod_security/nginx/modsecurity

make -j 4
make install

if [ $? -eq '0' ];
then
    nginx_init
    # create nobody user
    useradd -s /sbin/nologin -M nginx
    nginx -t
fi
# install modsecurity owasp
cd ${nginxdir} && git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
cp -r owasp-modsecurity-crs/ ${modsecurity_path}
cp ${modsecurity_path}/owasp-modsecurity-crs/crs-setup.conf.example ${modsecurity_path}/owasp-modsecurity-crs/crs-setup.conf
# deploy modsecurity
cp -r /usr/local/nginx/mod_security/{modsecurity.conf-recommended,unicode.mapping} ${modsecurity_path}
cp ${modsecurity_path}/modsecurity.conf-recommended ${modsecurity_path}/modsecurity.conf
sed -i 's/^SecRuleEngine DetectionOnly/SecRuleEngine on/' ${modsecurity_path}/modsecurity.conf
if [ $? -eq '0' ];
then
cat >> ${modsecurity_path}/modsecurity.conf << EOF
SecRule ARGS:testparam "@contains test" "id:1234,deny,status:403,msg:'Our test rule has triggered !!!197-test-196!!!'"
Include owasp-modsecurity-crs/crs-setup.conf
EOF
fi

echo '''
#############################
# #1. nginx开启ModSecurity
# ...
#         location / {
#             root   html;
#             # 开启ModSecurity
#             ModSecurityEnabled on;
#             # 选择ModSecurity配置文件
#             ModSecurityConfig /etc/nginx/modsecurity.conf;
#             index  index.html index.htm;
#         }
# ...

# #2. 测试
# #第一:重启nginx
# nginx -s reload &
# #第二:使用nikto测试owasp 核心规则是否生效
# #Nikto扫描工具生成恶意请求,包括针对已知易受攻击的文件,跨站点脚本(XSS)和其他类型的攻击的探测。
# #该工具还会报告传递给应用程序的请求,从而揭示应用程序中的潜在漏洞。
# git clone https://github.com/sullo/nikto
# cd nikto
# perl program/nikto.pl -h http://localhost
# #可以通过日志进行验证
# cat /var/log/modsec_audit.log
#############################
'''

Centos7 systemctl.service
# stat /usr/lib/systemd/system/nginx.service
文件:"/usr/lib/systemd/system/nginx.service"
权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)

[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID

[Install]
WantedBy=multi-user.target
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 162,825评论 4 377
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,887评论 2 308
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 112,425评论 0 255
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,801评论 0 224
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 53,252评论 3 299
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 41,089评论 1 226
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,216评论 2 322
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 31,005评论 0 215
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,747评论 1 250
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,883评论 2 255
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,354评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,694评论 3 265
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,406评论 3 246
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,222评论 0 9
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,996评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,242评论 2 287
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 36,017评论 2 281