46.5. Nginx module

简介:

46.5.1. stub_status

location /nginx_status {
	stub_status on;
	access_log  off;
	allow 127.0.0.1;
	deny all;
}
		

php-fpm 状态

    location ~ ^/(status|ping)$ {
        access_log off;
        allow 202.82.21.12;
        deny all;
        fastcgi_pass 127.0.0.1:9000;
		fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
        include fastcgi_params;
    }
		

46.5.2. sub_filter 页面中查找和替换

		
location / {
    sub_filter '<a href="http://127.0.0.1:8080/'  '<a href="https://$host/';
    sub_filter '<img src="http://127.0.0.1:8080/' '<img src="https://$host/';
    sub_filter_once on;
}
		
		

替换掉proxy_pass页面中的内容

    location ~ ^/live800 {
        proxy_pass           http://218.23.24.53;
        rewrite              ^/live800/(.*)  /$1 break;
        proxy_set_header    Accept-Encoding "";
        proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header    Host www.abc.com;

        sub_filter_types text/html text/css text/xml text/css text/javascript;
        sub_filter 'www.abc.com'  '$host';
        sub_filter_once off;
    }
		

46.5.3. auth_basic

cd /usr/local/nginx/conf
server {
	listen 80;
	server_name www.example.com;
	root /var/www/htdocs;
	index index.html;

	location / {
		try_files $uri $uri/ /index.html;
		auth_basic            "Login";
        auth_basic_user_file  htpasswd;
	}
}
		

生成密码文件

$ sudo apt-get install apache2-utils

htpasswd -c -d htpasswd user_name
		
[提示] 提示

必须使用 -d Force CRYPT encryption of the password. 选项,

46.5.4. valid_referers

例 46.4. Example: valid_referers

location /photos/ {
  valid_referers none blocked www.mydomain.com mydomain.com;

  if ($invalid_referer) {
    return   403;
  }
}
			
location ~* \.(gif|jpg|jpeg|png|bmp|txt|zip|jar|swf)$ {
	valid_referers none blocked *.mydomain.com;
	if ($invalid_referer) {
		rewrite ^/  http://www.mydomain.com/default.gif;
		#return 403;
	}

}

location /images/ {
	alias /www/images/;
	valid_referers none blocked *.mydomain.com;
	if ($invalid_referer) {
		rewrite ^/  http://www.mydomain.com/default.gif;
	}
}
			

46.5.5. ngx_http_flv_module

location ~ \.flv$ {
    flv;
}
		

46.5.6. ngx_http_mp4_module

location /video/ {
    mp4;
    mp4_buffer_size       1m;
    mp4_max_buffer_size   5m;
    mp4_limit_rate        on;
    mp4_limit_rate_after  30s;
}
		

46.5.7. limit_zone

limit_zone   one  $binary_remote_addr  10m;

