为nginx添加这些额外的第三方扩展加速你的web吧 Linux

nginx

Nginx 是一款高性能 Web 服务器软件,其有非常有益的IO表现,而且相较于 Apache Httpd 配置更加简单上手更加容易,本文将向大家介绍编译安装 Nginx 的第三方扩展。

Nginx 的额外扩展:

  1. OpenSSL 1.1.0,提供 ALPN 支持,支持 HTTP/2
  2. Nginx-CT,透明证书提高 HTTPS 网站的安全性和浏览器支持
  3. ngx_PageSpeed,Google 家的网站性能优化工具
  4. Brotli,实现比 Gzip 更高的压缩率
  5. Jemalloc,优化内存管理

第一个openssl和最后一个jemalloc现在大多数的一件安装包都有包括,不做描述.需要的请自行Google搜索相关教程.今天主要说一下Nginx-CT,ngx_PageSpeed,Brotli这三个扩展的安装.(建议把第三方扩展都放在一个文件夹下面方便管理)

下载需要的源码包:

wget https://github.com/grahamedgecombe/nginx-ct/archive/v1.3.2.tar.gz

tar xzf v1.3.2.tar.gz

git clone https://github.com/google/ngx_brotli.git
cd ngx_brotli
git submodule update --init

wget https://github.com/pagespeed/ngx_pagespeed/archive/v1.12.34.3-stable.zip
unzip v1.12.34.3-stable.zip
cd ngx_pagespeed-1.12.34.3-stable/
wget https://dl.google.com/dl/page-speed/psol/1.12.34.2-x64.tar.gz
tar -xzvf 1.12.34.2-x64.tar.gz

接下来就是编译了,这是在已经安装好nginx的环境增加扩展,所有只需要make 不需要make install ,不然就覆盖安装了...

linux下错误提示很人性化的,多看提示,更具提示操作,基本上都可以解决问题.

重要步骤:

通过命令 nginx -V 查看nginx的配置及其已有的扩展.我们只需要在后面增加模块即可:

比如使用nginx -V命令查看结果类似于下面:

--prefix=/usr/local/nginx --with-http_stub_status_module

我们只需要在原有的基础上添加我们的模块即可:

./configure --prefix=/usr/local/nginx --with-http_stub_status_module --add-module=/data/software/nginx-ct-1.3.2 --add-module=/data/software/ngx_brotli --add-module=/data/software/ngx_pagespeed-1.12.34.3-stable

等它自动配置完成后,直接 make 一下就行.

然后备份原有已安装好的nginx:

cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
service nginx stop

然后将刚刚编译好的nginx覆盖掉原有的nginx(这个时候nginx要停止状态,通过上面的命令,已经停止了):

cp ./objs/nginx /usr/local/nginx/sbin/

然后启动nginx (service nginx start),就可以通过命令nginx -V 查看第三方扩展是否已经加入成功.

PS:笔记+分享,好的扩展,开源产品大家都应该去体验一下!

标签: nginx 编译

admin 发布于  2017-10-1 17:06 

两种方式反代Google(镜像)--nginx反代和nginx扩展 Linux

写这篇文章的缘由是看见了我的博友Secret他写了一篇文章:

造轮子之谷歌镜像站 让我想起了 之前自己折腾过的nginx扩展镜像Google,效率比这个高,而且支持高级的配置,多级配合组成类似集群的功能,今天又折腾了一下,所以写一下过程,以方便后来需要的人.

声明:请在法律允许范围内合理使用搜索引擎,本文只作为技术笔记,不负任何责任.


  1. 更新库
  2. apt-get update
  3.  
  4. # 安装 gcc & git
  5. apt-get install build-essential git gcc g++ make -y
  6.  
  7. # nginx 官网: http://nginx.org/en/download.html
  8. wget "http://nginx.org/download/nginx-1.8.1.tar.gz"
  9.  
  10. # pcre 官网:http://www.pcre.org/
  11. wget "ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.39.tar.gz"
  12.  
  13. # opessl 官网:https://www.openssl.org/
  14. wget "https://www.openssl.org/source/openssl-1.0.1t.tar.gz"
  15.  
  16. # zlib 官网:http://www.zlib.net/
  17. wget "http://zlib.net/zlib-1.2.8.tar.gz"
  18.  
  19. # 下载本扩展
  20. git clone https://github.com/cuber/ngx_http_google_filter_module
  21.  
  22. # 下载 substitutions 扩展
  23. git clone https://github.com/yaoweibin/ngx_http_substitutions_filter_module
  24.  
  25. # 解压缩
  26. tar xzvf nginx-1.8.1.tar.gz && tar xzvf pcre-8.39.tar.gz && tar xzvf openssl-1.0.1t.tar.gz && tar xzvf zlib-1.2.8.tar.gz
  27.  
  28. # 进入 nginx 源码目录
  29. cd nginx-1.8.1
  30.  
  31. # 创建 nginx 安装目录
  32. mkdir /usr/local/nginx-1.8.1

编译nginx及其扩展

  1. # 设置编译选项
  2. ./configure \
  3. --prefix=/usr/local/nginx-1.8.1 \
  4. --with-pcre=../pcre-8.39 \
  5. --with-openssl=../openssl-1.0.1t \
  6. --with-zlib=../zlib-1.2.8 \
  7. --with-http_ssl_module \
  8. --add-module=../ngx_http_google_filter_module \
  9. --add-module=../ngx_http_substitutions_filter_module
  10.  
  11. # 编译, 安装
  12. # 如果扩展有报错, 请发 issue 到
  13. # https://github.com/cuber/ngx_http_google_filter_module/issues
  14. make
  15. make install

 最后启动nginx,访问你的服务器IP或者是解析到上面的域名,即可看到nginx是否安装好.

