Nginx源码安装和调优技巧

本文内容php

  1. Nginx与apache的对比
  2. 实战1:在“腾讯云主机”上源码编译安装Nginx
  3. 实战2:Nginx调优之隐藏版本信息防止黑客扫描识别漏洞
  4. 实战3:设置网页缓存

实验环境: 使用RHEL6.5/centos6.5   64位html

nginx概述: 前端

      Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理服务器。 Nginx 是由 Igor Sysoev(伊戈尔·塞索耶夫)为俄罗斯访问量第二的 rambler.ru 站点开发的,第一个公开版本0.1.0发布于2004年10月4日。mysql

         Nginx可以选择高效的epoll(Linux2.6内核)做为网络I/O)模型,在高链接并发的状况下,Nginx是Apache服务器不错的替代品,它可以支持高达5000个并发链接数的响应,而内存、CPU等系统资源消耗却很是低,运行很是稳定。nginx

nginx的官方网站:c++

http://nginx.org/en/download.htmlweb

https://oscimg.oschina.net/oscnet/f4f307d86117f4a98178f382bccee0f4297.jpg

Mainline version  主线版本sql

Stable version  稳定版本shell

Legacy versions  遗产版本 /历史版本apache

版本命名的常识:偶数为稳定,奇数是测试版或开发版本

 

Tengine概述:
Tengine是由淘宝网发起的Web服务器项目。它在Nginx的基础上,针对大访问量网站的需求,添加了不少高级功能和特性。Tengine的性能和稳定性已经在大型的网站如淘宝网,天猫商城等获得了很好的检验。

官方网站:http://tengine.taobao.org/

 

Nginx和Apache的区别:

nginx或apache 服务器自己能够解析php文件吗?  

apache和nginx在处理php代码时有什么区别?

 

       1:Nginx是经过php-fpm这个服务来处理php文件

       2:Apache是经过libphp5.so这个模块来处理php文件

 

Nginx

https://oscimg.oschina.net/oscnet/4292ccec6fe2e135a58099df9757fa7fcdb.jpg

Apache

https://oscimg.oschina.net/oscnet/0acad75ba7c614e02bb784d86ad3f7b6172.jpg

 

总结:

Apache的libphp5.so随着apache服务器一块儿运行,而Nginx和php-fpm是各自独立运行,因此在运行过程当中,Nginx和php-fpm都须要分别启动!

 

实战场景: 公司如今要上一台web服务器, 并发数据要达5000人同时在线。 这时应该选择apache或nginx ?  你选择哪一个?

 

nginx相对于apache的优势: 
轻量级,一样起web 服务,比apache 占用更少的内存及资源 ;高并发,nginx 处理请求是异步非阻塞的,而apache 则是阻塞型的,在高并发下nginx 能保持低资源低消耗高性能;高度模块化的设计,编写模块相对简单;社区活跃,各类高性能模块出品迅速。

apache 相对于nginx 的优势: 
apache 超稳定 ,通常来讲,须要并发性高的web 服务,用nginx 。若是不须要性能只求稳定,那就apache 。

nginx处理动态请求是鸡肋,通常动态请求要apache去作,nginx只适合静态和反向。

 

安装nginx

登陆腾讯云主机: https://www.qcloud.com/

云主机装系统使用的镜像: 不是rhel.iso  用的是 centos.img  或 centos.qcow2

听风格, 开眼界 !

 

1、安装nginx时必须先安装相应的编译工具
[root@localhost ~]#yum -y install gcc gcc-c++ autoconf automake 

注意  :默认是最小化安装

gcc                            c语言编译器

gcc-c++                    c++语言编译器

autoconf automake           用于make编译的工具


[root@localhost ~]#yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel

zlib  : nginx提供gzip模块,须要zlib库支持
openssl : nginx提供ssl功能
pcre  : 支持地址重写rewrite功能

 

安装nginx:

上传nginx软件包对

[root@localhost ~]# yum install lrzsz -y

 

[root@localhost ~]# tar zxvf nginx-1.10.2.tar.gz

[root@localhost ~] cd nginx-1.10.2

 

[root@localhost  nginx-1.10.2]# useradd  -s /sbin/noligin    -M     nginx                           # 添加nginx用户,但没有登陆权限

#vi /etc/passwd                    #查看配置文件,/sbin/noligin表示不能登陆

https://oscimg.oschina.net/oscnet/fe206a7699464fa9e4d107e3fb0f794a076.jpg

#建立一个nginx用户

-s      指定登陆shell

-M     不建立家目录

 

[root@localhost nginx-1.10.2]#./configure --prefix=/usr/local/nginx   --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module  --with-http_gzip_static_module  --with-pcre  #检查编译环境

 

