Linux nginx 安裝

一、
$ sudo yum install gd-devel pcre-devel libcurl-devel openssl-devel.x86_64javascript

二、
# 下載Tenginecss

mkdir /home/tools/nginxhtml

cd /home/tools/nginx
wget http://tengine.taobao.org/download/tengine-2.2.0.tar.gz
tar -zxvf tengine-2.2.0.tar.gz
cd tengine-2.2.0
wget https://github.com/3078825/ngx_image_thumb/archive/master.zip
unzip master.zip
#$ ./configure --add-module=./nginx-image-master
./configure --prefix=/usr/local/nginx  --with-http_image_filter_module --with-http_concat_module  --add-module=./ngx_image_thumb-master/
make
sudo make install 前端

三、配置方法java

打開 nginx.conf
vim /usr/local/nginx/nginx.conf 

location / {
   root html;
   #添加如下配置
   image on;
   image_output on;
}
或者指定目錄開啓
location /upload {
   root html; 
   image on;
   image_output on;
}node

其餘參數說明:linux

image on/off 是否開啓縮略圖功能,默認關閉
image_backend on/off 是否開啓鏡像服務,當開啓該功能時,請求目錄不存在的圖片(判斷原圖),將自動從鏡像服務器地址下載原圖
image_backend_server 鏡像服務器地址
image_output on/off 是否不生成圖片而直接處理後輸出 默認off
image_jpeg_quality 75 生成JPEG圖片的質量 默認值75
image_water on/off 是否開啓水印功能
image_water_type 0/1 水印類型 0:圖片水印 1:文字水印
image_water_min 300 300 圖片寬度 300 高度 300 的狀況才添加水印
image_water_pos 0-9 水印位置 默認值9 0爲隨機位置,1爲頂端居左,2爲頂端居中,3爲頂端居右,4爲中部居左,5爲中部居中,6爲中部居右,7爲底端居左,8爲底端居中,9爲底端居右
image_water_file 水印文件(jpg/png/gif),絕對路徑或者相對路徑的水印圖片
image_water_transparent 水印透明度,默認20
image_water_text 水印文字 "Power By Vampire"
image_water_font_size 水印大小 默認 5
image_water_font 文字水印字體文件路徑
image_water_color 水印文字顏色,默認 #000000
 
 調用說明 nginx


這裏假設你的nginx 訪問地址爲 http://127.0.0.1/
並在nginx網站根目錄存在一個 test.jpg 的圖片
經過訪問
http://127.0.0.1/test.jpg!c300x200.jpg 將會 生成/輸出 test.jpg 300x200 的縮略圖
其中 c 是生成圖片縮略圖的參數, 300 是生成縮略圖的 寬度 200 是生成縮略圖的 高度
一共能夠生成四種不一樣類型的縮略圖。
支持 jpeg / png / gif (Gif生成後變成靜態圖片)
C 參數按請求寬高比例從圖片高度 10% 處開始截取圖片,而後縮放/放大到指定尺寸( 圖片縮略圖大小等於請求的寬高 )
M 參數按請求寬高比例居中截圖圖片,而後縮放/放大到指定尺寸( 圖片縮略圖大小等於請求的寬高 )
T 參數按請求寬高比例按比例縮放/放大到指定尺寸( 圖片縮略圖大小可能小於請求的寬高 )
W 參數按請求寬高比例縮放/放大到指定尺寸,空白處填充白色背景顏色( 圖片縮略圖大小等於請求的寬高 )c++


調用舉例git

http://127.0.0.1/test.jpg!c300x300.jpg
http://127.0.0.1/test.jpg!t300x300.jpg
http://127.0.0.1/test.jpg!m300x300.jpg
http://127.0.0.1/test.jpg!w300x300.jpg
http://127.0.0.1/test.c300x300.jpg
http://127.0.0.1/test.t300x300.jpg
http://127.0.0.1/test.m300x300.jpg
http://127.0.0.1/test.w300x300.jpg


PS: HttpImageFilterModule模塊須要依賴gd-devel的支持,可使用yum或apt-get方便地安裝,若是未安裝回報「/configure: error: the HTTP image filter module requires the GD library.」錯誤

yum install gd-devel

apt-get install libgd2-xpm libgd2-xpm-dev

 

