文章出處

使用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_spdy_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模塊。

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


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜

    大師兄 發表在 痞客邦 留言(0) 人氣()