linux 首先安装nginx
1、安装必要环境
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
yum -y install gd-devel
2、cd /usr/local
mkdir nginx
cd nginx
3、下载源码
wget http://nginx.org/download/nginx-1.10.1.tar.gz
tar -zxvf nginx-1.10.1.tar.gz
cd nginx-1.10.1.tar.gz
4、配置
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_image_filter_module
5、编译安装
make
make install
6、配置nginx.conf
location ~* .*\.(jpg|gif|png)$ {
root html;
set $w $arg_w;
set $h $arg_h;
image_filter resize $w $h;//缩放
#image_filter crop $w $h;//剪裁
image_filter_jpeg_quality 75;
image_filter_buffer 10m;
access_log off;
}
7、测试:http://ip/1.jpg?w=200&h=201
第二版:可以将缩放完的图片进行缓存,减少服务压力,提高性能。
1、完整配置
#user nobody;
user root;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 9080;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /home/upload;
index index.html index.htm;
}
location ~* /(.*)\/(.*)_(\d+)x(\d+)\.(jpg|png|jpeg|gif)$ {
root /home/upload;
if (-f $request_filename) {
break;
}
set $filepath $1;
set $filename "$2.$5";
set $thumb "$2_$3x$4.$5";
set $width $3;
set $height $4;
if (!-f $document_root/$filepath/$filename) {
return 404;
}
rewrite /(.*)\/(.*)\.(.*) /imgcache/$2.$3;
if (!-f $request_filename) {
proxy_pass http://127.0.0.1:$server_port/image-resize/$filepath/$filename?width=$width&height=$height;
break;
}
proxy_store $document_root/imgcache/$thumb;
proxy_store_access user:rw group:rw all:r;
proxy_set_header Host $host;
}
location /image-resize {
root /home/upload;
rewrite /(image-resize)/(.*) /$2 break;
image_filter resize $arg_width $arg_height;
image_filter_jpeg_quality 75;
allow 127.0.0.0/8;
deny all;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}