nginx文件处理
有些时候需要在网站中创建一些文本文件已实现某些功能,如当前获取免费SSL证书的文件验证因版本控制又不想在网站新建文件或者模拟robots.txt文件来控制搜索引擎爬虫行为,这时候可以配置nginx返回text/plain格式文本以达到直接访问文件的效果。如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| server { listen 80; server_name www.example.com; access_log /var/log/nginx/www-example-com.log access; root /etc/nginx/html; ...... location = /.well-known/pki-validation/fileauth.txt { default_type text/plain; return 200 '201708090000005cpmpl49g1psxj1r86w70mmpi27g61r4f7u2bthwedki0trwtx'; } location = /robots.txt { default_type text/plain; return 200 'User-agent: *\nDisallow: /\n'; } ...... }
|
访问效果如下:

nginx图片处理
ngxin有很多有一堆的module,当业务量不大服务器的负载较低时,可以简单的用ngxin
的Image Filter
作为图片缩放、剪裁、旋转等处理的工具。
安装依赖
image filter
依赖libgd2
,所以需要先安装。
安装nginx
nginx
的module
是静态加载
的,必须编译到nginx的主文件里面。图片处理需要安装nginx-mod-http-image-filter
模块。
1
| yum install nginx nginx-mod-http-image-filter nginx-all-modules
|
配置image filter
网上有很多利用image filter
做图片缩放的配置,下面是我写的一个配置(部分代码),比较简陋,另外,目前的问题是jpeg的图片质量参数无法生效,还在找原因。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| set $width "-"; set $height "-"; if ( $arg_w != "" ){ set $width $arg_w; } if ( $arg_h != "" ){ set $height $arg_h; } set $rotate "-"; if ( $arg_r != "" ){ set $rotate $arg_r; } set $quality "-"; if ( $arg_q != "" ){ set $quality $arg_q; } location /images/ { image_filter resize $width $height; image_filter rotate $rotate; image_filter_jpeg_quality $quality; image_filter_interlace on; image_filter_transparency on; image_filter_buffer 8M; error_page 415 = /empty; } location = /empty {
empty_gif; }
|
这样就可以通过*.jpg?r=90&w=100&h=100
这样的参数配置做图片的缩放和旋转了。
其他选择
nginx 还有一些其他的第三方module可以用于图片缩放。例如:
- ngx_small_light 可以使用ImageMagick的功能,比image filter强大很多。
- ngx_image_thumb是国产的,这个module会自动处理图盘的后缀参数,进行图片剪裁、缩放,配置起来比较容易,同时支持图片水印。