nginx lua处理图片
Posted archoncap
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx lua处理图片相关的知识,希望对你有一定的参考价值。
user apache apache; worker_processes 4; worker_rlimit_nofile 100000; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { use epoll; multi_accept on; worker_connections 51200; } # load modules compiled as Dynamic Shared Object (DSO) # dso { # load ngx_http_fastcgi_module.so; # load ngx_http_rewrite_module.so; } http { server_tokens off; include mime.types; default_type application/octet-stream; sendfile on; tcp_nopush on; keepalive_timeout 20; server_names_hash_bucket_size 128; client_header_buffer_size 4k; large_client_header_buffers 4 32k; client_body_buffer_size 512k; client_max_body_size 20m; gzip on; gzip_disable "msie6"; gzip_min_length 1k; gzip_buffers 16 64k; gzip_http_version 1.1; gzip_comp_level 6; gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/javascript; gzip_vary on; lua_package_path ‘/usr/local/tengine/lua/?.lua;;‘; upstream tomcat_server { ip_hash; server 172.16.151.112:6080 ; server 172.16.151.113:6080 ; # server 172.16.151.235 max_fails=2 fail_timeout=30s; } server { listen 80; server_name image0.lovebuy.com.cn; # root /usr/html/lovebuy; location = / { root /usr/html/lovebuy/html; index index.html; } location ~ ^/index.html$ { root /usr/html/lovebuy/html; } location ^~ /subject/ { root /usr/html/lovebuy/html; } # location ~ ^/goods_(\d\d\d).html$ { # # if (-f $request_filename) { # root /usr/html/lovebuy/html; # rewrite ^/goods_(\d\d).html$ /page0$1/goods_0$1.html break; # rewrite ^/goods_(\d+).html$ /page$1/goods_$1.html break; # } # # if (!-f $request_filename) { # # rewrite ^/goods_(\d+).html http://www.lovebuy.com.cn/goods_3784.html permanent; # rewrite ^/goods_(\d+).html http://172.16.151.112:6080/goods_$1.htm permanent; # } # proxy_set_header Host $host; # proxy_set_header X-Forwarded-For $remote_addr; # proxy_pass http://tomcat_server; # } location ~ ^/goods_(.*)(\d\d\d).html$ { root /usr/html/lovebuy/html/page$2/; if (-f $request_filename) { rewrite ^/goods_(.*)(\d\d\d).html$ /goods_$1$2.html break; } if (!-f $request_filename) { rewrite ^/goods_(\d+).html http://$host/goods_$1.htm ; # proxy_pass http://tomcat_server ; } # proxy_set_header Host $host; # proxy_set_header X-Forwarded-For $remote_addr; # proxy_pass http://tomcat_server; } # location ~ ^/goods_(.*)(\d\d\d).html$ { # # root /usr/html/lovebuy/html; # rewrite ^/goods_(.*)(\d\d\d).html$ /page$2/goods_$1$2.html break; # } #/thumbnail目录下的图片请求不经过缩略图模块 location ^~ /thumbnail/ { root /usr/html/lovebuy/upload; } location /lua { default_type ‘text/plain‘; content_by_lua ‘ngx.say("hello, lua")‘; } ######################################################### #对类似_100x100.gif/jpg/png/jpeg进行缩略图处理 location ~* _([0-9]+)x([0-9]+)\.(gif|jpg|png|jpeg)$ { #匹配文件名规则 # location ^~ /photo/ { #匹配文件名规则 rewrite ^/(upload/)(.*)$ /$2 last; root /usr/html/lovebuy/upload; #点根目录 lua_code_cache off; set $image_root /usr/html/lovebuy/upload; #图片目录 set $thumbnail_root /usr/html/lovebuy/upload/thumbnail; #缩略图存放目录 #果缩略图文件存在,直接返回 set $file $thumbnail_root$uri; if (-f $file) { rewrite ^/(.*)$ /thumbnail/$1 last; } #果缩略图文件不存在,则应用缩略图模块处理 if (!-f $file) { rewrite_by_lua_file /usr/local/tengine/lua/thumbnail.lua; # rewrite_by_lua ‘ # local command = "/usr/local/GraphicsMagick/bin/gm convert /usr/local/tengine/html/image/photo/2m.jpg /usr/local/tengine/html/image/photo/2m_300x300.jpg" ; # os.execute(command); # ‘; } } location /nginx-status { stub_status on; access_log off; #加入访问限制 #allow 60.195.252.106; allow 172.16.151.83; #deny all; } ##################缓存配置###############################3 location ~ \.(gif|jpg|jpeg|png|bmp|ico)$ { root /usr/html/lovebuy/; expires 1d; } location ^~ /resources/ { root /usr/html/lovebuy/; expires 30d; } ###################缓存配置-end########################## location / { concat on; #开启concat模块 root /usr/html/lovebuy/; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
以上是关于nginx lua处理图片的主要内容,如果未能解决你的问题,请参考以下文章
使用nginx+lua+GraphicsMagick实现图片自动 裁剪
搭建FastDFS分布式集群通过Nginx+Lua+GraphicsMagick实现动态压缩图片
搭建带lua模块的nginx调用ImageMagick工具实现实时剪切缩放图片功能