GVKun编程网logo

Nginx 模块 - ngx_http_rewrite_module(Nginx 模块开发指南)

21

针对Nginx模块-ngx_http_rewrite_module和Nginx模块开发指南这两个问题,本篇文章进行了详细的解答,同时本文还将给你拓展20、nginx之ngx_http_upstream

针对Nginx 模块 - ngx_http_rewrite_moduleNginx 模块开发指南这两个问题,本篇文章进行了详细的解答,同时本文还将给你拓展20、nginx 之 ngx_http_upstream_module 模块、21、nginx 之 ngx_http_proxy_module 模块、Docker Nginx ngx_http_image_filter_module、Nginx 1.13.4 发布,新增 ngx_http_mirror_module 模块等相关知识,希望可以帮助到你。

本文目录一览:

Nginx 模块 - ngx_http_rewrite_module(Nginx 模块开发指南)

Nginx 模块 - ngx_http_rewrite_module(Nginx 模块开发指南)

原文地址

ngx_http_rewrite_module 模块用于通过 PCRE 正则表达式改变请求 URI,返回重定向并可以有条件地选择配置。

break、if、return、rewrite 以及 set 指令的处理顺序如下:

  • 首先按顺序执行在 server 块中指定的该模块的指令
  • 然后循环:
    • 根据请求 URI 搜索 location
    • 该模块的指令在 location 内指定时,按顺序执行
    • 如果请求 URI 被重写,则重复循环但不超过 10 次。

1. 指令

break

break 的可用上下文有:server、location、if。用于停止处理当前的 ngx_http_rewrite_module 指令集合。

如果指令是在 location 中声明的,则在该 location 中继续处理请求。

示例:

if ($slow) {
    limit_rate 10k;
    break;
}

if

if 的可用上下文有:server、location。如果指定的条件为 true,则在花括号内指定的模块指令被执行,并且该请求被分配给 if 指令内的配置。if 指令中的配置从先前的配置级别继承。

条件可能是以下任何一种情况:

  • 变量名;如果变量值是空字符串或 “0” 则为 FALSE。注意,在 1.0.1 版本之前,任何以 “0” 开头的字符串都会被当做 FALSE。
  • 使用 “=” 和 “!=” 的变量跟字符串的比较
  • 使用 “~”(区分大小写匹配)和 “~*”(不区分大小写匹配)运算符将变量与正则表达式匹配。正则表达式可以包含捕获,之后可以通过 1..1..9 这几个变量名重复使用。“!~” 和 “!~*” 用作不匹配运算符。如果正则表达式包含 “}” 或 “;” 字符,则整个表达式应该用单引号或双引号括起来。
  • 用 “-f” 和 “!-f” 运算符检查文件是否存在
  • 用 “-d” 和 “!-d” 运算符检查目录是否存在
  • 用 “-e” 和 “!-e” 运算符检查文件、目录或符号链接的存在性
  • 用 “-x” 和 “!-x” 运算符检查可执行文件

示例:

if ($http_user_agent ~ MSIE) {
    rewrite ^(.*)$ /msie/$1 break;
}

if ($http_cookie ~* "id=([^;]+)(?:;|$)") {
    set $id $1;
}

if ($request_method = POST) {
    return 405;
}

if ($slow) {
    limit_rate 10k;
}

if ($invalid_referer) {
    return 403;
}

$invalid_referer 变量的值由 valid_referers 指令设置。

return

return 的可用上下文有:server、location、if。
停止处理并向客户端返回指定的代码。非标代码 444 会关闭连接并且不会发送响应头。

从 0.8.42 版本开始,可以指定重定向 URL(代码 301,302,303,307 和 308)或响应正文文本(用于其他代码)。响应正文文本和重定向 URL 可以包含变量。作为特殊情况,可以指定重定向 URL 作为此服务器本地的 URI,在这种情况下,完整重定向 URL 将根据请求方案($scheme)、server_name_in_redirect 以及 port_in_redirect 指令形成。

另外,可以将使用 302 代码的临时重定向 URL 指定为唯一参数。这样的参数应该以 “http//”,“https//” 或 “$scheme” 字符串开头。URL 中可以包含变量。

