若是nginx被***或者訪問量忽然變大,nginx會由於負載變高或者內存不夠用致使服務器宕機,最終致使站點沒法訪問。解決方法是利用淘寶開發的模塊nginx-http-sysguard,主要用於當負載和內存達到必定的閥值之時,會執行相應的動做,好比直接返回503,504或者其餘的.一直等到內存或者負載回到閥值的範圍內,站點恢復可用。簡單的說,這個模塊是讓nginx有個緩衝時間。html
我使用的操做系統是Ubuntu12.0.4,下面的操做都以該系統爲基礎。nginx
首先到官網上下載nginx1.12.1.tar.gz穩定版,咱們使用源碼包安裝方式git
http://nginx.org/en/download.html
github
在虛擬機的/opt目錄下新建一個文件夾nginx1.12.1,將下載的源碼包放到該文件夾內,在該文件夾中解壓源碼包,進入解壓後的文件夾,進行配置瀏覽器
cd /opt/nginx-1.12.1/ tar -zxvf nginx1.12.1.tar.gz cd nginx-1.12.1/ ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --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_stub_status_module --with-http_auth_request_module --with-http_xslt_module=dynamic --with-http_p_w_picpath_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_perl_module=dynamic --with-threads --with-stream --with-stream_ssl_module --with-stream_geoip_module=dynamic --with-http_slice_module --with-mail --with-mail_ssl_module --with-file-aio --with-ipv6 --with-http_v2_module --with-cc-opt='-g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Wformat-security -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,--as-needed'
配置的參數是直接從另外一臺機器上copy過來的,這臺虛擬機第一次裝nginx,配置的時候報不少錯,缺乏不少庫bash
報錯:adding module in debian/extra/njs-0.1.0/nginx
./configure: error: no debian/extra/njs-0.1.0/nginx/config was found服務器
解決:將配置參數中的「--add-dynamic-module=debian/extra/njs-0.1.0/nginx」刪掉
dom
報錯:./configure: error: SSL modules require the OpenSSL library.
ide
解決:apt-get install openssl libssl-dev
測試
報錯:./configure: error: the HTTP XSLT module requires the libxml2/libxslt libraries.
解決:apt-get install libxml2-dev libxslt-dev
報錯:/configure: error: the HTTP p_w_picpath filter module requires the GD library.
解決:apt-get install libgd2-xpm libgd2-xpm-dev
報錯:./configure: error: the GeoIP module requires the GeoIP library.
解決:apt-get install libgeoip-dev
我遇到的問題就這些,而後這些庫都安裝完後,從新配置,而後編譯make
編譯過程當中遇到的問題
報錯:/usr/bin/ld: cannot find -lperl
解決:apt-get install libperl-dev
而後編譯經過,而後安裝make install,nginx1.12.1安裝完了
接下來添加nginx-http-sysguard模塊,從官網上下載這個模塊
https://github.com/alibaba/nginx-http-sysguard/archive/master.zip
解壓到/opt/nginx-1.12.1/中,而後打補丁
cd /opt/nginx-1.12.1 patch -p1 < ../nginx-http-sysguard-master/nginx_sysguard_1.3.9.patch
用nginx -V查看安裝命令,而後再次配置一次,在以前的配置命令後面加上 --add-module=/opt/nginx-1.12.1/nginx-http-sysguard-master
而後安裝編譯make && make install
nginx安裝好後測試一下
在nginx.conf中server段中添加
sysguard on; sysguard_load load=0.01 action=/loadlimit; sysguard_mem swapratio=20% action=/swaplimit; location /loadlimit { return 404; } location /swaplimit { return 503; }
並在server中配置服務器的網站根目錄路徑root /Disk/p_w_picpath;
定義默認請求的路徑
啓動/usr/sbin/nginx -c /etc/nginx/nginx.conf
啓動時報錯:[emerg] getpwnam("nginx") failed
解決:useradd nginx 新建一個nginx用戶
啓動成功,在/Disk/p_w_picpath下放一個圖片,在瀏覽器上訪問這個圖片能夠訪問到
uptime查看cpu負載狀況
當load average最左邊的值高於0.01時,圖片就訪問不到了。而且頁面上會給出提示。
當cpu平均負載降下來的時候就又能夠訪問到了