参数说明:

--prefix=/usr/local/nginx       指定安装路径

--user=nginx --group=nginx    指定运行nginx进程的用户和组

--with-http_ssl_module         支持ssl加密

--with-http_realip_module      此模块支持显示真实来源IP地址,主要用于NGINX作前端负载均衡服务器使用

--with-http_gzip_static_module  这个模块指定压缩

--with-pcre                   此模块支持rewrite功能          

[root@xuegod63 nginx-1.10.2]# echo $?

0

 

编译和安装:

[root@localhost  nginx-1.10.2]# make -j 4    #编译,把源代码编译成可执行的二进制文件(只有一核,就直接make)。

-j 4  #以4个进程同时编译

编译:mysql 内核 1个小时。  -j 4  20分钟。

 

[root@localhost  nginx-1.10.2]# make  install   #安装

 

nginx主要目录结构:

[root@localhost  nginx-1.10.2]# ls /usr/local/nginx/

conf  html  logs  sbin

conf              #配置文件

html            #网站根目录

logs             #日志

sbin             #nginx启动脚本

 

启动nginx :

[root@localhost  ~]# /usr/local/nginx/sbin/nginx   

测试:

http://123.207.157.107/

https://oscimg.oschina.net/oscnet/20e8d9d70650221e810fccf421088ded43f.jpg

 

实战2: nginx调优--隐藏版本信息

 

为何要屏蔽nginx版本信息?

答:由于黑客能够扫描出nginx版本信息,能够查看对应的版本信息的漏洞,而后攻击

如何查nginx版本信息?

 

查看nginx版本信息:

[root@localhost ~]# curl -I www.baidu.com

[root@localhost ~]#  curl -I www.tencent.com

查看本机使用的服务是nginx仍是apache

 

Nginx 漏洞

https://oscimg.oschina.net/oscnet/7ca2a7f6ae3d2076ea3c2abdaa992292dba.jpg

 

[root@localhost ~]# curl -I www.taobao.com

HTTP/1.1 302 Found

Server: Tengine  基于nginx作了二次开发

 

查看本身的服务器:

[root@localhost ~]# curl  -I 123.207.157.107

HTTP/1.1 200 OK

Server: nginx/1.10.2   #这里暴露了目标信息

 

完全让版本等敏感信息消失

[root@localhost nginx-1.10.2]# cd /root/nginx-1.10.2

[root@localhost nginx-1.10.2]# vim src/core/nginx.h          //目的更改源码隐藏软件名称和版本号

13

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

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

[root@localhost nginx-1.9.12]# vim src/http/ngx_http_header_filter_module.c

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

改:49 static char ngx_http_server_string[] = "Server: nginx" CRLF;

为:49 static char ngx_http_server_string[] = "Server: XWS" CRLF;

 

这个文件定义了http错误码的返回,有时候咱们页面程序出现错误,Nginx会代咱们返回相应的错误代码,回显的时候,会带上nginx和版本号,咱们把他隐藏起来

[root@localhost nginx-1.9.12]# vim src/http/ngx_http_special_response.c

29

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>XWS</center>" CRLF

"</body>" CRLF

"</html>" CRLF

;

 

须要从新进行nginx安装:

 [root@localhost ~]# /usr/local/nginx/sbin/nginx -s stop

[root@localhost~]# rm -rf /usr/local/nginx/

[root@localhost nginx-1.10.2]# pwd

/root/nginx-1.10.2

[root@localhost nginx-1.10.2]# make clean           #清空以前编译安装的内容。

rm -rf Makefile objs

编译和安装:

[root@localhost nginx-1.10.2]#./configure --prefix=/usr/local/nginx   --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module  --with-http_gzip_static_module  --with-pcre && make && make install

 

 

启动服务:[root@localhost nginx]# /usr/local/nginx/sbin/nginx

 

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

启动服务:[root@localhost nginx]# /usr/local/nginx/sbin/nginx

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

[root@localhost ~]# curl -I 123.207.157.107

HTTP/1.1 200 OK

Server: XWS/8.8.2

或:

http://123.207.157.107/aaa.html

nginx有24处调优

 

重启

1、验证nginx配置文件是否正确

方法一:进入nginx安装目录sbin下,输入命令./nginx -t

看到以下显示nginx.conf syntax is ok

nginx.conf test is successful

说明配置文件正确!

方法二:在启动命令-c前加-t

 

重启Nginx服务

 方法一:进入nginx可执行目录sbin下,输入命令./nginx -s reload 便可

方法二:查找当前nginx进程号,而后输入命令:kill -HUP 进程号 实现重启nginx服务

相关文章
相关标签/搜索