版本 0.7.51 之前的版本可以返回的代码(仅支持这些代码):204,400,402-406,408,410,411,413,416 和 500-504。
在 1.1.16 和 1.0.13 之后,代码 307 才开始被视为重定向。
在版本 1.13.0 之后,代码 308 才开始被视为重定向。
另请参阅 error_page 指令。

rewrite

rewrite 的可用上下文有:server、location、if。

如果指定的正则表达式与请求 URI 匹配,则将按照替换字符串修改 URI。rewrite 指令按照其在配置文件中的顺序依次执行。可以使用标志来终止指令。如果替换字符串以 “http//”,“https//” 或 “$scheme” 开头,则处理停止并且将重定向返回给客户端。

可选的标志有:

  • last:停止处理当前的 ngx_http_rewrite_module 指令集合,开始搜索能够匹配修改过的 URI 的新的 location
  • break:跟 break 指令一样,停止处理当前的 ngx_http_rewrite_module 指令集合
  • redirect:返回使用 302 代码的临时重定向,在替换字符串不以 “http//”,“https//” 或 “$scheme” 开头时使用
  • permanent:返回使用 301 代码的永久重定向

完整的重定向 URL 根据请求方案($scheme)、server_name_in_redirect 以及 port_in_redirect 指令构成。

示例:

server {
    ...
    rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 last;
    rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra  last;
    return  403;
    ...
}

但是如果这些指令被放到了 “/download” 这个 location 中,last 标志需要替换为 break,否则 Nginx 会在循环 10 次后返回 500 错误:

location /download/ {
    rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 break;
    rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra  break;
    return  403;
}

如果替换字符串包含新的请求参数,之前的请求参数会追加在新请求参数后面。如果不希望这样,那么在替换字符串的末尾添加一个问号可以避免添加它们,例如:

rewrite ^/users/(.*)$ /show?user=$1? last;

如果正则表达式包含 “}” 或 “;” 字符,整个正则表达式需要放在单引号或双引号里。

rewrite_log

语法:rewrite_log on | off;
默认值:rewrite_log off;
rewrite_log 的可用上下文有: http、server、location、if。

启用或禁用将 ngx_http_rewrite_module 模块指令处理结果记录到通知级别(notice)的 error_log 中。

set

语法:set $variable value;
默认值:无

set 的可用上下文有:server、location、if。

为指定的变量设置一个值。该值可以包含文本,变量及其组合。

uninitialized_variable_warn

语法:uninitialized_variable_warn on | off
默认值:uninitialized_variable_warn on;

uninitialized_variable_warn 的可用上下文有:http、server、location、if。

是否将未初始化变量导致的警告写入日志。

2. 内部实现

ngx_http_rewrite_module 模块的指令在配置阶段被编译成内部指令,在请求处理期间会被解释。解释器是一个简单的虚拟堆栈机器。

例如,下面的指令

location /download/ {
    if ($forbidden) {
        return 403;
    }

    if ($slow) {
        limit_rate 10k;
    }

    rewrite ^/(download/.*)/media/(.*)\..*$ /$1/mp3/$2.mp3 break;
}

会被翻译成这些指令:

variable $forbidden
check against zero
    return 403
    end of code
variable $slow
check against zero
match of regular expression
copy "/"
copy $1
copy "/mp3/"
copy $2
copy ".mp3"
end of regular expression
end of code

注意,上面的 limit_rate 指令没有对应的指令,因为它与 ngx_http_rewrite_module 模块无关。会为 if 块创建一个单独的配置。如果条件成立,则将请求分配给 limit_rate 等于 10k 的此配置。

这个指令中:

rewrite ^/(download/.*)/media/(.*)\..*$ /$1/mp3/$2.mp3 break;

如果正则表达式中的第一个斜杠放在圆括号中,可以减少最终的指令:

rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 break;

相关指令看起来是这个样子:

match of regular expression
copy $1
copy "/mp3/"
copy $2
copy ".mp3"
end of regular expression
end of code

20、nginx 之 ngx_http_upstream_module 模块

20、nginx 之 ngx_http_upstream_module 模块

