使用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
本博客Android APP 下载 |
![]() |
支持我们就给我们点打赏 |
![]() |
支付宝打赏 支付宝扫一扫二维码 |
![]() |
微信打赏 微信扫一扫二维码 |
![]() |
如果想下次快速找到我,记得点下面的关注哦!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
2016-10-27 BitTorrent Sync - 神奇的文件同步软件,无需服务器让多台电脑互相同步!