server {
	location /download/ {
	limit_conn   one  1;
}
		

46.5.8. image_filter

image_filter 配置项:
image_filter off;    在所在location关闭模块处理。
image_filter test;  确保应答是JPEG,GIF或PNG格式的图像。否则错误 415 (Unsupported Media Type) 将被返回。
image_filter size;  以JSON格式返回图像信息。
image_filter rotate 90 | 180 | 270;  将图像逆时针旋转指定角度。 参数的值可以包含变量。 可以单独使用,或与 resize 和 crop 变换同时使用.
image_filter resize width height;  按比例缩小图像至指定大小。 如果想只指定其中一维,另一维可以指定为: “-”。 如果有错误发生,服务器会返回 415 (Unsupported Media Type). 参数的值可以包含变量。 当与 rotate 参数同时使用时, 旋转发生在缩放 之后。 
image_filter crop width height;  按比例以图像的最短边为准对图像大小进行缩小,然后裁剪另一边多出来的部分。 如果想只指定其中一维,另一维可以指定为: “-”。 如果有错误发生,服务器会返回 415 (Unsupported Media Type). 参数的值可以包含变量。 当与 rotate 参数同时使用时, 旋转发生在裁剪 之前。

image_filter_buffer 配置项:
image_filter_buffer size; 例如 image_filter_buffer 1M; 设置用来读图像的缓冲区的最大值。 若图像超过这个大小,服务器会返回 415 (Unsupported Media Type).
image_filter_jpeg_quality quality; 例如 image_filter_jpeg_quality 75;设置变换后的JPEG图像的 质量 。 可配置值: 1 ~ 100 。 更小的值意味着更差的图像质量以及更少需要传输的数据。 推荐的最大值是95. 参数的值可以包含变量。
image_filter_sharpen percent; image_filter_sharpen 0;  增加最终图像的锐度。 锐度百分比可以超过100. 0为关闭锐化。 参数的值可以包含变量。
image_filter_transparency on|off; image_filter_transparency on;定义当对PNG,或者GIF图像进行颜色变换时是否需要保留透明度。 损失透明度有可能可以获得更高的图像质量。 PNG图像中的alpha通道的透明度默认会一直被保留。
 		
		
比如所有的图片并修改尺寸为 800x600

       location ~* \.(jpg|gif|png)$ {
               image_filter resize 800 600;
       }

匹配images目录所有图片并修改尺寸为1920x1080

       location ~* /images/.*\.(jpg|gif|png)$ {
               image_filter resize 1920 1080;
       }


再比如用url来指定

location ~* (.*\.(jpg|gif|png))!(.*)x(.*)$ {
    set $width      $3;
    set $height     $4;
	rewrite "(.*\.(jpg|gif|png))(.*)$" $1;
}

location ~* .*\.(jpg|gif|png)$ {
	image_filter resize $width $height;
}

location ~* /images/(.+)_(d+)x(d+).(jpg|gif|png)$ {            
    set $height $2;
    set $width $3;
    if ($height = "0") {
        rewrite /images/(.+)_(d+)x(d+).(jpg|gif|png)$ /images/$1.$4 last;
    }
    if ($width = "0") {
        rewrite /images/(.+)_(d+)x(d+).(jpg|gif|png)$ /images/$1.$4 last;
    }

    #根据给定的长宽生成缩略图
    image_filter resize $height $width;
    
    #原图最大2M,要裁剪的图片超过2M返回415错误,根据你的需求调节参数image_filter_buffer 
    image_filter_buffer 2M;                          
    
    #error_page  415      		/images/404.jpg;
    try_files /images/$1.$4  	/images/404.jpg;	
}

location ~* /images {
    
}
        
location ~* ^/images/resize/([\d\-]+)_([\d\-]+)/(.+) {  
    alias /www/example.com/img.example.com/$3;
    image_filter test;
    image_filter resize $1 $2;
    image_filter_buffer 2M;
    image_filter_jpeg_quality 95;
    image_filter_sharpen 90;
    expires 60d;
}		
		

46.5.9. ngx_stream_proxy_module

ngx_stream_proxy_module 用法与 ngx_http_proxy_module 及其相似, 前者用于tcp代理或负载均衡。后者只能用于 http 的代理

注意模块的proxy_pass指令只能在server段使用, 提供域名或ip地址和端口转发,协议可以是tcp,也可以是udp。

		
server {
    listen 127.0.0.1:80;
    proxy_pass 127.0.0.1:8080;
}

server {
    listen 25;
    proxy_connect_timeout 1s;
    proxy_timeout 1m;
    proxy_pass mail.example.com:25;
}

server {
    listen 53 udp;
    proxy_responses 1;
    proxy_timeout 20s;
    proxy_pass dns.example.com:53;
}

server {
    listen [::1]:8000;
    proxy_pass unix:/tmp/stream.socket;
}		
		</screen>
	</section>
	<section id="ngx_http_mirror_module">
		<title>ngx_http_mirror_module</title>
		<screen>
location / {
    mirror /mirror;
    proxy_pass http://backend;
}

location /mirror {
    internal;
    proxy_pass http://test_backend$request_uri;
}
		
		

46.5.10. limit_except

    location /api/ {

        limit_except PUT DELETE {
            proxy_pass http://127.0.0.1:9080;
        }
    }		
		

46.5.11. geoip_country_code

location /google {
    if ( $geoip_country_code ~ (RU|CN) ) {
        proxy_pass http://www.google.hk;
    }
}
		




原文出处:Netkiller 系列 手札
本文作者:陈景峯
转载请与作者联系,同时请务必标明文章原始出处和作者信息及本声明。

目录
相关文章
|
应用服务中间件 C语言 nginx
Nginx Upload Module 上传模块
传统站点在处理文件上传请求时,普遍使用后端编程语言处理,如:Java、PHP、Python、Ruby等。今天给大家介绍Nginx的一个模块,Upload Module上传模块,此模块的原理是先把用户上传的文件保存到临时文件,然后在交由后台页面处理,并且把文件的原名,上传后的名称,文件类型,文件大小set到页面。
1908 0
|
前端开发 应用服务中间件 nginx
|
负载均衡 JavaScript 前端开发
《深入理解Nginx:模块开发与架构解析》一2.5 用HTTP proxy module配置一个反向代理服务器
本节书摘来自华章出版社《深入理解Nginx:模块开发与架构解析》一书中的第2章,第2.5节,作者 陶辉,更多章节内容可以访问云栖社区“华章计算机”公众号查看
2067 0
|
JSON 应用服务中间件 对象存储
利用 Aliyun OSS Nginx proxy module 实现OSS 图片处理回写功能
1、主要介绍内容 此篇文章主要利用Aliyun OSS Nginx proxy module 实现OSS 图片处理回写功能,借助OSS Nginx Proxy module 及 OSS 的上传回调功能实现OSS图片处理回写功能,当然文章目的并不在于强调图片处理回写功能,而是借实现一个例子来利用Al
18681 0
|
应用服务中间件 对象存储 nginx
|
存储 应用服务中间件 Linux
FastDFS(1):Centos7 安装FastDFS+nginx module
本文原文连接: http://blog.csdn.net/freewebsys/article/details/45150941 转载请注明出处! 1,关于FastDFS 摘自:http://www.oschina.net/p/fastdfs FastDFS是一个开源的分布式文件系统,她对文件进行管理,功能包括:文件存储、文件同步、文件访问(文件上传、文件下载)等,解决
4922 0
|
应用服务中间件 nginx 数据格式
Nginx 学习笔记(四)nginx-module-vts模块
看看已经搭建好的效果图 1、基本信息 2、过滤信息 3、上游服务器 4、其他进程  1、获取组区域(默认返回json格式数据) (1)mainZones https://www.tinywan.
2941 0
|
18天前
|
运维 前端开发 应用服务中间件
LNMP详解(八)——Nginx动静分离实战配置
LNMP详解(八)——Nginx动静分离实战配置
23 0
|
28天前
|
应用服务中间件 nginx
Nginx中如何配置中文域名?
Nginx中如何配置中文域名?
38 0
|
17天前
|
前端开发 应用服务中间件 nginx
Nginx配置详解Docker部署Nginx使用Nginx部署vue前端项目
Nginx配置详解Docker部署Nginx使用Nginx部署vue前端项目
78 0