編譯nginx的源碼安裝subs_filter模塊

使用nginx的反向代理功能搭建nuget鏡像服務器時,須要針對官方nuget服務器的響應內容進行字符串替換,好比將www.nuget.org替換爲鏡像服務器的主機名,將https://替換爲http://。而nginx沒有內置這個功能,須要使用第三方module,好比subs_filter。node

在nginx中配置module,不像apache那麼簡單(複製module文件,修改配置文件),須要將module的源碼引入nginx的源碼,本身編譯nginx並安裝。nginx

下面分享一下本身在centos上編譯並安裝包含subs_filter模塊的nginx的實際操做步驟。git

0)若是centos上沒有安裝nginx,先用yum安裝一下,yum安裝時會自動添加一些nginx的初始配置文件,好比/etc/rc.d/init.d/nginx,/etc/nginx/nginx.conf(本身編譯安裝時不會添加這些配置文件)。github

yum install nginx

1)從 http://wiki.nginx.org/Install 的 #Source Releases 部分獲得nginx的源碼下載地址,下載解壓。web

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)。apache

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

(注:保存路徑爲/git/ngx_http_substitutions_filter_module)centos

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模塊。dom

注:若是出現下面的錯誤post

./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

相關文章
相關標籤/搜索