Nginx源码安装及调优配置(一)

简介:

Nginx编译前的优化

[root@linuxprobe ~]# wget http://nginx.org/download/nginx-1.10.1.tar.gz
[root@linuxprobe ~]# tar xvf nginx-1.10.1.tar.gz -C /usr/local/src/
[root@linuxprobe ~]# cd /usr/local/src/nginx-1.10.1/

编译前的优化主要是用来修改程序名等等,例如:

[root@linuxprobe nginx-1.10.1]# curl -I http://www.baidu.com
……
Server: bfe/1.0.8.14
……
[root@linuxprobe nginx-1.10.1]# curl -I http://www.sina.com.cn
……
Server: nginx
……
[root@linuxprobe nginx-1.10.1]# curl -I http://www.linuxprobe.com
HTTP/1.1 200 OK
Server: nginx/1.10.1 #我们目标是将nginx更改名字
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.29
Set-Cookie: PHPSESSID=smm0i6u4f9v7bj0gove79ja1g7; path=/
Cache-Control: no-cache
Date: Mon, 07 Seq 2016 06:09:11 GMT
[root@linuxprobe nginx-1.10.1]# vim src/core/nginx.

  • 目的更改源码隐藏软件名称和版本号

#define NGINX_VERSION "nginx_stable" #此行修改的是你想要的版本号

#define NGINX_VER "linuxprobe/" NGINX_VERSION #此行修改的是你想修改的软件名称

[root@linuxprobe nginx-1.10.1]# vim +49 src/http/ngx_http_header_filter_module.c

  • 修改HTTP头信息中的connection字段,防止回显具体版本号

拓展:通用http头域

通用头域包含请求和响应消息都支持的头域,通用头域包含Cache-Control、 Connection、Date、Pragma、Transfer-Encoding、Upgrade、Via。对通用头域的扩展要求通讯双方都支持此扩展,如果存在不支持的通用头域,一般将会作为实体头域处理。那么也就是说有部分设备,或者是软件,能获取到connection,部分不能,要隐藏就要彻底!

static char ngx_http_server_string[] = "Server: LinuxprobeWeb" CRLF;

[root@linuxprobe nginx-1.10.1]# vim +29 src/http/ngx_http_special_response.c

  • 定义了http错误码的返回

有时候我们页面程序出现错误,Nginx会代我们返回相应的错误代码,回显的时候,会带上nginx和版本号,我们把他隐藏起来

static u_char ngx_http_error_full_tail[] ="<hr><center>" NGINX_VER "</center>" CRLF
"</body>" CRLF
"</html>" CRLF;

修改后

static u_char ngx_http_error_tail[] ="<hr><center>LinuxprobeWeb</center>" CRLF
"</body>" CRLF
"</html>" CRLF;

Nginx正式安装

一键安装相关依赖包

[root@linuxprobe nginx-1.10.1]# yum install gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel -y

安装pcre依赖

#本地下载pcre上传到服务器