四、常見錯誤處理:        
一、運行nginx出現錯誤:error while loading shared libraries:libpcre.so.1:cannot open shared object file: no such file or directory
   從錯誤看出是缺乏lib文件致使,進一步查看下
   # cd /usr/sbin/nginx 
   libpcre.so.1=> not found,這個文件 not found只須要進入/lib64目錄中手動連接下
   # cd /lib64
   # ln -s libpcre.so.0.0.1 libpcre.so.1
二、關閉nginx服務器:
   # /usr/local/nginx/sbin/nginx -s stop    

三、在./configure出現-bash: make: command not found
   # yum -y install gcc automake autoconf libtool make

四、unrecognized option `-DHAVE_CONFIG_H'
   gcc-c++未安裝,安裝便可,而後從新編譯configure make make install
   # rpm -qa | grep gcc-c++
   # yum install -y gcc-c++

五、undefined reference to `rc4_md5_enc'
六、./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl= option
apt-get install libssl-dev
要安裝 openssl-devel.x86_64包
yum install openssl-devel.x86_64

五、啓動、中止腳本

nginx.sh 放在 /usr/local/nginx目錄下

#!/bin/sh
#
CWD='pwd'
case "$1" in   
        'start')   
                sbin/nginx   
    ;;   
        'stop')   
                kill -2 `cat nginx.pid`
                        ;;   
        'restart')   
                cd "$CMD"  
                $0 stop   
                $0 start   
        ;;
    'reload')   
                sbin/nginx -s reload
    ;;   
        *)   
        echo $"Usage: $0 {start|stop|restart|reload}"  
        exit 1  
esac   
exit 0    

六、設置開啓啓動

    # echo "/usr/local/nginx/nginx.sh start">>/etc/rc.local
七、設定操做系統最大打開文件數
    # ulimit -SHn 102400

    #ulimit -u 65535
    # echo "ulimit -SHn 102400" >>/etc/rc.local 

    # echo "ulimit -u 65535" >>/etc/rc.local 

八、調優最大TCP鏈接數

在 /etc/sysctl.conf中添加配置

vim /etc/sysctl.conf

# Increase system IP port limits to allow for more connections

# 調高系統的 IP 以及端口數據限制,從能夠接受更多的鏈接

net.ipv4.ip_local_port_range = 2000 65000

net.ipv4.tcp_window_scaling = 1

# number of packets to keep in backlog before the kernel starts dropping them

# 設置協議棧能夠緩存的報文數閥值,超過閥值的報文將被內核丟棄

net.ipv4.tcp_max_syn_backlog = 3240000

# increase socket listen backlog

# 調高 socket 偵聽數閥值

net.core.somaxconn = 3240000

net.ipv4.tcp_max_tw_buckets = 1440000

# Increase TCP buffer sizes

# 調大 TCP 存儲大小

net.core.rmem_default = 8388608

net.core.rmem_max = 16777216

net.core.wmem_max = 16777216

net.ipv4.tcp_rmem = 4096 87380 16777216

net.ipv4.tcp_wmem = 4096 65536 16777216

net.ipv4.tcp_congestion_control = cubic

 

每次修訂配置以後都須要執行如下命令使之生效.

sysctl -p /etc/sysctl.conf

 

九、nginx.conf配置示例

#nx運行的用戶及組

user  root;

#工做進程數,通常是配置爲小於CPU數

worker_processes  4;

worker_cpu_affinity 0001 0010 0100 1000;

 

#[ debug | info | notice | warn | error | crit ]

error_log  logs/error.log error;

#進程文件

pid        /usr/local/nginx/nginx.pid;

#一個nginx進程打開的最多文件描述符數目,理論值應該是最多打開文件數(ulimit -n)與nginx進程數相除,可是nginx分配請求並非那麼均勻,因此最好與ulimit -n的值保持一致。

worker_rlimit_nofile 102400;

 

events {

    #Linux下性能最好的event

    #epoll是多路複用IO(I/O Multiplexing)中的一種方式,可是僅用於linux2.6以上內核,能夠大大提升nginx的性能

    use epoll;

    #每一個進程容許最大的鏈接數

    worker_connections  10240;

}

 

#設定http服務器