nginx 的 ngx_http_upstream_module 模块是用于 nginx 反向代理的,默认在安装 nginx 时已经被安装,ngx_http_upstream_module 模块

的内容应放于 nginx.conf 配置的 http {} 标签内。

20.1、ngx_http_upstream_module 模块内部 server 标签说明:

server 192.168.3.101:80 weight=1 max_fails=3 fail_timeout=60 [backup] [down]

健康检查需要使用 nginx_upstream_check_module 模块。


20.2、ngx_http_upstream_module 模块的调度算法:

1、调度算法分类:

调度算法一般分为两类:

第一类为静态调度算法,即负载均衡器根据自身设定的规则进行分配,不需要考虑后端节点服务器的情。例如:rr、wrr、ip_hash 等都属于静态调度算法。

第二类为动态调度算法,即负载均衡器会根据后端节点的当前状态来决定是否分发请求,例如:连接数少的有限获得请求,响应时间短的优先获得请求,

例如:least_conn、fair 等都属于动态调度算法。


2、常见的调度算法:

(1)rr 轮循 (round robin 默认调度算法,静态调度算法):

按客户端请求顺序把客户端的请求逐一分配到不同的后端节点服务器,这相当于 LVS 中的 rr 算法,如果后端节点服务器宕机 (默认情况下 nginx 只检测 80 端口)。

宕机的服务器会自动从节点服务器池中剔除,以便客户端的用户访问不受影响。新的请求会分配给正产的服务器。

upstream myapp1 {

server 192.168.3.103;

server 192.168.3.104;

}


(2)wrr (weight 权重轮循,静态调度算法):

在 rr 轮循算法的基础上加上权重,即为权重轮循算法,当使用该算法时,权重和用户访问成正比,权重值越大,被转发的请求也就越多。可以根据服务器的配

置和性能指定权重值大小,有效解决新旧服务器性能不均带来的请求分配问题。

upstream myapp1 {

server 192.168.3.103 weight=1;

server 192.168.3.104 weight=2;

}


(3)ip_hash (静态调度算法):

每个请求按客户端 IP 的 hash 结果分配,当新的请求到达时,先将其客户端 IP 通过哈希算法哈希出一个值,在随后的客户端请求中,客户 IP 的哈希值只要相同,

就会被分配至同一台服务器,该调度算法可以解决动态网页的 session 共享问题,但有时会导致请求分配不均,即无法保证 1:1 的负载均衡,因为在国内大多数

公司都是 NAT 上网模式,多个客户端会对应一个外部 IP所以,这些客户端都会被分配到同一节点服务器,从而导致请求分配不均。LVS 负载均衡的 -P 参数、

keepalived 配置里的 persistence_timeout 50 参数都类似这个 Nginx 里的 ip_hash 参数,其功能均为解决动态网页的 session 共享问题。注意:当负载调度

算法为 ip_hash 时,后端服务器在负载均衡调度中的状态不能有 weight 和 backup ,即使有也不会生效。

upstream myapp1 {

ip_hash;

server 192.168.3.103;

server 192.168.3.104;

}


(4)fair (动态调度算法):

fair 调度算法会根据后端节点服务器的响应时间来分配请求,响应时间端的优先分配。这是更加智能的调度算法。此种算法可以依据页面大小和加载时间长短,智

能地进行负载均衡,也就是根据后端服务器的响应时间来分配请求,响应时间短的优先分配。Nginx 本身是不支持 fair 调度算法的,如果需要使用这种调度算法,

必须下载 Nginx 的相关模块 upstream_fair。

upstream myapp1 {

fair;

server 192.168.3.103;

server 192.168.3.104;

}


(5)least_conn (动态调度算法):

least_conn 调度算法会根据后端节点的连接数来决定分配情况,哪个机器连接数少就分发。

upstream myapp1 {

least_conn;

server 192.168.3.103;

server 192.168.3.104;

}

(6)url_hash 算法:

url_hash 按访问 URL 的 hash 结果来分配请求,使每个 URL 定向到同一个后端服务器,可以进一步提高后端缓存服务器的效率命中率。多用于后端服务器为缓存

