博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
1.5. Nginx module
阅读量:7188 次
发布时间:2019-06-29

本文共 5662 字,大约阅读时间需要 18 分钟。

1.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;    }

1.5.2. sub_filter 页面中查找和替换

location / {    sub_filter '

替换掉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;    }

1.5.3. auth_basic

cd /usr/local/nginx/confserver {	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-utilshtpasswd -c -d htpasswd user_name
[提示] 提示

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

1.5.4. valid_referers

例 1.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;	}}

1.5.5. ngx_http_flv_module

location ~ \.flv$ {    flv;}

1.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;}

1.5.7. limit_zone

limit_zone   one  $binary_remote_addr  10m;server {	location /download/ {	limit_conn   one  1;}

1.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;}

1.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;}						
ngx_http_mirror_module
location / { mirror /mirror; proxy_pass http://backend;}location /mirror { internal; proxy_pass http://test_backend$request_uri;}

1.5.10. limit_except

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

1.5.11. geoip_country_code

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

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

你可能感兴趣的文章
阿里开发者们的第20个感悟:好的工程师为人写代码,而不仅是为编译器
查看>>
linux 磁盘管理
查看>>
VMWARE服务器虚拟化功能介绍
查看>>
深入搜索引擎原理
查看>>
jemalloc 内存分配管理
查看>>
mysqld源码及目录结构
查看>>
通过交换 a,b 中的元素,使[序列 a 元素的和]与[序列 b 元素的和]之间的差最小。...
查看>>
Cesium入门7 - Adding Terrain - 添加地形
查看>>
kubernetets单机版安装
查看>>
MAC-mojave-关于VMware虚拟机键盘鼠标失灵解决
查看>>
我的友情链接
查看>>
2-2 中断/异常机制工作原理
查看>>
Java到底是传引用还是传值?
查看>>
RAR压缩包审计工具unrar-nofree
查看>>
什么是CSS网页切图
查看>>
ConcurrentHashMap学习
查看>>
struts2标签实例
查看>>
Golang walk在win及linux建立 GUI 應用程式
查看>>
Linux执行命令常见的英语语句
查看>>
elasticsearch简单JavaAPI总结
查看>>