http {

    #隱藏返回給客戶端的 server版本號

    server_tokens off;

    #文件擴展名與文件類型映射表

    include       mime.types;

    #默認文件類型

    default_type  application/octet-stream;

    

    #默認編碼

    charset  utf-8;

    

    #服務器名字的hash表大小

    server_names_hash_bucket_size 512;

    #上傳文件大小限制

    client_header_buffer_size 2k;

    #設定請求緩

    large_client_header_buffers 4 4k;

    client_max_body_size 8m;

 

    open_file_cache max=204800 inactive=20s;

    open_file_cache_min_uses 1;

    open_file_cache_valid 30s;

 

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

    #                  '$status $body_bytes_sent "$http_referer" '

    #                  '"$http_user_agent" "$http_x_forwarded_for"';

 

    #指令指定 nginx 是否調用 sendfile 函數(zero copy 方式)來輸出文件,對於普通應用,必須設爲 on,若是用來進行下載等應用磁盤IO重負載應用,可設置爲 off,以平衡磁盤與網絡I/O處理速度,下降系統的uptime.

    sendfile        on;

    #如下兩個選項用於防止網絡阻塞 參考http://i.cn.yahoo.com/nesta2001zhang/blog/p_104/

    tcp_nopush     on;

    tcp_nodelay on;

 

    #超時時間

    keepalive_timeout  30;

    #後端服務器鏈接的超時時間_發起握手等候響應超時時間

    proxy_connect_timeout 5;

    #鏈接成功後_等候後端服務器響應時間_其實已經進入後端的排隊之中等候處理(也能夠說是後端服務器處理請求的時間)

    proxy_read_timeout 10;

    #後端服務器數據回傳時間_就是在規定時間以內後端服務器必須傳完全部的數據

    proxy_send_timeout 5;

    #設置代理服務器(nginx)保存用戶頭信息的緩衝區大小

    proxy_buffer_size 128k;

    #proxy_buffers緩衝區,網頁平均在256k如下的話,這樣設置

    proxy_buffers 4 256k;

    #高負荷下緩衝大小(proxy_buffers*2)

    proxy_busy_buffers_size 256k;

    #設定緩存文件夾大小,大於這個值,將從upstream服務器傳遞請求,而不緩衝到磁盤

    proxy_temp_file_write_size 1024m;

    

    client_body_timeout 10;

    client_header_timeout 10;

    send_timeout 10;

 

    #打開gzip模式

    gzip  on;

    #最小壓縮文件大小

    gzip_min_length  1k;

    #壓縮緩衝區

    gzip_buffers     4 16k;

    gzip_http_version 1.0;

    #壓縮級別 1-9 ,1 最快,9最慢

    gzip_comp_level 2;

    #壓縮類型,默認就已經包含text/html 因此下面就不用再寫了,固然寫上去的話,也不會有問題,可是會有一個warn

    gzip_types       text/plain application/x-javascript text/css application/xml;

    gzip_vary on;

 

    #開啓限制IP鏈接數的時候須要使用

    #爲session會話狀態分配一個大小爲size的內存存儲區,限制了每秒(分、小時)只接受rate個IP的頻率。

    limit_req_zone $binary_remote_addr zone=req_one:10m rate=200r/s;

    limit_conn_zone $binary_remote_addr zone=one:10m;

    

    #注:proxy_temp_path和proxy_cache_path指定的路徑必須在同一分區

    proxy_temp_path /usr/local/nginx/proxy_temp;

    #設置Web緩存區名稱爲cache_one,內存緩存空間大小爲200MB,1天沒有被訪問的內容自動清除,硬盤緩存空間大小爲10GB。

    proxy_cache_path /usr/local/nginx/proxy_cache levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=10g;

 

    

    #配置後端服務器信息

    #每一個請求按時間順序逐一分配到不一樣的後端服務器,若是後端服務器down掉,能自動剔除。

    upstream front{

        server  localhost:9090 max_fails=3 fail_timeout=60s;

        #server  localhost:8020 backup;

    }

    
 

 

    server{

 

        #配置監聽端口

        listen       80;

        #server_name  192.192.192.192 192.192.192.192;

        charset utf-8;

        #access_log  logs/host.access.log;

        

        #前端根目錄 ,用於存放一些額外要訪問的文件,如驗證網站的全部者

        set $staticfilehome /home/project/qibao-product-web-front/root;

         

        #對 "/" 啓用負載均衡

        location / {

            #定義服務器的默認網站根目錄位置

            root   $staticfilehome;

            expires 0m;            

            #定義首頁索引文件的名稱

            index  index.html index.htm;

            proxy_redirect off;

            #http應用,使用http_host,防止後臺要取端口的狀況(host 只返回ip地址)

            proxy_set_header Host $http_host;

            proxy_set_header X-Real-IP $remote_addr;

            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

            #容許客戶端請求的最大單文件字節數

            client_max_body_size    20m;

            #緩衝區代理緩衝用戶端請求的最大字節數

            client_body_buffer_size 128k;

            #該指令用於指定使用的內存存儲區(zone)名稱,以及最大的突發請求數(burse)。若是請求的速率超過了limit_req_zone指令中設置的速率,這些請求將被延遲處理,在這種狀況下,請求得到服務不可用信息,返回503狀態碼。

            limit_req zone=req_one burst=100 nodelay;

            #指定一個會話最大的併發鏈接數。當超過指定的最發併發鏈接數時,服務器將返回 "Service unavailable" (503)。

            limit_conn one 100;

            #若是後端的服務器返回50二、50四、執行超時等錯誤,自動將請求轉發到upstream負載均衡池中的另外一臺服務器,實現故障轉移。

            proxy_next_upstream http_502 http_504 error timeout invalid_header;

            #參數非空的,則訪問 負載均衡

            if ( $query_string ~ ".+") {

                proxy_pass http://front; #注意設置在這裏

            }

            #文件不存在,則訪問 負載均衡

            try_files $uri @proxyroot;

        }

 

        #先在root,根目錄下找

        location @proxyroot {

            #定義服務器的默認網站根目錄位置

            root   $staticfilehome;

            #文件不存在,則訪問 負載均衡

            try_files $uri @proxyfrontweb;

            expires 0d;            

        }

      

        #對 frontweb 請求,啓用負載均衡

        location @proxyfrontweb {

            #定義服務器的默認網站根目錄位置

            root   html;

            #定義首頁索引文件的名稱

            index  index.html index.htm;

            proxy_redirect off;

            #http應用,使用http_host,防止後臺要取端口的狀況(host 只返回ip地址)

            proxy_set_header Host $http_host;

            proxy_set_header X-Real-IP $remote_addr;

            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

            #容許客戶端請求的最大單文件字節數

            client_max_body_size    20m;

            #緩衝區代理緩衝用戶端請求的最大字節數

            client_body_buffer_size 128k;

            #在須要使用負載均衡的server中增長,這個指令設置被代理服務器的地址和被映射的URI

            proxy_pass http://front;  #注意設置在這裏

            #該指令用於指定使用的內存存儲區(zone)名稱,以及最大的突發請求數(burse)。若是請求的速率超過了limit_req_zone指令中設置的速率,這些請求將被延遲處理,在這種狀況下,請求得到服務不可用信息,返回503狀態碼。

            limit_req zone=req_one burst=100 nodelay;

            #指定一個會話最大的併發鏈接數。當超過指定的最發併發鏈接數時,服務器將返回 "Service unavailable" (503)。

            limit_conn one 100;

            #若是後端的服務器返回50二、50四、執行超時等錯誤,自動將請求轉發到upstream負載均衡池中的另外一臺服務器,實現故障轉移。

            proxy_next_upstream http_502 http_504 error timeout invalid_header;

            expires 0d;

        }

 

        #error_page  404              /404.html;

 

        # redirect server error pages to the static page /50x.html

        #

        error_page   500 502 503 504 404  /50x.html;

        location = /50x.html {

            root   html;

 

        }        

        #對靜態資源文件緩存

        location /assets/ {

            expires 7d;

            concat    on;

            access_log off;

            root /home/project/qibao-product-web-front/webapp;

         }

         

         #圖片

         location /upload/ {

            root /home/data/qibao_product/;

            expires 0d;

            access_log off;

            image on;

            image_output on;
            

         }

        

        #禁止訪問 WEB-INF下 文件

        location ~ ^/(WEB-INF)/ {   

            deny all;

        }        

    }


}

相關文章
相關標籤/搜索