如何更好的手工编译安装Nginx


=Start=

缘由:

为了能在Nginx上使用一些额外的功能/模块,一般都是需要手工编译安装Nginx的,这对于专门研究Nginx或对此比较熟悉的人来说不算麻烦,但对于我这种新手来说就非常麻烦了——如果只是简单的设置「--prefix」、「--with-pcre」等几个编译选项的话,我也会一点,但如果考虑得不全面的话后期还需要重新编译,但该添加哪些选项我还真不清楚;还有就是如果完全重头开始编译Nginx的话,用户、组的创建,启动脚本的编写等都是需要自己来处理的,这也是我为什么一直都比较忌惮从头编译Nginx等系统软件的原因,需要考虑的问题太多了。

但今天在测试一个Nginx模块的时候学到了一个比较好的办法,自己实际在 CentOS 7 测试了一下,挺好用的。于是记录一下,方便以后参考。

正文:

参考解答:

完全从头开始指定Nginx的编译选项对我来说显然不现实,一个更好的办法是参照系统的编译选项,然后根据实际需要增加几个「--add-module」选项即可很好的结合二者的优点(方便快速&可定制):

# 首先用yum的方式在CentOS 7上安装Nginx及需要的模块
# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
# yum install nginx nginx-module-xslt nginx-module-perl nginx-module-image-filter nginx-module-geoip

# 然后通过「-V」选项查看yum命令在安装时给它设置了哪些编译选项
# nginx -V

&

# 创建一个单独的目录用于存放编译Nginx需要的软件
$ mkdir compile_nginx
$ cd compile_nginx

$ git clone https://github.com/cuber/ngx_http_google_filter_module
$ git clone https://github.com/yaoweibin/ngx_http_substitutions_filter_module

$ wget https://github.com/pagespeed/ngx_pagespeed/archive/v1.12.34.2-beta.tar.gz
$ mv v1.12.34.2-beta.tar.gz ngx_pagespeed.1.12.34.2-beta.tar.gz
$ tar zxf ngx_pagespeed.1.12.34.2-beta.tar.gz

# 一般建议是下载和yum命令安装的Nginx相同版本的进行重新编译,不过如果你想体验新版本的Nginx,也可以先编译试试,一般也没什么问题
$ wget http://nginx.org/download/nginx-1.11.7.tar.gz
$ tar zxf nginx-1.11.7.tar.gz
$ cd nginx-1.11.7
# 使用上面的「nginx -V」命令给出的编译选项,然后在末尾添加上自己需要的选项
$ ./configure \
 --prefix=/etc/nginx \
 --sbin-path=/usr/sbin/nginx \
 --modules-path=/usr/lib64/nginx/modules \
 --conf-path=/etc/nginx/nginx.conf \
 --error-log-path=/var/log/nginx/error.log \
 --http-log-path=/var/log/nginx/access.log \
 --pid-path=/var/run/nginx.pid \
 --lock-path=/var/run/nginx.lock \
 --http-client-body-temp-path=/var/cache/nginx/client_temp \
 --http-proxy-temp-path=/var/cache/nginx/proxy_temp \
 --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
 --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
 --http-scgi-temp-path=/var/cache/nginx/scgi_temp \
 --user=nginx \
 --group=nginx \
 --with-file-aio \
 --with-threads \
 --with-http_addition_module \
 --with-http_auth_request_module \
 --with-http_dav_module \
 --with-http_flv_module \
 --with-http_gunzip_module \
 --with-http_gzip_static_module \
 --with-http_mp4_module \
 --with-http_random_index_module \
 --with-http_realip_module \
 --with-http_secure_link_module \
 --with-http_slice_module \
 --with-http_ssl_module \
 --with-http_stub_status_module \
 --with-http_sub_module \
 --with-http_v2_module \
 --with-mail \
 --with-mail_ssl_module \
 --with-stream \
 --with-stream_ssl_module \
 --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic' \
 --add-module=../ngx_http_google_filter_module \
 --add-module=../ngx_http_substitutions_filter_module \
 --add-module=../ngx_pagespeed-1.12.34.2-beta
$ make

$ sudo cp /usr/sbin/nginx{,.orig}
$ sudo cp -rf objs/nginx /usr/sbin/nginx
参考链接:

=END=

, ,

