Nginx記錄1-基礎安裝相關

多線程架構

master進程主要用來管理worker進程冒嫡,具體包括如下4個主要功能:
(1)接收來自外界的信號。
(2)向各worker進程發(fā)送信號漱逸。
(3)監(jiān)控woker進程的運行狀態(tài)艺蝴。
(4)當woker進程退出后(異常情況下),會自動重新啟動新的woker進程际起。
woker進程主要用來處理網(wǎng)絡事件,各個woker進程之間是對等且相互獨立的吐葱,它們同等競爭來自客戶端的請求街望,一個請求只可能在一個woker進程中處理,woker進程個數(shù)一般設置為機器CPU核數(shù)唇撬。

參考文獻

官方文檔 | 在線文檔
安裝 | 應用實例 | 安全實例 | Nginx限流特技
反向代理-proxy_buffering
Aiod | ngx_http_core_module-日志常用參數(shù)

安裝過程

編譯選項:
--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

編譯參數(shù)
--with-stream 支持TCP代理以及負載均衡功能 官網(wǎng)

錯誤記錄:參考錯誤記錄
配置文件:參考配置文件

功能:安裝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規(guī)則

#!/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 核心規(guī)則是否生效
# #Nikto掃描工具生成惡意請求,包括針對已知易受攻擊的文件燕偶,跨站點腳本(XSS)和其他類型的攻擊的探測喝噪。
# #該工具還會報告?zhèn)鬟f給應用程序的請求,從而揭示應用程序中的潛在漏洞指么。
# 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
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末酝惧,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子伯诬,更是在濱河造成了極大的恐慌晚唇,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,490評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件盗似,死亡現(xiàn)場離奇詭異哩陕,居然都是意外死亡,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,581評論 3 395
  • 文/潘曉璐 我一進店門悍及,熙熙樓的掌柜王于貴愁眉苦臉地迎上來闽瓢,“玉大人,你說我怎么就攤上這事心赶】鬯希” “怎么了?”我有些...
    開封第一講書人閱讀 165,830評論 0 356
  • 文/不壞的土叔 我叫張陵园担,是天一觀的道長届谈。 經(jīng)常有香客問我,道長弯汰,這世上最難降的妖魔是什么艰山? 我笑而不...
    開封第一講書人閱讀 58,957評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮咏闪,結果婚禮上曙搬,老公的妹妹穿的比我還像新娘。我一直安慰自己鸽嫂,他們只是感情好纵装,可當我...
    茶點故事閱讀 67,974評論 6 393
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著据某,像睡著了一般橡娄。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上癣籽,一...
    開封第一講書人閱讀 51,754評論 1 307
  • 那天挽唉,我揣著相機與錄音缸濒,去河邊找鬼浇垦。 笑死斑鸦,一個胖子當著我的面吹牛斋否,可吹牛的內容都是我干的袖订。 我是一名探鬼主播痒玩,決...
    沈念sama閱讀 40,464評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼须误,長吁一口氣:“原來是場噩夢啊……” “哼畅哑!你這毒婦竟也來了俏险?” 一聲冷哼從身側響起严拒,我...
    開封第一講書人閱讀 39,357評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎竖独,沒想到半個月后糙俗,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,847評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡预鬓,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,995評論 3 338
  • 正文 我和宋清朗相戀三年巧骚,在試婚紗的時候發(fā)現(xiàn)自己被綠了赊颠。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,137評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡劈彪,死狀恐怖竣蹦,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情沧奴,我是刑警寧澤痘括,帶...
    沈念sama閱讀 35,819評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站滔吠,受9級特大地震影響纲菌,放射性物質發(fā)生泄漏。R本人自食惡果不足惜疮绷,卻給世界環(huán)境...
    茶點故事閱讀 41,482評論 3 331
  • 文/蒙蒙 一翰舌、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧冬骚,春花似錦椅贱、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,023評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至喜德,卻和暖如春山橄,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背舍悯。 一陣腳步聲響...
    開封第一講書人閱讀 33,149評論 1 272
  • 我被黑心中介騙來泰國打工航棱, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人贱呐。 一個月前我還...
    沈念sama閱讀 48,409評論 3 373
  • 正文 我出身青樓丧诺,卻偏偏與公主長得像入桂,于是被迫代替她去往敵國和親奄薇。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,086評論 2 355