ngx_http_google_filter_module项目github地址(他那里也有说明,不过是英文的,能看懂的可以直接去看原文):

https://github.com/cuber/ngx_http_google_filter_module


下面说一下nginx的配置:

  1. 简单的单机配置https,已经不支持http反代了
  1. server {
  2. server_name <你的域名>;
  3. listen 443;
  4.  
  5. ssl on;
  6. ssl_certificate <你的证书>;
  7. ssl_certificate_key <你的私钥>;
  8.  
  9. resolver 8.8.8.8;
  10. location / {
  11. google on;
  12. }
  13. }
  1. 进阶配置:配置多个服务器来缓解并发和出现验证码的频率

google_scholar 依赖于 google, 所以 google_scholar 无法独立使用. 由于谷歌学术近日升级, 强制使用 https 协议, 并且 ncr 已经支持, 所以不再需要指定谷歌学术的 tld

  1. location / {
  2. google on;
  3. google_scholar on;
  4. # 设置成德文,默认的语言是中文简体
  5. google_language "de";
  6. }

Upstreaming

upstream 减少一次域名解析的开销, 并且通过配置多个网段的 google ip 能够一定程度上减少被 google 机器人识别程序侦测到的几率 (弹验证码). upstream 参数要放在 http{} 中(也就是放在server{}配置外),注意这个参数只有你加了SSL证书是https的时候才会有效,否则会报错! 寻找这个参数的谷歌IP很简单,在你的VPS上面 ping www.google.com ,获得的IP把最后一位数 加1或者减1 就行了。

upstream www.google.com {
  server 173.194.38.1:443;
  server 173.194.38.2:443;
  server 173.194.38.3:443;
  server 173.194.38.4:443;
}

Proxy Protocol--代理保护

默认情况下,代理将使用https与后端服务器通信。您可以使用google_ssl_off强制某些域名回退到http协议。如果要通过没有ssl证书的另一个网关来代理某些域,这是非常有用的。

#
# eg. 
# i want to proxy the domain 'www.google.com' like this
# vps(hk) -> vps(us) -> google
#

#
# configuration of vps(hk)
#
server {
  # ...
  location / {
    google on;
    google_ssl_off "www.google.com";
  }
  # ...
}

upstream www.google.com {
  server < ip of vps(us) >:80;
}

#
# configuration of vps(us)
#
server {
  listen 80;
  server_name www.google.com;
  # ...
  location / {
    proxy_pass https://www.google.com;
  }
  # ...
}

所有的这些配置都是在全新的机器上配置,如果你已经配置好了nginx那么,也很容易,你只需要重新添加扩展动态编译进去就好了,编译完切记不要make install,只需要make编译,然后覆盖就行.

./configure \
--prefix=/usr/local/nginx \
--user=www \
--group=www \
--with-http_stub_status_module \
--with-http_v2_module \
--with-http_ssl_module \
--with-http_gzip_static_module \
.......
--add-module=/data/software/ngx_http_google_filter_module \
--add-module=/data/software/ngx_http_substitutions_filter_module

注意:你需要在nginx的源码包文件夹下面执行这个./configure命令,使用之前先使用 nginx-V 查看nginx版本下载相同版本的源码包,添加扩展的时候要注意路径,在最好复制的时候先停止nginx,同时以防万一,先拷贝一份nginx在覆盖.参考资料如下:

http://imshusheng.com/linux/173.html

http://www.ttlsa.com/nginx/how-to-install-nginx-third-modules/

http://coolnull.com/4245.html

就到这里了.下次再见:) 最后 欢迎访问我的Google镜像:gg.mrxn.net


admin 发布于  2017-9-24 07:38 

error while loading shared libraries: libsodium.so.18: cannot open shared Linux

昨晚在部署环境编译pureFTP的时候,报错:

error while loading shared libraries: libsodium.so.18: cannot open shared

百度,Google一阵总算找到解决方法:

首先确认libsodium.so.18在你得系统里是否存在,直接一行命令就搞定:

cat /etc/ld.so.conf

include ld.so.conf.d/*.conf

/lib

/usr/lib

/usr/lib64

/usr/local/lib

或者是下面的find命令:

find / -name "libsodium.so.18" ,我的系统返回的是存在的,一般的系统都存在的....

/usr/lib/libsodium.so.18 ...那既然存在好报错,那就软连接到另一个 libsodium.so 上,具体命令如下:

ln -sf /usr/lib/libsodium.so /usr/lib/libsodium.so.18

提示:开始没有加f参数,失败,File exists ,加上f强制软连接.OK

最后一定要记得

ldconfig

是配置生效,再去编译一般就OK啦,不对再继续找原因.

注:参考链接--

http://www.jb51.net/LINUXjishu/268747.html


下面记录一下.使用acme.sh安装ssl证书时需要注意的,按照作者的步骤clone或者wget,curl且运行完脚本后,请reboot一下vps,

不然你执行acme.sh --可能会提示找不到命令,如果不重启,也可以,切换到acme.sh的安装目录,

cd /root/.acme.sh/ ,然后执行, ./acme.sh --参数 .推荐重启一下,方便.

如果在安装证书过程中出错,Verifying invalid这类的最好就换一种方式验证域名.作者那里有提到.

在安装证书前,域名切记不要被Google或者是火狐报毒...不然是不能通过的,不能颁发证书的.更多的使用方法去作者的github主页看,很详细.


admin 发布于  2017-3-20 15:16