网络架构搭建组建
load balance(负载均衡组) | lb1:192.168.8.5 lb2:192.168.8.6 |
web(nginx组) | web1:192.168.8.7 web2:192.168.8.8 |
php(动态解析服务器) | php:192.168.8.9 |
mysql(数据库) | mysql:192.168.8.10 |
nfs(文件共享存储服务器) | nfs:192.168.8.11 |
1、修改主机名
操作为了方便区分操作主机,我们将每一台主机设置主机名并加入/etc/hosts文件中。
可以执行如下代码,自动修改主机名自动修改主机名称并加入hosts文件-脚本-CSDN博客(准备工作不写在文章中了。)
2、生成免密密钥、安装自动应答工具expect
ssh-keygen -t ecdsa
yum -y install expect
免密脚本
#!/bin/bash
SERVERS="192.168.8.8 192.168.8.6 192.168.8.7 192.168.8.9 192.168.8.10 192.168.8.11"
PASSWD="jingyu"
function sshcopyid
{
expect -c "
set timeout -1;
spawn ssh-copy-id $1;
expect {
\"yes/no\" { send \"yes\r\" ;exp_continue; }
\"password:\" { send \"$PASSWD\r\";exp_continue; }
};
expect eof;
"
}
for server in $SERVERS
do
sshcopyid $server
done
!!!修改脚本中的密码与自己的密码一致之后执行。第一次需要确认身份是正常的。
主机1-4安装nginx(主机192.168.8.5-192.168.8.8)源代码安装
编译安装前的优化
yum -y install gcc gcc-c++ make libtool zlib zlib-devel pcre pcre-devel openssl openssl-devel
nginx-1.24.0.tar.gz下载链接 nginx-1.26.0.tar.gz下载链接 nginx-1.27.0.tar.gz下载链接
文章使用nginx-1.26.0版本。
wget https://nginx.org/download/nginx-1.26.0.tar.gz
tar xf nginx-1.26.0.tar.gz -C /usr/src/
安装配置隐藏版本号增加安全性。
vim /usr/src/nginx*/src/core/nginx.h
修改这两处
//此行修改的是你想要的版本
#define NGINX_VERSION " 1.10.2" //第 13 行
//此行修改的是你想修改的软件名称
#define NGINX_VER "nginx/" NGINX_VERSION //第 14 行
修改上面的信息,即可更改 nginx 显示版本。例如:(curl–I 可看到,请求头和响应头显示) #define NGINX_VERSION "csdn"
#define NGINX_VER " jingyu/" NGINX_VERSION
修改 HTTP 头信息中的connection 字段,防止回显具体版本号
拓展:通用 http 头 ,通用头包含请求和响应消息都支持的头,通用头包含 Cache-Control、 Connection 、Date 、Pragma 、Transfer-Encoding 、Upgrade 、Via。对通用头的扩展要求通讯双 方都支持此扩展,如果存在不支持的通用头,一般将会作为实体头处理。那么也就是说有部分设备、或者是软件,能获取到 connection,部分不能,为了安全要做到所有的有可能的漏洞彻底隐藏。
修改nginx目录中的src目录、http目录、ngx_header_fileter_module.c文件
vim /usr/src/nginx-1.26.0/src/http/ngx_http_header_filter_module.c
修改前:
static char ngx_http_server_string[] = "Server: nginx" CRLF; //第 49 行 修改后:
修改后:
static char ngx_http_server_string[] = "Server: csdn-jingyu" CRLF; 定义了 http 错误码的返回:
页面错误信息隐藏
页面程序出现错误,Nginx 会代我们返回相应的错误代码,回显的时候,会带上 nginx 和版本号,我们把他隐藏起来
vim /usr/src/nginx-1.26.0/src/http/ngx_http_special_response.c
修改后
static u_char ngx_http_error_tail[] =
"<hr><center>csdn-jingyu</center>" CRLF
"</body>" CRLF
"</html>" CRLF
;
创建nginx运行账户并加入nginx组
groupadd nginx
useradd -M -s /sbin/nologin -g nginx nginx
./configure --prefix=/usr/local/nginx1.26 \
--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-http_gzip_static_module \
--with-http_ssl_module \
--with-http_realip_module \
--user=nginx --group=nginx \
make && make install
添加永久环境变量优化nginx程序执行路径
nginx -t
sed -i '$aexport NGINX_HOME=/usr/local/nginx1.26' /etc/profile
sed -i '$aPATH=$APTH:$NGINX_HOME/sbin' /etc/profile
source /etc/profile