前言
在进行Nginx的配置的时候,必不可少的是进行调试,第三方的调试工具echo-nginx-module使用简单的命令echo得到了很多人的广泛采用,我们接上一篇文章在Ubuntu中利用Nginx进行多站点的配置的内容,这篇文一部分是对多站点配置的总结回顾,一部分是继续讲解Nginx有关知识,比如为使用apt install方式安装的Nginx增加第三方模块等。
调试模块的安装与使用(重新编译Nginx)
我之所以需要使用echo进行调试,是因为当我采用基于域名的虚拟主机之后,产生了登录某个网站后跳转完了没有带上:888这个端口号,当然不能访问啦(没备案),自己手动加上又可以了,于是我大胆猜测是在使用命令proxy_set_header Host $host这里出了问题,众所周知,Nginx做代理的时候不会传递真实的IP和主机名,所以我们手动添加字段,但是我猜想这里的$host返回的值只是baota.dfface.com,并没有带上:888。这样的猜想完全没有问题,但还是要实际检验,这就用到了第三方模块echo_nginx_module。
还记得我们之前在云主机上安装Nginx的简单粗暴的方式吗?对,就是apt install nginx,这种方式其实也是最推荐的方式,毕竟人家都帮你在/etc/init.do中写好了脚本。可以对比一下编译安装:在CentOS中通过源码编译安装Nginx。
在采用包管理器安装Nginx的情况下,如何扩展第三方模块成了一个问题!这里有前人的踩坑记录,我也是参考了他的:apt-get 方式安装nginx后,增加模块--with-http_perl_module。
- 备份原来的nginx,以防不可控问题
which nginx # 结果:/usr/sbin/nginx
cp /usr/sbin/nginx /usr/sbin/nginx.bk
- 查看原来的Nginx安装和配置信息
nginx -V
# 结果:
nginx version: nginx/1.10.3
built by gcc 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.11)
built with OpenSSL 1.0.2g 1 Mar 2016
TLS SNI support enabled
configure arguments: --with-cc-opt='-g -O2 -fPIE -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -fPIE -pie -Wl,-z,relro -Wl,-z,now' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-ipv6 --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_v2_module --with-http_sub_module --with-http_xslt_module --with-stream --with-stream_ssl_module --with-mail --with-mail_ssl_module --with-threads
在这里我们可以很清楚的看到Nginx的版本号,以及后面编译安装时的一些参数!这里比较有用的一个技巧就是不要选择最新版Nginx,这里的版本号是多少,我们就去官网nginx.org/下载这个版本,作者这里是:1.10.3。为什么要下载相同版本?防止出问题,我们毕竟是在人家的基础上扩展功能,不是追求最新版,追求最新版请移步编译安装:在CentOS中通过源码编译安装Nginx!
- 在官网下载对应版本的Nginx源码以及第三方模块
echo-nginx-module并解压

- 进入nginx-1.10.3中,开始重新编译生成nginx
技巧是直接复制之前nginx -V后面的配置信息configure arguments,在最后面加上--add-module=/root/nginx/echo-nginx-module即可:
./configure --with-cc-opt='-g -O2 -fPIE -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -fPIE -pie -Wl,-z,relro -Wl,-z,now' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-ipv6 --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_v2_module --with-http_sub_module --with-http_xslt_module --with-stream --with-stream_ssl_module --with-mail --with-mail_ssl_module --with-threads --add-module=/root/nginx/echo-nginx-module
# 上一个命令生成makefile,下一步就是编译生成Nginx
make
# 你没看错!只有make而没有make install,安装的话可能全部复制替换,你的所有配置文件可能就没了
提示,在./configure执行过程中可能会遇到问题,我们下载相关包之后再次./configure:
# the HTTP XSLT module requires the libxml2/libxslt libraries
sudo apt-get install libxml2 libxml2-dev libxslt-dev
# the HTTP image filter module requires the GD library.
sudo apt-get install libgd2-xpm libgd2-xpm-dev
# the GeoIP module requires the GeoIP library.
sudo apt-get install geoip-database libgeoip-dev
# the HTTP rewrite module requires the PCRE library.
apt-get install libpcre3 libpcre3-dev
- 替换
/usr/sbin/nginx
进入当前的**objs**目录,你会发现一个新的二进制可执行文件:nginx,用它替换/usr/sbin/nginx:
cp ./nginx /usr/sbin/nginx
- 大功告成,然后进行配置,载入配置,愉快测试吧
这里就不说啦,怎么调试可以看GitHub上的官方文件。
最后的结果就是我的猜想非常正确,proxy_set_header Host $host出了问题,这里的$host返回的值只是baota.dfface.com,并没有带上:888,因此解决方案就是命令改写为:proxy_set_header Host $host:888,完美解决啦!
dfface 的版权声明:所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处,严禁商业用途!