时的场景下,Nginx 本身是不支持 url_hash 的,如果需要使用这种调度算法,必须安装 Nginx 的 hash 模块软件包。

upstream myapp1 {

server 192.168.3.103;

server 192.168.3.104;

hash $request_uri;

hash_method crc32;

}





















21、nginx 之 ngx_http_proxy_module 模块

21、nginx 之 ngx_http_proxy_module 模块

Nginx 的代理功能是通过 ngx_http_proxy_module 模块来实现的。默认在安装 Nginx 时已经安装了 ngx_http_proxy_module 模

块,因此可直接使用 ngx_http_proxy_module 模块。

21.1、ngx_http_proxy_module 模块介绍:

1、proxy_pass 属于 ngx_http_proxy_module 模块,此模块可以将请求转发到另一台服务器,在实际的反向代理工作中,

会通过 location 功能匹配指定的 URI,然后把接收到的符合匹配 URI 的请求通过 proxy_pass 抛给定义好的 upstream 节

点池。


2、官网地址:http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass


3、官网 proxy_pass 使用案例:

(1)将匹配 URI 为 name 的请求抛给 http://127.0.0.1/remote/:

location /name/ {

proxy_pass http://127.0.0.1/remote/;

}


(2)将匹配 URI 为 some/path 的请求抛给 http://127.0.0.1:

location /some/path/ {

proxy_pass http://127.0.0.1;

}


(3)将匹配 URI 为 name 的请求应用指定的 rewrite 规则,然后抛给 http://127.0.0.1:

location /name/ {

rewrite /name/([^/]+) /users?name=$1 break;

proxy_pass http://127.0.0.1;

}


21.2、ngx_http_proxy_module 模块参数说明:




Docker Nginx ngx_http_image_filter_module

Docker Nginx ngx_http_image_filter_module

请帮我配置Nginx和Docker的ngx_http_image_filter_module模块.

我想用Nginx创建图像缩略图.我知道这需要ngx_http_image_filter_module.如果我看一下Dockerfile的Dockerfile,我可以看到它已经用Nginx-module-image-filter构建(我相信这是我需要的).但是当我尝试在Nginx配置中使用image_filter resize 100 100时,Docker无法启动此容器并抛出错误:

Nginx_1 | 2016/11/13 13:28:28 [emerg] 1#1: unkNown directive “image_filter” in /etc/Nginx/conf.d/Nginx.conf:34

Nginx_1 | Nginx: [emerg] unkNown directive “image_filter” in /etc/Nginx/conf.d/Nginx.conf:34

我已经尝试使用自己的Dockerfile构建容器,但没有任何改变.我用过这个Dockerfile:

FROM Nginx

RUN apt-get update

RUN apt-get install --no-install-recommends --no-install-suggests -y \
                        Nginx-module-image-filter

我的主机配置(位于/etc/Nginx/conf.d/下)

server {
  listen 80;

  location /images/originals/ {
    alias /site/storage/app/public/images/originals/;
    image_filter resize 100 100;
  }
}

更新

Nginx配置(标准)

user  Nginx;
worker_processes  1;