[root@linuxprobe]# tar zxvf /usr/local/src/pcre-8.36.tar.gz -C /usr/local/src/
[root@linuxprobe nginx-1.10.1]# cd  /usr/local/src/pcre-8.36
[root@linuxprobe nginx-1.10.1]# ./configure && make && make install
[root@linuxprobe nginx-1.10.1]# ./configure --prefix=/usr/local/nginx --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-pcre=/usr/local/src/pcre-8.36 --with-openssl=/usr/include/openssl
注意:TCP_FASTOPEN 只在 3.7.1 以及更新的 Linux 内核版本才支持
--with-http_dav_module             #启用支持(增加PUT,DELETE,MKCOL:创建集合,COPY和MOVE方法)默认关闭,需要编译开启
--with-http_stub_status_module     #启用支持(获取Nginx上次启动以来的工作状态)
--with-http_addition_module        #启用支持(作为一个输出过滤器,支持不完全缓冲,分部分相应请求)
--with-http_sub_module             #启用支持(允许一些其他文本替换Nginx相应中的一些文本)
--with-http_flv_module             #启用支持(提供支持flv视频文件支持)
--with-http_mp4_module             #启用支持(提供支持mp4视频文件支持,提供伪流媒体服务端支持)
--with-pcre=/usr/local/src/pcre-8.36            #需要注意,这里指的是源码,用#./configure --help |grep pcre查看帮助
[root@linuxprobe nginx-1.10.1]# make && make install
#Nginx安装路径。如果没有指定,默认为 /usr/local/nginx。
--prefix=PATH       #Nginx可执行文件安装路径。只能安装时指定,如果没有指定,默认为PATH/sbin/nginx。
--sbin-path=PATH    #在没有给定-c选项下默认的nginx.conf的路径。如果没有指定,默认为PATH/conf/nginx.conf。 
--conf-path=PATH    #在nginx.conf中没有指定pid指令的情况下,默认的nginx.pid的路径。如果没有指定,默认为 PATH/logs/nginx.pid。
--pid-path=PATH     #nginx.lock文件的路径。
--lock-path=PATH    #在nginx.conf中没有指定error_log指令的情况下,默认的错误日志的路径。如果没有指定,默认为 PATH/logs/error.log。
--error-log-path=PATH    #在nginx.conf中没有指定access_log指令的情况下,默认的访问日志的路径。如果没有指定,默认为 PATH/logs/access.log。
--http-log-path=PATH     #在nginx.conf中没有指定user指令的情况下,默认的nginx使用的用户。如果没有指定,默认为 nobody。
--user=USER       #在nginx.conf中没有指定user指令的情况下,默认的nginx使用的组。如果没有指定,默认为 nobody。
--group=GROUP     #指定编译的目录
--builddir=DIR    #启用 rtsig 模块
--with-rtsig_module    #允许或不允许开启SELECT模式,如果configure没有找到合适的模式,比如,kqueue(sun os)、epoll(linux kenel 2.6+)、rtsig(实时信号)
--with-select_module(--without-select_module)    #允许或不允许开启POLL模式,如果没有合适的,则开启该模式。
--with-poll_module(--without-poll_module)        #开启HTTP SSL模块,使NGINX可以支持HTTPS请求。这个模块需要已经安装了OPENSSL,在DEBIAN上是libssl-dev

--with-http_ssl_module         #启用ngx_http_ssl_module
--with-http_realip_module      #启用 ngx_http_realip_module
--with-http_addition_module    #启用 ngx_http_addition_module
--with-http_sub_module    #启用 ngx_http_sub_module
--with-http_dav_module    #启用 ngx_http_dav_module
--with-http_flv_module    #启用 ngx_http_flv_module
--with-http_stub_status_module    #启用 "server status" 页
--without-http_charset_module     #禁用 ngx_http_charset_module
--without-http_gzip_module        #禁用 ngx_http_gzip_module. 如果启用,需要 zlib 。
--without-http_ssi_module         #禁用 ngx_http_ssi_module
--without-http_userid_module      #禁用 ngx_http_userid_module
--without-http_access_module      #禁用 ngx_http_access_module
--without-http_auth_basic_module  #禁用 ngx_http_auth_basic_module
--without-http_autoindex_module   #禁用 ngx_http_autoindex_module
--without-http_geo_module         #禁用 ngx_http_geo_module
--without-http_map_module         #禁用 ngx_http_map_module
--without-http_referer_module     #禁用 ngx_http_referer_module
--without-http_rewrite_module     #禁用 ngx_http_rewrite_module. 如果启用需要 PCRE 。
--without-http_proxy_module       #禁用 ngx_http_proxy_module
--without-http_fastcgi_module     #禁用 ngx_http_fastcgi_module
--without-http_memcached_module   #禁用 ngx_http_memcached_module
--without-http_limit_zone_module  #禁用 ngx_http_limit_zone_module
--without-http_empty_gif_module   #禁用 ngx_http_empty_gif_module
--without-http_browser_module     #禁用 ngx_http_browser_module
--without-http_upstream_ip_hash_module   #禁用 ngx_http_upstream_ip_hash_module
--with-http_perl_module -         #启用 ngx_http_perl_module
--with-perl_modules_path=PATH     #指定 perl 模块的路径
--with-perl=PATH        #指定 perl 执行文件的路径
--http-log-path=PATH    #Set path to the http access log
--http-client-body-temp-path=PATH    #Set path to the http client request body temporary files
--http-proxy-temp-path=PATH          #Set path to the http proxy temporary files
--http-fastcgi-temp-path=PATH        #Set path to the http fastcgi temporary files
--without-http                 #禁用 HTTP server
--with-mail                    #启用 IMAP4/POP3/SMTP 代理模块
--with-mail_ssl_module         #启用 ngx_mail_ssl_module
--with-cc=PATH                 #指定 C 编译器的路径
--with-cpp=PATH                #指定 C 预处理器的路径
--with-cc-opt=OPTIONS #
--with-ld-opt=OPTIONS #Additional parameters passed to the linker. With the use of the system library PCRE in FreeBSD, it is necessary to indicate --with-ld-opt="-L /usr/local/lib".
--with-cpu-opt=CPU        #为特定的CPU编译,有效的值包括:pentium, pentiumpro, pentium3, pentium4, athlon, opteron, amd64, sparc32, sparc64, ppc64
--without-pcre            #禁止PCRE库的使用。同时也会禁止 HTTP rewrite 模块。在 "location" 配置指令中的正则表达式也需要 PCRE 。
--with-pcre=DIR           #指定 PCRE 库的源代码的路径。
--with-pcre-opt=OPTIONS   #设置PCRE的额外编译选项。
--with-md5=DIR            #使用MD5汇编源码。
--with-md5-opt=OPTIONS    #Set additional options for md5 building.
--with-md5-asm            #Use md5 assembler sources.
--with-sha1=DIR           #Set path to sha1 library sources.
--with-sha1-opt=OPTIONS   #Set additional options for sha1 building.
--with-sha1-asm           #Use sha1 assembler sources.
--with-zlib=DIR           #Set path to zlib library sources.
--with-zlib-opt=OPTIONS   #Set additional options for zlib building.
--with-zlib-asm=CPU       #Use zlib assembler sources optimized for specified CPU, valid values are: pentium, pentiumpro
--with-openssl=DIR        #Set path to OpenSSL library sources
--with-openssl-opt=OPTIONS #Set additional options for OpenSSL building
--with-debug              #启用调试日志
--add-module=PATH         #Add in a third-party module found in directory PATH

启动nginx

[root@linuxprobe nginx-1.10.1]# /usr/local/nginx/sbin/nginx
[root@linuxprobe nginx-1.10.1]# netstat -antup | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 52553/nginx

测试是否隐藏了版本和软件名

[root@linuxprobe nginx-1.10.1]# cd
[root@linuxprobe ~]# curl -I http://127.0.0.1
相关文章
|
10天前
|
移动开发 前端开发 JavaScript
前端vue2、vue3去掉url路由“ # ”号——nginx配置(一)
前端vue2、vue3去掉url路由“ # ”号——nginx配置
37 0
|
10天前
|
JavaScript 前端开发 应用服务中间件
angular引入包、路由权限配置、打包问题与nginx配置问题(简单部署)
angular引入包、路由权限配置、打包问题与nginx配置问题(简单部署)
17 0
|
10天前
|
前端开发 JavaScript 应用服务中间件
前端vue2、vue3去掉url路由“ # ”号——nginx配置(二)
前端vue2、vue3去掉url路由“ # ”号——nginx配置
32 0
|
3天前
|
应用服务中间件 PHP nginx
php如何实现检测nginx配置的正确性
请确保在执行此操作时,PHP有足够的权限来执行Nginx命令和访问Nginx配置文件。另外,将上述代码嵌入到您的应用程序中时,要注意安全性,以防止潜在的命令注入攻击。
21 3
|
7天前
|
缓存 前端开发 安全
Nginx缓存及HTTPS配置小记(下)
Nginx缓存及HTTPS配置小记(下)
19 1
|
7天前
|
缓存 负载均衡 应用服务中间件
Nginx缓存及HTTPS配置小记(上)
Nginx缓存及HTTPS配置小记
19 0
|
7天前
|
XML 网络协议 应用服务中间件
Nginx应用进阶HTTP核心模块配置小结(下)
Nginx应用进阶HTTP核心模块配置小结(下)
16 1
|
7天前
|
存储 算法 应用服务中间件
Nginx应用进阶HTTP核心模块配置小结(上)
Nginx应用进阶HTTP核心模块配置小结
18 1
|
10天前
|
安全 应用服务中间件 网络安全
linux_nginx中添加ssl配置(open ssl)
linux_nginx中添加ssl配置(open ssl)
20 1
|
10天前
|
JSON JavaScript 前端开发
vue2_vite.config.js的proxy跨域配置和nginx配置代理有啥区别?
vue2_vite.config.js的proxy跨域配置和nginx配置代理有啥区别?
24 1