《“如何更好的手工编译安装Nginx”》 有 13 条评论

  1. 如果在VPS上搭建了基于Nginx做WebServer的网站,最好将Nginx等服务设置成开机自启动:
    `
    # CentOS 7
    # systemctl enable nginx
    # systemctl enable php-fpm
    # systemctl enable mysqld

    # CentOS 6
    # chkconfig –level 345 nginx on
    # chkconfig –level 345 php-fpm on
    # chkconfig –level 345 mysqld on
    `

  2. Nginx最新模块—ngx_http_mirror_module分析
    https://mp.weixin.qq.com/s?__biz=MzIxNzg5ODE0OA==&mid=2247483708&idx=1&sn=90b0b1dccd9c337922a0588245277666
    http://nginx.org/en/CHANGES
    `
    最近Nginx官网公布了Nginx 1.13.4最新的 ngx_http_mirror_module 模块,利用此模块,业务可以将线上实时访问流量拷贝至其他环境,基于这些流量可以做版本发布前的预先验证,进行流量放大后的压测等等。

    ngx_http_mirror_module 模块配置分为两部分,源地址(original)和镜像地址(mirror)配置。根据此文章的源码分析来看,在整个流程中,Nginx将请求转发送至original和mirror,然后等待响应,几乎不会对正常请求造成影响,整个处理过程是完全异步的。
    `

  3. HTTP2简介和nginx中开启HTTP2
    https://www.ipcpu.com/2017/11/http2-nginx/
    `
    在 Nginx 上 开启 HTTP/2 需要 Nginx 1.9.5 以上版本,并且需要 OpenSSL 版本在 1.0.2 以上。

    从Nginx 1.9.5 开始,http_v2_module 已经替换了ngx_http_spdy_module,全面支持HTTP/2协议。默认没有放进编译参数,需要编译参数 –with-http_v2_module 开启。

    可以用 nginx -V 查看。

    确保支持http2后,nginx配置作如下修改:
    server {
    listen 443 ssl http2;
    server_name ixyzero.com;

    验证/检测HTTP2是否开启:
    chrome://net-internals/#http2
    Chrome插件「HTTP/2 and SPDY indicator」
    Chrome控制台
    `

  4. Nginx在用yum命令进行升级之后,相关目录的用户属主和权限就会重置(可能变得和自己设置的不太一样,比如从apache变成nginx),所以会导致一些比较诡异的状态:
    WordPress可以新增「小文章」,但不能添加「大文章」(涉及到临时目录的访问)
    `
    # tail -f /var/log/nginx/error.log

    2018/02/11 16:51:41 [crit] 19581#0: *6 open() “/var/lib/nginx/tmp/client_body/0000000001” failed (13: Permission denied), client: 127.0.0.1, server: ixyzero.com, request: “POST /blog/wp-admin/admin-ajax.php HTTP/2.0”, host: “ixyzero.com”, referrer: “https://ixyzero.com/blog/wp-admin/post-new.php”

    # ps aux | grep nginx

    # ls -lt /var/log/nginx/
    # chown -R apache:apache /var/log/nginx/*.log

    # ls -lt /var/lib/nginx/
    # chown -R apache:apache /var/lib/nginx/
    `

  5. Nginx请求处理流程你了解吗?
    https://mp.weixin.qq.com/s/otQIhuLABU3omOLtRfJnZQ
    `
    nginx 11 个处理阶段
    1)NGX_HTTP_POST_READ_PHASE:
    接收到完整的HTTP头部后处理的阶段,它位于uri重写之前,实际上很少有模块会注册在该阶段,默认的情况下,该阶段被跳过。
    2)NGX_HTTP_SERVER_REWRITE_PHASE:
    URI与location匹配前,修改URI的阶段,用于重定向,也就是该阶段执行处于server块内,location块外的重写指令,在读取请求头的过程中nginx会根据host及端口找到对应的虚拟主机配置。
    3)NGX_HTTP_FIND_CONFIG_PHASE:
    根据URI寻找匹配的location块配置项阶段,该阶段使用重写之后的uri来查找对应的location,值得注意的是该阶段可能会被执行多次,因为也可能有location级别的重写指令。
    4)NGX_HTTP_REWRITE_PHASE:
    上一阶段找到location块后再修改URI,location级别的uri重写阶段,该阶段执行location基本的重写指令,也可能会被执行多次。
    5)NGX_HTTP_POST_REWRITE_PHASE:
    防止重写URL后导致的死循环,location级别重写的后一阶段,用来检查上阶段是否有uri重写,并根据结果跳转到合适的阶段。
    6)NGX_HTTP_PREACCESS_PHASE:
    下一阶段之前的准备,访问权限控制的前一阶段,该阶段在权限控制阶段之前,一般也用于访问控制,比如限制访问频率,链接数等。
    7)NGX_HTTP_ACCESS_PHASE:
    让HTTP模块判断是否允许这个请求进入Nginx服务器,访问权限控制阶段,比如基于ip黑白名单的权限控制,基于用户名密码的权限控制等。
    8)NGX_HTTP_POST_ACCESS_PHASE:
    访问权限控制的后一阶段,该阶段根据权限控制阶段的执行结果进行相应处理,向用户发送拒绝服务的错误码,用来响应上一阶段的拒绝。
    9)NGX_HTTP_TRY_FILES_PHASE:
    为访问静态文件资源而设置,try_files指令的处理阶段,如果没有配置try_files指令,则该阶段被跳过。
    10)NGX_HTTP_CONTENT_PHASE:
    处理HTTP请求内容的阶段,大部分HTTP模块介入这个阶段,内容生成阶段,该阶段产生响应,并发送到客户端。
    11)NGX_HTTP_LOG_PHASE:
    处理完请求后的日志记录阶段,该阶段记录访问日志。

    以上11个阶段中,HTTP无法介入的阶段有4个:
    3)NGX_HTTP_FIND_CONFIG_PHASE
    5)NGX_HTTP_POST_REWRITE_PHASE
    8)NGX_HTTP_POST_ACCESS_PHASE
    9)NGX_HTTP_TRY_FILES_PHASE
    剩余的7个阶段,HTTP模块均能介入,每个阶段可介入模块的个数也是没有限制的,多个HTTP模块可同时介入同一阶段并作用于同一请求。
    `

  6. 如何在 OpenResty 上使用 HTTP2 的功能?
    `
    一般而言,如果你只是把 OpenResty 当 Nginx 用,那基本没啥区别,只需要在编译的时候添加如下选项即可:
    –with-http_v2_module

    但是,如果你要在用 OpenResty 时,享受 HTTP/2 协议的网络性能优化,且借助它实现自己的一些编码开发功能,就需要注意:
    ngx.req.raw_header()
    ngx.location.capture*
    因为 OpenResty 官方并没有宣称完整支持HTTP/2协议,所以实际使用开发支持时可能会有坑。上面这两个是已经在github上 openresty 的 issue 里明确提到的在 HTTP/2 下有兼容性问题的API,需要规避(使用其它没问题的API替代或是绕过);更多其它的API还需要根据你在日常工作中使用到的API进行整理,同时借鉴其它知名的基于 OpenResty 开发出的应用在支持 HTTP/2 时所采取的方案。
    `

    HTTP2 support #566
    https://github.com/openresty/lua-nginx-module/issues/566
    `
    @alubbe opened this issue on 11 Sep 2015
    @agentzh agentzh closed this on 19 Aug 2016

    Yeah, certain features are still problematic in HTTP/2. We’ll work on them.
    `

  7. HTTP/2 requests to Lua scripts fail when using ‘lua_check_client_abort on’ #724
    https://github.com/openresty/lua-nginx-module/issues/724
    `
    HTTP/2 has not yet been officially supported by this module. Needs to do a lot of work on our side.
    `

    ngx.req.raw_header() err http v2 not supported yet #979
    https://github.com/openresty/lua-nginx-module/issues/979

    http v2 not supported yet #1186
    https://github.com/openresty/lua-nginx-module/issues/1186
    `
    Just use the ngx.req.get_headers() API function for HTTP/2 requests instead of ngx.req.raw_header().
    `
    https://github.com/openresty/lua-nginx-module/#ngxreqget_headers

    Support for HTTP/2 #574
    https://github.com/Kong/kong/issues/574

    refactor(plugins) removing ngx.req.raw_header() for HTTP/2 compatibility #2540
    https://github.com/Kong/kong/pull/2540

    https://openresty.org/en/changelog-1013006.html
    `
    bugfix: we now throw a Lua exception when ngx.location.capture* Lua API is used inside an HTTP2 request since it is known to lead to hanging.
    `
    OpenResty 完全开发指南
    https://wiki.shileizcc.com/confluence/pages/viewpage.action?pageId=47416504

    https://www.wanglibing.com/cn/posts/openresty/

回复 a-z 取消回复

您的电子邮箱地址不会被公开。 必填项已用*标注