error_log  /var/log/Nginx/error.log warn;
pid        /var/run/Nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/Nginx/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  /var/log/Nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/Nginx/conf.d/*.conf;
}

顺便说一句,如果我打入Nginx容器并运行Nginx -V我得到以下内容:

root@8abdbeab3e2e:/etc/Nginx/conf.d# Nginx -V

Nginx version: Nginx/1.11.5

built by gcc 4.9.2 (Debian 4.9.2-10)

built with OpenSSL 1.0.1t 3 May 2016

TLS SNI support enabled

configure arguments: –prefix=/etc/Nginx –sbin-path=/usr/sbin/Nginx –modules-path=/usr/lib/Nginx/modules –conf-path=/etc/Nginx/Nginx.conf –error-log-path=/var/log/Nginx/error.log –http-log-path=/var/log/Nginx/access.log –pid-path=/var/run/Nginx.pid –lock-path=/var/run/Nginx.lock –http-client-body-temp-path=/var/cache/Nginx/client_temp –http-proxy-temp-path=/var/cache/Nginx/proxy_temp –http-fastcgi-temp-path=/var/cache/Nginx/fastcgi_temp –http-uwsgi-temp-path=/var/cache/Nginx/uwsgi_temp –http-scgi-temp-path=/var/cache/Nginx/scgi_temp –user=Nginx –group=Nginx –with-compat –with-file-aio –with-threads –with-http_addition_module –with-http_auth_request_module –with-http_dav_module –with-http_flv_module –with-http_gunzip_module –with-http_gzip_static_module –with-http_mp4_module –with-http_random_index_module –with-http_realip_module –with-http_secure_link_module –with-http_slice_module –with-http_ssl_module –with-http_stub_status_module –with-http_sub_module –with-http_v2_module –with-mail –with-mail_ssl_module –with-stream –with-stream_realip_module –with-stream_ssl_module –with-stream_ssl_preread_module –with-cc-opt=’-g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2′ –with-ld-opt=’-Wl,-z,relro -Wl,-z,Now -Wl,–as-needed’

我们可以看到这里没有图像过滤器模块.

更新2

apt-get install Nginx-extras-dbg

root@e124bcecfddc:/# apt-get install Nginx-extras-dbg
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
  init-system-helpers libluajit-5.1-2 libluajit-5.1-common Nginx-common Nginx-extras
Suggested packages:
  fcgiwrap Nginx-doc ssl-cert
The following NEW packages will be installed:
  init-system-helpers libluajit-5.1-2 libluajit-5.1-common Nginx-common Nginx-extras Nginx-extras-dbg
0 upgraded, 6 newly installed, 0 to remove and 10 not upgraded.
Need to get 5920 kB of archives.
After this operation, 7880 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://security.debian.org/ jessie/updates/main Nginx-common all 1.6.2-5+deb8u4 [88.1 kB]
Get:2 http://security.debian.org/ jessie/updates/main Nginx-extras amd64 1.6.2-5+deb8u4 [595 kB]
Get:3 http://security.debian.org/ jessie/updates/main Nginx-extras-dbg amd64 1.6.2-5+deb8u4 [4982 kB]
Get:4 http://httpredir.debian.org/debian/ jessie/main libluajit-5.1-common all 2.0.3+dfsg-3 [36.6 kB]
Get:5 http://httpredir.debian.org/debian/ jessie/main libluajit-5.1-2 amd64 2.0.3+dfsg-3 [204 kB]
Get:6 http://httpredir.debian.org/debian/ jessie/main init-system-helpers all 1.22 [14.0 kB]
Fetched 5920 kB in 6s (967 kB/s)
debconf: delaying package configuration, since apt-utils is not installed
Selecting prevIoUsly unselected package libluajit-5.1-common.
(Reading database ... 9747 files and directories currently installed.)
Preparing to unpack .../libluajit-5.1-common_2.0.3+dfsg-3_all.deb ...
Unpacking libluajit-5.1-common (2.0.3+dfsg-3) ...
Selecting prevIoUsly unselected package libluajit-5.1-2:amd64.
Preparing to unpack .../libluajit-5.1-2_2.0.3+dfsg-3_amd64.deb ...
Unpacking libluajit-5.1-2:amd64 (2.0.3+dfsg-3) ...
Selecting prevIoUsly unselected package init-system-helpers.
Preparing to unpack .../init-system-helpers_1.22_all.deb ...
Unpacking init-system-helpers (1.22) ...
Selecting prevIoUsly unselected package Nginx-common.
Preparing to unpack .../Nginx-common_1.6.2-5+deb8u4_all.deb ...
Unpacking Nginx-common (1.6.2-5+deb8u4) ...
dpkg: error processing archive /var/cache/apt/archives/Nginx-common_1.6.2-5+deb8u4_all.deb (--unpack):
 trying to overwrite '/etc/Nginx/scgi_params', which is also in package Nginx 1.11.5-1~jessie
dpkg-deb: error: subprocess paste was killed by signal (broken pipe)
Selecting prevIoUsly unselected package Nginx-extras.
Preparing to unpack .../Nginx-extras_1.6.2-5+deb8u4_amd64.deb ...
Unpacking Nginx-extras (1.6.2-5+deb8u4) ...
dpkg: error processing archive /var/cache/apt/archives/Nginx-extras_1.6.2-5+deb8u4_amd64.deb (--unpack):
 trying to overwrite '/usr/sbin/Nginx', which is also in package Nginx 1.11.5-1~jessie
dpkg-deb: error: subprocess paste was killed by signal (broken pipe)
Selecting prevIoUsly unselected package Nginx-extras-dbg.
Preparing to unpack .../Nginx-extras-dbg_1.6.2-5+deb8u4_amd64.deb ...
Unpacking Nginx-extras-dbg (1.6.2-5+deb8u4) ...
Errors were encountered while processing:
 /var/cache/apt/archives/Nginx-common_1.6.2-5+deb8u4_all.deb
 /var/cache/apt/archives/Nginx-extras_1.6.2-5+deb8u4_amd64.deb
E: Sub-process /usr/bin/dpkg returned an error code (1)

apt-get install libgd-dev

root@e124bcecfddc:/# apt-get install libgd-dev
Reading package lists... Done
Building dependency tree
Reading state information... Done
You might want to run 'apt-get -f install' to correct these:
The following packages have unmet dependencies:
 libgd-dev : Depends: libgd3 (= 2.1.0-5+deb8u7) but 2.1.0-5+deb8u6 is to be installed
             Depends: libpng-dev
             Depends: libz-dev
             Depends: libjpeg-dev
             Depends: libfreetype6-dev but it is not going to be installed
             Depends: libxpm-dev but it is not going to be installed
             Depends: libx11-dev but it is not going to be installed
             Depends: libxt-dev but it is not going to be installed
             Depends: libfontconfig-dev
             Depends: libvpx-dev but it is not going to be installed
             Depends: libtiff-dev
 Nginx-extras-dbg : Depends: Nginx-extras (= 1.6.2-5+deb8u4) but it is not going to be installed
E: Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution).

apt-get -f install

root@e124bcecfddc:/# apt-get -f install
Reading package lists... Done
Building dependency tree
Reading state information... Done
Correcting dependencies... Done
The following extra packages will be installed:
  Nginx-common Nginx-extras
Suggested packages:
  fcgiwrap Nginx-doc ssl-cert
The following NEW packages will be installed:
  Nginx-common Nginx-extras
0 upgraded, 2 newly installed, 0 to remove and 10 not upgraded.
4 not fully installed or removed.
Need to get 683 kB of archives.
After this operation, 1812 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://security.debian.org/ jessie/updates/main Nginx-common all 1.6.2-5+deb8u4 [88.1 kB]
Get:2 http://security.debian.org/ jessie/updates/main Nginx-extras amd64 1.6.2-5+deb8u4 [595 kB]
Fetched 683 kB in 0s (1444 kB/s)
debconf: delaying package configuration, since apt-utils is not installed
(Reading database ... 9786 files and directories currently installed.)
Preparing to unpack .../Nginx-common_1.6.2-5+deb8u4_all.deb ...
Unpacking Nginx-common (1.6.2-5+deb8u4) ...
dpkg: error processing archive /var/cache/apt/archives/Nginx-common_1.6.2-5+deb8u4_all.deb (--unpack):
 trying to overwrite '/etc/Nginx/scgi_params', which is also in package Nginx 1.11.5-1~jessie
dpkg-deb: error: subprocess paste was killed by signal (broken pipe)
Preparing to unpack .../Nginx-extras_1.6.2-5+deb8u4_amd64.deb ...
Unpacking Nginx-extras (1.6.2-5+deb8u4) ...
dpkg: error processing archive /var/cache/apt/archives/Nginx-extras_1.6.2-5+deb8u4_amd64.deb (--unpack):
 trying to overwrite '/usr/sbin/Nginx', which is also in package Nginx 1.11.5-1~jessie
dpkg-deb: error: subprocess paste was killed by signal (broken pipe)
Errors were encountered while processing:
 /var/cache/apt/archives/Nginx-common_1.6.2-5+deb8u4_all.deb
 /var/cache/apt/archives/Nginx-extras_1.6.2-5+deb8u4_amd64.deb
E: Sub-process /usr/bin/dpkg returned an error code (1)

更新3

我已经清理了所有内容,删除了所有图像和容器,并从头开始.

我将这个docker-compose文件用于我的Nginx容器:

Nginx:
    image: Nginx
    build:
      context: ./env/Nginx
    ports:
      - '80:80'
    volumes:
      - ./env/Nginx/abmotors.conf:/etc/Nginx/conf.d/abmotors.conf:ro

这是位于./env/Nginx的Dockerfile(用于构建Nginx容器)

FROM Nginx

RUN apt-get update

RUN apt-get install --no-install-recommends --no-install-suggests -y \
                        libgd-dev \
                        Nginx-module-image-filter

一切都在积累,但当我运行并打入Nginx容器并运行Nginx -V时,它给了我以下内容:

root@7087db72bdb1:/# Nginx -V
Nginx version: Nginx/1.11.6
built by gcc 4.9.2 (Debian 4.9.2-10)
built with OpenSSL 1.0.1t  3 May 2016
TLS SNI support enabled
configure arguments: --prefix=/etc/Nginx --sbin-path=/usr/sbin/Nginx --modules-path=/usr/lib/Nginx/modules --conf-path=/etc/Nginx/Nginx.conf --error-log-path=/var/log/Nginx/error.log --http-log-path=/var/log/Nginx/access.log --pid-path=/var/run/Nginx.pid --lock-path=/var/run/Nginx.lock --http-client-body-temp-path=/var/cache/Nginx/client_temp --http-proxy-temp-path=/var/cache/Nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/Nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/Nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/Nginx/scgi_temp --user=Nginx --group=Nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-z,relro -Wl,-z,Now -Wl,--as-needed'

仍然没有关于libgd和image-filter模块的信息.

提前致谢.

解决方法:

Nginx在1.9.11中引入了动态模块,“image-filter”现在只是动态模块之一.

Nginx标准的Docker镜像已经提供了它:

$docker run -it --rm Nginx:mainline ls -l /etc/Nginx/modules/ | grep image
-rw-r--r-- 1 root root  23536 Oct 11 15:54 ngx_http_image_filter_module-debug.so
-rw-r--r-- 1 root root  23536 Oct 11 15:54 ngx_http_image_filter_module.so

但是模块没有被默认的Nginx根配置文件/etc/Nginx/Nginx.conf加载,因为它被认为是“可选的”.您需要做的是将以下行放入该文件中:

load_module /etc/Nginx/modules/ngx_http_image_filter_module.so;

在顶层!这就是定义守护进程,用户,http的地方.

Nginx 1.13.4 发布,新增 ngx_http_mirror_module 模块

Nginx 1.13.4 发布,新增 ngx_http_mirror_module 模块

Nginx 1.13.4 已发布,有以下的更新:

1.13.4 中的 ngx_http_mirror_module 模块通过创建后台镜像子请求实现了原始请求的镜像。镜像子请求的输出会被忽略。

配置示例

location / {
    mirror /mirror;
    proxy_pass http://backend;
}

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

Directives

Syntax: mirror uri | off;
Default:
mirror off;
Context: httpserverlocation

1.13.4 更新内容:

  • Feature: the ngx_http_mirror_module.

  • Bugfix: client connections might be dropped during configuration testing when using the "reuseport" parameter of the "listen" directive on Linux.

  • Bugfix: request body might not be available in subrequests if it was saved to a file and proxying was used.

  • Bugfix: cleaning cache based on the "max_size" parameter did not work on Windows.

  • Bugfix: any shared memory allocation required 4096 bytes on Windows.

  • Bugfix: nginx worker might be terminated abnormally when using the "zone" directive inside the "upstream" block on Windows.

下载地址

关于Nginx 模块 - ngx_http_rewrite_moduleNginx 模块开发指南的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于20、nginx 之 ngx_http_upstream_module 模块、21、nginx 之 ngx_http_proxy_module 模块、Docker Nginx ngx_http_image_filter_module、Nginx 1.13.4 发布,新增 ngx_http_mirror_module 模块等相关内容,可以在本站寻找。

本文标签: