顯示具有 電腦-Linux-Server-Nginx 標籤的文章。 顯示所有文章
顯示具有 電腦-Linux-Server-Nginx 標籤的文章。 顯示所有文章

2016年11月18日 星期五

電腦 nginx增加ssl功能

增加ssl
nginx.conf
server {
    listen              443 ssl;
    server_name           www.example.com;
    ssl_certificate          www.example.com.crt;
    ssl_certificate_key  www.example.com.key;
    ssl_protocols          TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers             HIGH:!aNULL:!MD5;
    ...
}
測試ssl
openssl s_client -connect www.example.com:443
==
HTTPS server optimization

worker_processes auto;
nginx.conf
http {
    ssl_session_cache   shared:SSL:10m;
    ssl_session_timeout 10m;

    server {
        listen              443 ssl;
        server_name         www.example.com;
        keepalive_timeout   70;

        ssl_certificate         www.example.com.crt;
        ssl_certificate_key  www.example.com.key;
        ssl_protocols         TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers             HIGH:!aNULL:!MD5;
        ...

==
Name-based HTTPS servers
server {
    listen          443 ssl;
    server_name     www.example.com;
    ssl_certificate www.example.com.crt;
    ...
}

server {
    listen          443 ssl;
    server_name     www.example.org;
    ssl_certificate www.example.org.crt;
    ...
}

or
server {
    listen          192.168.1.1:443 ssl;
    server_name     www.example.com;
    ssl_certificate www.example.com.crt;
    ...
}

server {
    listen          192.168.1.2:443 ssl;
    server_name     www.example.org;
    ssl_certificate www.example.org.crt;
    ...
}
==
An SSL certificate with several names

ssl_certificate     common.crt;
ssl_certificate_key common.key;

server {
    listen          443 ssl;
    server_name     www.example.com;
    ...
}

server {
    listen          443 ssl;
    server_name     www.example.org;
    ...
}

Configuring HTTPS servers

2016年11月14日 星期一

電腦 nginx增加modsecurity功能


modsecurity 安裝
wget https://www.modsecurity.org/tarball/2.9.1/modsecurity-2.9.1.tar.gz
tar -zxvf modsecurity-2.9.1.tar.gz
cd modsecurity-2.9.1
./configure --enable-standalone-module --disable-mlogc
make

nginx 安裝
wget http://nginx.org/download/nginx-1.10.2.tar.gz
tar -zxvf nginx-1.10.2.tar.gz
cd nginx-1.10.2

./configure  --add-module=../modsecurity-2.9.1/nginx/modsecurity/


modsecurity設定
cp ../modsecurity-2.9.1/modsecurity.conf-recommended /etc/nginx/modsecurity.conf
cp ../modsecurity-2.9.1/unicode.mapping /etc/nginx/

vim nginx.conf
增加
ModSecurityEnabled on;
ModSecurityConfig modsecurity.conf;

測試
nginx -t

啟用nginx後,檢查error.log



增加 OWASP ModSecurity Core Rule Set (CRS) 規則
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
or
wget https://github.com/SpiderLabs/owasp-modsecurity-crs/archive/v3.0.0.tar.gz

tar zxvpf v3.0.0.tar.gz
mv owasp-modsecurity-crs-3.0.0/ /etc/nginx/owasp-modsecurity-crs
cp crs-setup.conf.example crs-setup.conf
cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf

新增 modsec_includes.conf

include modsecurity.conf
include owasp-modsecurity-crs/crs-setup.conf

如果需要其它規則也可以加入
include owasp-modsecurity-crs/rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf


修改nginx.conf

ModSecurityConfig modsecurity.conf;
更改為
ModSecurityConfig modsec_includes.conf;

重新載入nginx


測試
Cross-site Scripting test request

http://192.168.1.106/search.aspx?txtSearch=%3Cscript%3Ealert%28%27foo%27%29%3C%2Fscript%3E


電腦 nginx的ngx_cache_purge功能

下載ngx_cache_purge
   wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz
   tar zxvf ./ngx_cache_purge-2.3.tar.gz

重新編譯,增加--add-module=../ngx_cache_purge-2.3 參數
make clean
./configure \

--add-module=../ngx_cache_purge-2.3

make
make install


設定nginx.conf
vi /etc/nginx/nginx.conf

proxy_cache_path   /tmp/cache   keys_zone=tmpcache:10m;

location / {
proxy_pass http://127.0.0.1:8000;
proxy_cache tmpcache;
proxy_cache_key $uri$is_args$args;
proxy_cache_purge PURGE from 127.0.0.1;
}

CentOS-Nginx安裝-原始碼

1.Centos 7最小安裝
2.依據yum安裝的編譯設定
3.安裝相關rpm
   yum groupinstall "Development Tools"
4.下載 nginx
    wget http://nginx.org/download/nginx-1.10.2.tar.gz
    tar zxvpf nginx-1.10.2.tar.gz
5.安裝 nginScript(njs) dynamic module
    curl -L -O http://hg.nginx.org/njs/archive/tip.tar.gz
    wget http://hg.nginx.org/njs/archive/tip.tar.gz
    tar zxvf ./tip.tar.gz
    mv ./njs-91543c86f412 ./nginx-1.9.13
    cd ./nginx-1.9.13/njs-91543c86f412
     ./configure
    make
 6. 安裝 module
    yum install pcre-devel openssl-devel libxslt-devel gd-devel perl-ExtUtils-Embed http-devel GeoIP-devel
 7.編譯
 ./configure \
 --prefix=/etc/nginx \
 --sbin-path=/usr/sbin/nginx \
 --modules-path=/usr/lib64/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_image_filter_module=dynamic \
 --with-http_geoip_module=dynamic \
 --with-http_perl_module=dynamic \
  --with-threads \
 --with-stream \
 --with-stream_ssl_module \
 --with-http_slice_module \
 --with-mail \
 --with-mail_ssl_module \
 --with-file-aio \
 --with-ipv6 \
 --with-http_v2_module \
 --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic' \
 --with-ld-opt=-Wl,-E

 make
 make install

#NJS單獨可以編譯,未解決
--add-dynamic-module=njs-91543c86f412/nginx \

nginxのコンパイルメモ

電腦 nginx的http_slice_module功能


確認nginx 是否有包含aio功能
nginx -v
是否有 --with-http_slice_module
如果沒有,需重新編譯

設定nginx.conf
vi /etc/nginx/nginx.conf

    proxy_cache_path /tmp/nginx/cache levels=1:2 keys_zone=cache:100m;
 
        location / {
            slice 1m;
            proxy_cache cache;
            proxy_cache_key $uri$is_args$args$slice_range;
            proxy_set_header Range $slice_range;
            proxy_cache_valid 200 206 1h;
            #proxy_set_header Range $http_range;
            proxy_pass http://127.0.0.1:8080;

        }


嚐鮮:Nginx-1.9.8推出的切片模塊
首先,如果不帶Range 請求,後端大文件在本地cache 時,會按照配置的slice 大小進行切片存儲。

其次,如果帶Range 請求,則Nginx 會用合適的Range 大小(以slice 為邊界)去後端請求,這個大小跟客戶端請求的Range 可能不一樣,並將以slice 為大小的切片存儲到本地,並以正確的206響應客戶端。

注意上面所說的,Nginx 到後端的Range 並不一定等於客戶端請求的Range,因為無論你請求的Range 如何,Nginx 到後端總是以slice 大小為邊界,將客戶端請求分割成若干個子請求到後端,假設配置的slice 大小為1M,即1024字節,那麼如果客戶端請求Range 為0-1023範圍以內任何數字,均會落到第一個切片上,如果請求的Range 橫跨了幾個slice 大小,則nginx會向後端發起多個子請求,將這幾個slice 緩存下來。而對客戶端,均以客戶端請求的Range 為準。如果一個請求中,有一部分文件之前沒有緩存下來,則Nginx 只會去向後端請求缺失的那些切片。

由於這個模塊是建立在子請求的基礎上,會有這麼一個潛在問題:當文件很大或者slice 很小的時候,會按照slice 大小分成很多個子請求,而這些個子請求並不會馬上釋放自己的資源,可能會導致文件描述符耗盡等情況。

需要注意的點:

1.該模塊用在proxy_cache 大文件的場景,將大文件切片緩存
2.編譯時對configure 加上--with-http_slice_module 參數
3.$slice_range 一定要加到proxy_cache_key 中,並使用proxy_set_header 將其作為Range 頭傳遞給後端
4.要根據文件大小合理設置slice 大小

Nginx:作為緩存,支持Range回源

電腦 nginx使用 aio 功能

確認nginx 是否有包含aio功能
nginx -v
是否有 --with-file-aio
如果沒有,需重新編譯

設定nginx.conf
vi /etc/nginx/nginx.conf

location / {
aio on;
directio 1;
output_buffers 1 128k;
}

Nginx 使用Linux-native aio 需要Linux 內核支持

Nginx 性能優異在於善於利用操作系統內核的各種特性,比如aio/epoll/sendfile (Linux), kqueue (FreeBSD) 等。對於使用VPS 做圖片站的站長來說,使用nginx 的aio 特性會大大提高性能,圖片站的特點是大量的讀io 操作,nginx aio 不用等待每次io 的結果有助於並發處理大量io 和提高nginx 處理效率。

2016年10月4日 星期二

CentOS-Nginx安裝-yum

CentOS-Nginx安裝-yum

vim /etc/yum.repos.d/nginx.repo

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

yum install nginx
systemctl start nginx.service

版本確認
nginx -v

編譯檔確認
nginx -V
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/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_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_perl_module=dynamic --add-dynamic-module=njs-91543c86f412/nginx --with-threads --with-stream --with-stream_ssl_module --with-http_slice_module --with-mail --with-mail_ssl_module --with-file-aio --with-ipv6 --with-http_v2_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic' --with-ld-opt=-Wl,-E