一、
$ 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;
}
}
}