WebEnh

.net7 mvc jquery bootstrap json 学习中 第一次学PHP,正在研究中。自学进行时... ... 我的博客 https://enhweb.github.io/ 不错的皮肤:darkgreentrip,iMetro_HD
  首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

编译nginx的源码安装subs_filter模块

Posted on 2019-10-27 13:51  WebEnh  阅读(1039)  评论(0编辑  收藏  举报

使用nginx的反向代理功能搭建nuget镜像服务器时,需要针对官方nuget服务器的响应内容进行字符串替换,比如将www.nuget.org替换为镜像服务器的主机名,将https://替换为http://。而nginx没有内置这个功能,需要使用第三方module,比如subs_filter。

在nginx中配置module,不像apache那么简单(复制module文件,修改配置文件),需要将module的源码引入nginx的源码,自己编译nginx并安装。

下面分享一下自己在centos上编译并安装包含subs_filter模块的nginx的实际操作步骤。

0)如果centos上没有安装nginx,先用yum安装一下,yum安装时会自动添加一些nginx的初始配置文件,比如/etc/rc.d/init.d/nginx,/etc/nginx/nginx.conf(自己编译安装时不会添加这些配置文件)。

yum install nginx

1)从 http://wiki.nginx.org/Install 的 #Source Releases 部分得到nginx的源码下载地址,下载解压。

wget http://nginx.org/download/nginx-1.8.0.tar.gz 
tar xf nginx-1.8.0.tar.gz

2)git签出subs_filter的源码(参考 nginx_substitutions_filter)。

git clone git://github.com/yaoweibin/ngx_http_substitutions_filter_module.git

(注:保存路径为/git/ngx_http_substitutions_filter_module)

3)nginx编译配置

复制代码
./configure --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module  --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module --add-module=/git/ngx_http_substitutions_filter_module
复制代码

最后的--add-module就是引入的subs_filter模块。

注:如果出现下面的错误

./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.

需要安装  libpcre3

apt-get install libpcre3 libpcre3-dev

4)编译并安装nginx

make && make install

5)在/etc/nginx/nginx.config中配置subs_filter

复制代码
server {
    listen       80;
    listen       [::]:80;
    server_name  [mirror_host_name];

    location / {
        proxy_pass http://www.nuget.org;
        proxy_cache nuget-cache;
        proxy_cache_valid 168h;
        proxy_ignore_headers Set-Cookie Cache-Control;
        subs_filter www.nuget.org [mirror_host_name];
        subs_filter https:// http://;
    }
}
复制代码

5)重启nginx服务

systemctl restart nginx

搞定!

【参考资料】

CentOS - Installing nginx from source

Websites with Nginx on CentOS 5