nginx安装与调优部署文档(Linux) nginx1.12安装
suiw9 2024-11-14 19:11 45 浏览 0 评论
1. 安装环境准备
1.1 主机环境准备
1.1.1. 关闭selinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
setenforce 0
1.1.2. 部署规划
软件安装路径 /usr/local/nginx/
软件日志路径 /usr/local/nginx/logs/
软件二进制路径 /usr/local/nginx/sbin/
软件缓存代理等路径 /var/tmp/nginx/{client_body,proxy,fastcgi,uwsgi,scgi}
软件主配置文件路径 /usr/local/nginx/conf
软件子配置文件路径 /usr/local/nginx/conf/conf.d/
端口规划 80
1.1.3. 系统主机时间、时区、系统语言
? 本节视实际情况需要操作
? 修改时区
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
? 修改系统语言环境
echo 'LANG="en_US.UTF-8"' >> /etc/profile && source /etc/profile
? 配置主机NTP时间同步
yum -y install ntp
systemctl enable ntpd && systemctl start ntpd
echo 'server ntp1.aliyun.com' >> /etc/ntp.conf
echo 'server ntp2.aliyun.com' >> /etc/ntp.conf
2. Nginx安装部署
备注:2.2章节、2.3章节与2.4章节的依赖包可使用yum直接安装,安装指令如下:
yum install -y pcre-devel zlib-devel openssl-devel
也可按本文档使用源码安装。如何使用yum安装,要注意nginx的安装参数要删除如下三行
--with-pcre=../pcre-8.44 \
--with-zlib=../zlib-1.2.11 \
--with-openssl=../openssl-1.1.1f \
2.1 Nginx依赖安装与环境准备
? 添加用户与用户组(用户名请自行定义)
groupadd -r nginx && useradd -s /sbin/nologin -r -g nginx nginx
? CentOS平台安装依赖
yum -y install gcc gcc-c++ automake autoconf libtool make wget net-tools
yum install -y libxslt* libxml2* gd-devel perl-devel perl-ExtUtils-Embed GeoIP GeoIP-devel GeoIP-data
12
? 下载nginx-1.19.1.tar.gz安装包,并解压
cd /opt
wget http://nginx.org/download/nginx-1.19.1.tar.gz
tar -zxvf nginx-1.19.1.tar.gz
123
? 从根源上隐藏nginx版本号
(1)修改nginx.h文件如下三行配置信息变更,举例如下
vi /opt/nginx-1.19.1/src/core/nginx.h
修改前
#define nginx_version 1019001
#define NGINX_VERSION "1.19.1"
#define NGINX_VER "nginx/" NGINX_VERSION
修改后
#define nginx_version 1010001
#define NGINX_VERSION "618"
#define NGINX_VER "WEB/" NGINX_VERSION
(2)修改ngx_http_header_filter_module.c文件的ngx_http_server_string显示名称与步骤1中的NGINX_VER名称一致
vi /opt/nginx-1.19.1/src/http/ngx_http_header_filter_module.c
修改前
static u_char ngx_http_server_string[] = "Server: nginx" CRLF;
static u_char ngx_http_server_full_string[] = "Server: " NGINX_VER CRLF;
static u_char ngx_http_server_build_string[] = "Server: " NGINX_VER_BUILD CRLF;
修改后
static u_char ngx_http_server_string[] = "Server: WEB" CRLF;
static u_char ngx_http_server_full_string[] = "Server: " NGINX_VER CRLF;
static u_char ngx_http_server_build_string[] = "Server: " NGINX_VER_BUILD CRLF;
(2)修改ngx_http_special_response.c文件的ngx_http_error_tail显示名称与步骤1中的NGINX_VER名称一致
vi /opt/nginx-1.19.1/src/http/ngx_http_special_response.c
修改前
static u_char ngx_http_error_tail[] =
"<hr><center>nginx</center>" CRLF
"</body>" CRLF
"</html>" CRLF
;
修改后
static u_char ngx_http_error_tail[] =
"<hr><center>WEB</center>" CRLF
"</body>" CRLF
"</html>" CRLF
;
注:修改完成后注意保存配置文件
? Nginx部署环境准备
mkdir -pv /var/tmp/nginx/{client_body,proxy,fastcgi,uwsgi,scgi}
mkdir -pv /usr/local/nginx
chown -R nginx:nginx /var/tmp/nginx
chown -R nginx:nginx /usr/local/nginx
2.2 Pcre安装
cd /opt
wget https://ftp.pcre.org/pub/pcre/pcre-8.44.tar.gz
tar -zxvf pcre-8.44.tar.gz
cd pcre-8.44/
./configure
make && make install
2.3 Zlib安装
cd /opt
wget http://www.zlib.net/fossils/zlib-1.2.11.tar.gz
tar -zxvf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure
make && make install
2.4 Openssl安装
cd /opt
wget https://ftp.openssl.org/source/old/1.1.1/openssl-1.1.1f.tar.gz
tar -zxvf openssl-1.1.1f.tar.gz
cd openssl-1.1.1f
./config
make && make install
2.5 Nginx安装
cd /opt/nginx-1.19.1
./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_xslt_module \
--with-http_stub_status_module \
--with-http_sub_module \
--with-http_random_index_module \
--with-http_degradation_module \
--with-http_secure_link_module \
--with-http_gzip_static_module \
--with-http_perl_module \
--with-pcre=../pcre-8.44 \
--with-zlib=../zlib-1.2.11 \
--with-openssl=../openssl-1.1.1f \
--with-debug \
--with-file-aio \
--with-mail \
--with-mail_ssl_module \
--http-client-body-temp-path=/var/tmp/nginx/client_body \
--http-proxy-temp-path=/var/tmp/nginx/proxy \
--http-fastcgi-temp-path=/var/tmp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--http-scgi-temp-path=/var/tmp/nginx/scgi \
--with-stream \
--with-ld-opt="-Wl,-E"
make && make install
2.6 配置nginx环境变量
cat >>/etc/profile<<EOF
NGINX_HOME=/usr/local/nginx
PATH=\$NGINX_HOME/sbin:\$PATH
EOF
source /etc/profile
2.7 配置nginx系统服务
1、添加nginx系统服务启动脚本
vi /etc/init.d/nginx
#!/bin/bash
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: 2345 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
#
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/nginx.lock
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n "Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n "Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
sleep 1
start
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart)
$1
;;
status)
rh_status
;;
*)
echo #34;Usage: $0 {start|stop|status|restart}"
exit 2
;;
esac
2、配置nginx系统服务及自启动
chmod +x /etc/init.d/nginx
chkconfig --add nginx && chkconfig nginx on
chkconfig --list nginx
3、启动与停止nginx服务
service nginx start 或使用 systemctl start nginx
service nginx status 或使用 systemctl status nginx
ps -ef|grep nginx
service nginx stop 或使用 systemctl stop nginx
2.8 配置防火墙
配置操作系统防火墙(端口号根据实际添加)
针对CentOS6:
iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
service iptables save
针对CentOS7:
firewall-cmd --permanent --zone=public --add-port=80/tcp
firewall-cmd --reload
3. Nginx加固
3.1 安全审计
? 启用错误日志
#错误日志
error_log logs/error.log;
? 启用访问日志
#访问日志
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer"'
'"$http_user_agent" "$http_x_forwarded_for"'
'"$request_time" "$upstream_response_time"';
#日志缓存
access_log logs/access.log main buffer=64k flush=60s;
open_log_file_cache max=300 inactive=20s valid=1m min_uses=2;
3.2 隐藏nginx版本
? 在nginx.conf配置文件中添加隐藏nginx版本的参数
# hide nginx version
server_tokens off;
? 在fastcgi.conf配置文件中添加#注释如下配置隐藏php中nginx的版本信息
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
3.3 数据保密性
? 配置防盗链,在nginx.conf对应的server中配置以下参数(根据实际环境需要配置)
location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip)$ {
valid_referers none blocked 域名;
if ($invalid_referer) {
return 403;
break;
}
access_log off;
}
3.4 配置错误界面
? 把error.html放在nginx/html下。在nginx.conf的http中配置以下参数
error_page 404 500 502 503 504 505 /error.html;
3.5 Web前端安全
? 防止点击劫持,防止ie内容嗅探,防止xss,只能从本域名加载资源(外部脚本无法执行),在nginx.conf的server中配置以下参数(根据实际环境需要配置)
add_header X-Frame-Options SAMEORIGIN;
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection 1;
#add_header Content-Security-Policy "default-src 'self'";
3.6 https安全
? 不使用SSL和TLS1.1以下,使用TLS1.2以上版本,在nginx.conf的server中配置以下参数(在启用https的场景中配置)
SSL_Protocols TLSv1.2;
3.7 访问控制
? 限制ip访问(因公网访问nginx,建议不设置。除非有恶意ip尝试cc攻击或暴力破解等非法操作)(根据实际环境需要配置)
location / {
deny 192.168.1.1; #拒绝IP
allow 192.168.1.0/24; #允许IP
allow 10.1.1.0/16; #允许IP
deny all; #拒绝其他所有IP
}
3.8 限制http请求方法
? 在nginx.conf的server中配置以下参数,只允许GET、POST两个http请求方式
location / {
if ($request_method !~* GET|POST) {
return 403;
}
}
4. Nginx优化
4.1 Nginx工作进程数量
? 一般设置CPU的核心或者核心数x2(worker_processes最多开启8个)
grep ^processor /proc/cpuinfo | wc -l //获取cpu核心数
worker_processes 4;
4.2 Nginx运行CPU亲和力
? 比如2核配置
worker_processes 2;
worker_cpu_affinity 01 10;
? 比如4核配置
worker_processes 4;
worker_cpu_affinity 0001 0010 0100 1000;
? 比如8核配置
worker_processes 8;
worker_cpu_affinity 00000001 00000010 00000100 0000100000010000 00100000 01000000 10000000;
4.3 优化内核参数与连接数
cat >>/etc/sysctl.conf<<EOF
fs.file-max = 6815744
net.ipv4.ip_forward = 0
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
net.ipv4.tcp_syncookies = 1
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
net.ipv4.tcp_max_tw_buckets = 6000
net.ipv4.tcp_sack = 1
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_rmem = 10240 87380 12582912
net.ipv4.tcp_wmem = 10240 87380 12582912
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.core.netdev_max_backlog = 262144
net.core.somaxconn = 40960
net.ipv4.tcp_max_orphans = 3276800
net.ipv4.tcp_max_syn_backlog = 262144
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_fin_timeout = 1
net.ipv4.tcp_keepalive_time = 30
net.ipv4.ip_local_port_range = 1024 65000
EOF
sysctl -p
cat >>/etc/security/limits.conf<<EOF
* soft nofile 65535
* hard nofile 65535
* soft noproc 65535
* hard noproc 65535
EOF
4.4 Nginx事件处理
? 启用epoll模型以提高处理效率
events {
use epoll;
worker_connections 65535;
multi_accept on;
}
4.5 开启高效传输模式
sendfile on;
tcp_nopush on;
4.6 连接超时时间
? 保护服务器资源,CPU,内存与控制连接数
keepalive_timeout 60;
tcp_nodelay on;
client_header_buffer_size 4k;
open_file_cache max=102400 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 1;
client_header_timeout 60;
client_body_timeout 60;
reset_timedout_connection on;
send_timeout 20;
client_max_body_size 10m;
4.7 配置文件专属路径便携配置
不同的服务配置单独的conf文件,提高运维效率,以nginx.conf配置文件添加include参数
mkdir /usr/local/nginx/conf/conf.d
include /usr/local/nginx/conf/conf.d/*.conf;
4.8 Gzip调优
使用gzip压缩功能,可能为我们节约带宽,加快传输速度
gzip on;
gzip_min_length 2k;
gzip_buffers 4 32k;
gzip_http_version 1.1;
gzip_comp_level 6;
gzip_typestext/plain text/css text/javascriptapplication/json application/javascript application/x-javascriptapplication/xml;
gzip_vary on;
gzip_proxied any;
4.9 Expires缓存调优
缓存,主要针对于图片,css,js等元素更改机会比较少的情况下使用,特别是图片,占用带宽大,可以设置图片在浏览器本地缓存365d,css,js,html可以缓存个10来天。
location ~* \.(ico|jpe?g|gif|png|bmp|swf|flv)$ {
expires 30d;
#log_not_found off;
access_log off;
}
location ~* \.(js|css)$ {
expires 7d;
log_not_found off;
access_log off;
}
5、error.html界面内容
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<title>网页访问不了</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" type="text/css" href="404/error_all.css?t=201303212934">
</head>
<body class="error-404">
<div id="doc_main">
<section class="bd clearfix">
<div class="module-error">
<div class="error-main clearfix">
<div class="label"></div>
<div class="info">
<h3 class="title">抱歉!该网站可能因为以下原因无法访问!</h3>
<div class="reason">
<p>1.您访问的域名未绑定主机;</p>
<p>2.您正在使用IP访问,请尝试使用域名访问;</p>
<p>3.该网站已被网站管理员停止。</p>
</div>
</div>
</div>
</div>
</section>
</div>
</body></html>
6. 结束
相关推荐
- 5款Syslog集中系统日志常用工具对比推荐
-
一、为何要集中管理Syslog?Syslog由Linux/Unix系统及其他网络设备生成,广泛分布于整个网络。因其包含关键信息,可用于识别网络中的恶意活动,所以必须对其进行持续监控。将Sys...
- 跨平台、多数据库支持的开源数据库管理工具——DBeaver
-
简介今天给大家推荐一个开源的数据库管理工具——DBeaver。它支持多种数据库系统,包括Mysql、Oracle、PostgreSQL、SLQLite、SQLServer等。DBeaver的界面友好...
- 强烈推荐!数据库管理工具:Navicat Premium 16.3.2 (64位)
-
NavicatPremium,一款集数据迁移、数据库管理、SQL/查询编辑、智能设计、高效协作于一体的全能数据库开发工具。无论你是MySQL、MariaDB、MongoDB、SQLServer、O...
- 3 年 Java 程序员还玩不转 MongoDB,网友:失望
-
一、什么场景使用MongoDB?...
- 拯救MongoDB管理员的GUI工具大赏:从菜鸟到极客的生存指南
-
作为一名在NoSQL丛林中披荆斩棘的数据猎人,没有比GUI工具更称手的瑞士军刀了。本文将带你围观五款主流MongoDB管理神器的特性与暗坑,附赠精准到扎心的吐槽指南一、MongoDBCompass:...
- mongodb/redis/neo4j 如何自己打造一个 web 数据库可视化客户端?
-
前言最近在做neo4j相关的同步处理,因为产线的可视化工具短暂不可用,发现写起来各种脚本非常麻烦。...
- solidworks使用心得,纯干货!建议大家收藏
-
SolidWorks常见问题...
- 统一规约-关乎数字化的真正实现(规范统一性)
-
尽管数字化转型的浪潮如此深入人心,但是,对于OPCUA和TSN的了解却又甚少,这难免让人质疑其可实现性,因为,如果缺乏统一的语义互操作规范,以及更为具有广泛适用的网络与通信,则数字化实际上几乎难以具...
- Elasticsearch节点角色配置详解(Node)
-
本篇文章将介绍如下内容:节点角色简介...
- 产前母婴用品分享 篇一:我的母婴购物清单及单品推荐
-
作者:DaisyH8746在张大妈上已经混迹很久了,有事没事看看“什么值得买”已渐渐成了一种生活习惯,然而却从来没有想过自己要写篇文章发布上来,直到由于我产前功课做得“太过认真”(认真到都有点过了,...
- 比任何人都光彩照人的假期!水润、紧致的肌肤护理程序
-
图片来源:谜尚愉快的假期临近了。身心振奋的休假季节。但是不能因为这种心情而失去珍贵的东西,那就是皮肤健康。炙热的阳光和强烈的紫外线是使我们皮肤老化的主犯。因此,如果怀着快乐的心情对皮肤置之不理,就会使...
- Arm发布Armv9边缘AI计算平台,支持运行超10亿参数端侧AI模型
-
中关村在线2月27日消息,Arm正式发布Armv9边缘人工智能(AI)计算平台。据悉,该平台以全新的ArmCortex-A320CPU和领先的边缘AI加速器ArmEthos-U85NPU为核心...
- 柔性——面向大规模定制生产的数字化实现的基本特征
-
大规模定制生产模式的核心是柔性,尤其是体现在其对定制的要求方面。既然是定制,并且是大规模的定制,对于制造系统的柔性以及借助于数字化手段实现的柔性,就提出了更高的要求。面向大规模定制生产的数字化业务管控...
- 创建PLC内部标准——企业前进的道路
-
作者:FrankBurger...
- 标准化编程之 ----------- 西门子LPMLV30测试总结
-
PackML乃是由OMAC开发且被ISA所采用的自动化标准TR88.00.02,能够更为便捷地传输与检索一致的机器数据。PackML的主要宗旨在于于整个工厂车间倡导通用的“外观和感觉”,...
你 发表评论:
欢迎- 一周热门
-
-
Linux:Ubuntu22.04上安装python3.11,简单易上手
-
宝马阿布达比分公司推出独特M4升级套件,整套升级约在20万
-
MATLAB中图片保存的五种方法(一)(matlab中保存图片命令)
-
别再傻傻搞不清楚Workstation Player和Workstation Pro的区别了
-
Linux上使用tinyproxy快速搭建HTTP/HTTPS代理器
-
如何提取、修改、强刷A卡bios a卡刷bios工具
-
Element Plus 的 Dialog 组件实现点击遮罩层不关闭对话框
-
日本组合“岚”将于2020年12月31日停止团体活动
-
SpringCloud OpenFeign 使用 okhttp 发送 HTTP 请求与 HTTP/2 探索
-
tinymce 号称富文本编辑器世界第一,大家同意么?
-
- 最近发表
-
- 5款Syslog集中系统日志常用工具对比推荐
- 跨平台、多数据库支持的开源数据库管理工具——DBeaver
- 强烈推荐!数据库管理工具:Navicat Premium 16.3.2 (64位)
- 3 年 Java 程序员还玩不转 MongoDB,网友:失望
- 拯救MongoDB管理员的GUI工具大赏:从菜鸟到极客的生存指南
- mongodb/redis/neo4j 如何自己打造一个 web 数据库可视化客户端?
- solidworks使用心得,纯干货!建议大家收藏
- 统一规约-关乎数字化的真正实现(规范统一性)
- Elasticsearch节点角色配置详解(Node)
- 产前母婴用品分享 篇一:我的母婴购物清单及单品推荐
- 标签列表
-
- dialog.js (57)
- importnew (44)
- windows93网页版 (44)
- yii2框架的优缺点 (45)
- tinyeditor (45)
- qt5.5 (60)
- windowsserver2016镜像下载 (52)
- okhttputils (51)
- android-gif-drawable (53)
- 时间轴插件 (56)
- docker systemd (65)
- slider.js (47)
- android webview缓存 (46)
- pagination.js (59)
- loadjs (62)
- openssl1.0.2 (48)
- velocity模板引擎 (48)
- pcre library (47)
- zabbix微信报警脚本 (63)
- jnetpcap (49)
- pdfrenderer (43)
- fastutil (48)
- uinavigationcontroller (53)
- bitbucket.org (44)
- python websocket-client (47)