linux nginx 缩放图片

本文详细介绍了在Linux环境下安装Nginx的过程,包括必要的环境搭建、源码下载与配置、编译安装及配置文件详解。特别聚焦于Nginx的图片处理模块,演示了如何使用Nginx进行图片缩放、缓存和优化,以减轻服务器压力并提升性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
    #    }
    #}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值