Nginx的搭建和优化
目录
Nginx概述
一款高性能、轻量级Web服务软件
Nginx相对于Apache的优点:
Apache相对于Nginx的优点
编译安装Nginx 服务
关闭防火墙并将压缩包解压到opt目录下
安装依赖包
创建运行用户
编译安装 Nginx
检查、启用、重启、停止 nginx服务
添加 Nginx 系统服务
认识Nginx服务的主配置文件 nginx.conf
全局配置
I/O事件配置
HTTP 配置
日志格式设定:
访问状态统计配置
基于授权的访问控制
生成用户密码认证文件
修改主配置文件相对应目录,添加认证配置项
重启服务,访问测试
基于客户端的访问控制
基于域名的 Nginx 虚拟主机
为虚拟主机提供域名解析
为虚拟主机准备网页文档
修改 Nginx的配置文件
基于不同IP的虚拟主机
基于端口的虚拟主机
修改nginx主配置文件,仅修改监听端口
总结
Nginx概述
一款高性能、轻量级Web服务软件
稳定性高
系统资源消耗低
对HTTP并发连接的处理能力高
单台物理服务器可支持30000 ~ 50000个并发请求
占用内存少,并发能力强
Nginx相对于Apache的优点:
轻量级,同样是web服务,比Apache占用更少的内存及资源;
高并发,Nginx处理请求是异步非塞的,而Apache则是阻塞型的,在高并发下Nginx 能保持低资源低消耗高性能;
高度模块化的设计编写模块相对简单;
社区活跃,各种高性能模块出品迅速。
Apache相对于Nginx的优点
rewrite ,比nginx 的rewrite 强大
模块超多,基本想到的都可以找到
少bug ,nginx 的bug 相对较多
超稳定
编译安装Nginx 服务
关闭防火墙并将压缩包解压到opt目录下
[root@localhost opt]# systemctl disable --now firewalld.service
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost opt]# setenforce 0
安装依赖包
看有没有挂载关盘
yum -y install pcre-devel zlib-devel gcc gcc-c++ make
创建运行用户
(Nginx 服务程序默认以 nobody 身份运行,建议为其创建专门的用户账号,以便更准确地控制其访问权限)
useradd -M -s /sbin/nologin/ nginx #-M 代表不创建家目录
编译安装 Nginx
[root@localhost opt]# tar zxvf nginx-1.12.0.tar.gz
[root@localhost opt]# cd nginx-1.12.0/./configure \
--prefix=/usr/local/nginx \ #指定nginx的安装路径
--user=nginx \ #指定用户名
--group=nginx \ #指定组名
--with-http_stub_status_module #启用 http_stub_ status_ module 模块以支持状态统计make -j2 && make install 启用两核处理器
[root@localhost nginx]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
#创建软链接便于系统管理,让系统识别nginx的操作命令
检查、启用、重启、停止 nginx服务
nginx -t #检查配置文件是否配置正确
nginx #启动 #####这里启动服务如果启动失败可以查看apache的服务是否开启,如果已经开启可以通过service httpd stop
关闭服务
----停止nginx 服务------
cat /usr/local/nginx/logs/nginx.pid #首先查看nginx的PID号
kill -3 <PID号>
kill -s QUIT <PID号>
killall -3 nginx
killall -s QUIT nginx----重载------
kill -l1<PID号>
kill -s HUP <PID号>
killall -1 nginx
killall -s HUP <PID号>----日志分隔,重新打开日志文件-------
Kill -USR1 <PID号>
-----平滑升级------
kill -USR2 <PID号>
新版本升级
./configure \
-prefix=/usr/local/ngink \
-user=nginx\
-group=nginx \
-with-http stub_status _module\
-with-http_ssl_modulemake
mv/usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx_old
cp objs/nginx/usr/local/nginx/sbin/nginx
make upgrade
#或者先killall nginx,再/usr/local/nginx/sbin/nginx
查看PID号的几种方式:
方法一:
[root@localhost logs]# cat /usr/local/nginx/logs/nginx.pid
4702
方法二:
[root@localhost logs]# ss -natp |grep nginx
LISTEN 0 128 *:80 *:* users:(("nginx",pid=4703,fd=6),("nginx",pid=4702,fd=6))
方法三:
[root@localhost logs]# ps aux |grep nginx
root 4702 0.0 0.0 20544 600 ? Ss 11:24 0:00 nginx: master process nginx
nginx 4703 0.0 0.0 23072 1380 ? S 11:24 0:00 nginx: worker process
root 4813 0.0 0.0 112824 988 pts/1 S+ 11:32 0:00 grep --color=auto nginx
##nginx两种进程:master和worker master是管理worker的 worker用来处理用户的连接请求的
方法四:
[root@localhost logs]# ps -ef |grep nginx
root 4702 1 0 11:24 ? 00:00:00 nginx: master process nginx
nginx 4703 4702 0 11:24 ? 00:00:00 nginx: worker process
root 4848 1452 0 11:36 pts/1 00:00:00 grep --color=auto nginx
方法五:
[root@localhost logs]# lsof -i :80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 4702 root 6u IPv4 32968 0t0 TCP *:http (LISTEN)
nginx 4703 nginx 6u IPv4 32968 0t0 TCP *:http (LISTEN)
添加 Nginx 系统服务
方法一:
#!/bin/bash
#chkconfig: 35 99 20
#desc: this is nginx control scpritCMD="/usr/local/nginx/sbin/nginx"
PID="/usr/local/nginx/logs/nginx.pid"case "$1" in
start)
$CMD
;;stop)
kill -s QUIT $(cat $PID)
;;restart)
$0 stop
$0 start
;;reload)
kill -s HUP $(cat $PID);;
*)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1
esac
[root@localhost init.d]# chmod +x nginx
[root@localhost init.d]# ls
functions netconsole network nginx README
[root@localhost init.d]# chkconfig --add nginx #添加为系统服务
[root@localhost init.d]# chkconfig --list nginx[root@localhost init.d]# netstat -natp | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4702/nginx: master
[root@localhost init.d]# kill -3 4702 ##杀死进程号[root@localhost init.d]# service nginx start
方法二:
vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
---------------------------------------------------------
#每行意义
[Unit]
Description=nginx #描述
After=network.target #描述服务类别
[Service]
Type=forking #后台运行形势
PIDFile =/usr/local/nginx/logs/nginx.pid #PID文件位置
ExecStart=/usr/local/nginx/sbin/nginx #启动服务
ExecrReload=/bin/kill -s HUP $MAINPID #根据PID重载配置
ExecrStop=/bin/kill -s QUIT $MAINPID #根据PID终止进程
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@localhost system]# systemctl daemon-reload ##这里需要重载一下,不然可能会启动失败
[root@localhost system]# systemctl start nginx[root@localhost system]# systemctl enable nginx.service
认识Nginx服务的主配置文件 nginx.conf
全局块:全局配置,对全局生效;
events块:配置影响Nginx服务器与用户的网络连接;
http块:配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置:
server块:配置虚拟主机的相关参数,一个http块中可以有多个server块;
location块:用于配置匹配的uri;
upstream:配置后端服务器具体地址,负载均衡配置不可或缺的部分。
vim /usr/local/nginx/conf/nginx.conf
全局配置
#user nobody; #运行用户,若编译时未指定则默认为 nobody
worker_ processes 1; #工作运行数量,一般设置为和CPU核数一样
#error_log logs/error.log; #错误日志文件的位置
#pid logs/nginx.pid; #PID文件的位置
查看内核版本号 查看发行版本号
uname -r cat /etc/*releas
I/O事件配置
events {
use epoll; #使用 epoll 模型,2.6及以上版本的系统内核,建议使用 epoll 模型以提高性能
worker_connections 4096; #每个进程处理 4096 个连接
}#如提高每个进程的连接数还需执行“ulimit -n 65535”命令临时修改本地每个进程可以同时打开的最大文件数。
#在Linux 平台上,在进行高并发TCP连接处理时,最高的并发数量都要受到系统对用户单一进程同时可打开文件数量的限制(这是因为每个TCP连接都要创建一个socket句柄,每个socket句柄同时也是一个文件句柄)。
#可使用ulimit -a 命令查看系统允许当前用户进程打开的文件数限制。
修改相关的内核,做过哪些优化?
vim /etc/security/limits.conf
优化一个用户打开文件的数量
HTTP 配置
http {
##文件扩展名与文件类型映射表
include mime.types;
##默认文件类型
default_type application/octet-stream;
##日志格式设定
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
##访问日志位置
#access_log logs/access.log main;
##支持文件发送(下载)
sendfile on;
##此项允许或禁止使用socket的TCP_CORK的选项(发送数据包前先缓存数据),此选项仅在使用sendfile的时候使用
#tcp_nopush on;
##连接保持超时时间,单位是秒
#keepalive_timeout 0;
keepalive_timeout 65; #连接保持超时
##gzip模块设置,设置是否开启gzip压缩输出
#gzip on;
##web 服务的监听配置
server {
listen 80; #监听地址及端口
server_name www.gcc.com; #站点域名,可以有多个,用空格隔开
#charset utf-8; #网页的默认字符集
location / { #根目录配置
root html; #网站根目录的位置/usr/local/nginx/html
index index.html index.htm; #设置首页文件名
}
##内部错误的反馈页面
error_page 500 502 503 504 /50x.html;
##错误页面配置
location = /50x.html {
root html;
}
}
}
日志格式设定:
$remote_addr 与 $http_x_forwarded for用以记录客户端的ip地址;
sremote_user:用来记录客户端用户名称;
stime_local: 用来记录访问时间与时区;
$request: 用来记录请求的url与http协议;
$status: 用来记录请求状态;成功是200,
$body_bytes_sent:记录发送给客户端文件主体内容大小;
$http_referer:用来记录从哪个页面链接访问过来的;
$http_user_agent:记录客户浏览器的相关信息;通常web服务器放在反向代理的后面,这样就不能获取到客户的IP地址了,通过$remote_add拿到的IP地址是反向代理服务器的iP地址。反向代理服务器在转发请求的http头信息中,可以增加x_forwarded_for信息,用以记录原有客户端的IP地址和原来客户端的请求的服务器地址。
location常见配置指令, root, alias, proxy_pass
root (根路径配置) :请求www.gcc.com/test/1.jpg,会返回文件/usr/local/nginx/html/test/1.jpg
alias (别名配置) :请求www.gcc.com/test/1.jpg,会返回文件/usr/local/nginx/html/1.jpgproxypass (反向代理配置)
proxy_pass http://127.0.0.1:8080/; 会转发请求到http://127.0.0.1:8080/1.jpg
proxy_ pass http://127.0.0.1:8080; 会转发请求到http://127.0.0.1:8080/test/1.jpg
vim /usr/local/nginx/conf/nginx.conf ##nginx主配置文件
cd /usr/local/nginx/html/
[root@localhost html]# ls
50x.html index.html
[root@localhost html]# mkdir test
[root@localhost html]# echo 'this is my test web' > test/test.html
这个时候我们在到Nginx的主配置文件中,将root 改成alias,结果又会不一样
查看当前nginx版本号 nginx -v
nginx -V 不仅能看到当前nginx版本号还能够看到安装时配置的参数
访问状态统计配置
1、先使用命令/usr/local/nginx/sbin/nginx -V 查看已安装的 Nginx 是否包含 HTTP_STUB_STATUS 模块
2、修改 nginx.conf 配置文件,指定访问位置并添加 stub_status 配置
cd /usr/local/nginx/conf
cp nginx.conf nginx.conf.bak
vim /usr/local/nginx/conf/nginx.conf
......
http {
......
server {
listen 80;
server_name localhost;
charset utf-8;
##添加 stub_status 配置##
location /status { #访问位置为/status
stub_status on; #打开状态统计功能
access_log off; #关闭此位置的日志记录
}
}
}
重启服务,访问测试
systemctl restart nginx
可curl -Ls http://192.168.217.100/status 结合awk与if 语句进行性能监控。
[root@localhost conf]# curl -Ls http://192.168.217.100/status
Active connections: 1
server accepts handled requests
2 2 2
Reading: 0 Writing: 1 Waiting: 0
[root@localhost conf]# CON=$( curl -Ls http://192.168.217.100/status |awk '/Active connections:/{print $3}')
[root@localhost conf]# if [ $CON -gt 25000 ];then
> echo "警告,当前并发数量为 $CON ,超过预警值!"
> fi
基于授权的访问控制
生成用户密码认证文件
yum install -y httpd-tools
htpasswd -c /usr/local/nginx/passwd.db zhangsan
chown nginx /usr/local/nginx/passwd.db 修改文件的归属
chmod 400 /usr/local/nginx/passwd.db 保护数据文件的安全设置为400,只有nginx和管理员用户才能访问,必须要操作,不修改400的话,执行的时候会报错
修改主配置文件相对应目录,添加认证配置项
vim /usr/local/nginx/conf/nginx.conf
location /status {
stub_status on;
access_log off;
}location /test {
root html;
index index.html index.htm;
auth_basic "Hello every body!";
auth_basic_user_file /usr/local/nginx/userlist.txt;
}
重启服务,访问测试
基于客户端的访问控制
访问控制规则如下:
deny IP/IP段:拒绝某个 IP 或 IP段的客户端访问
allow IP/IP段:允许某个 IP 或IP 段的客户端的访问
规则从上往下执行,如匹配则停止,不再往下匹配
vim /usr/local/nginx/conf/nginx.conf
location /test {
root html;
index index.html index.htm;
auth_basic "Hello every body!";
auth_basic_user_file /usr/local/nginx/userlist.txt;
deny 192.168.217.110;
}systemctl restart nginx
location /test {
root html;
index index.html index.htm;
auth_basic "Hello every body!";
auth_basic_user_file /usr/local/nginx/userlist.txt;
allow 192.168.217.0/24;
deny 192.168.217.110;
}
基于域名的 Nginx 虚拟主机
为虚拟主机提供域名解析
echo "192.168.217.100 www.kgc.com www.accp.com" >> /etc/hosts ##Linux系统
为虚拟主机准备网页文档
mkdir -p /var/www/html/song
mkdir -p /var/www/html/accp
echo "<h1> www.song.com</h1>" >/var/www/html/song/index.html
echo "<h1> www.accp.com</h1>" >/var/www/html/accp/index.html
修改 Nginx的配置文件
vim /usr/local/nginx/conf/nginx.conf
……
http {
……
server {
listen 80;
server_name www.kgc.com;———————————设置域名www.song.com
charset utf-8;
access_log logs/www.kgc.access.log;————————————设置日志名
location /test {
root html/kgc;——————————————设置www.song.com的工作目录
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
36和56行复制到80行下面在命令行模式输入 :36,56 co 80
server {
listen 80;
server_name www.accp.com;————————————设置域名www.accp.com
charset utf-8;
access_log logs/www.accp.access.log;
location /test {
root html/accp;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}nginx -t #检查语法
systemctl restart nginx浏览器访问:
http://www.kgc.com
http://www.accp.com
做一下域名解析 vim /etc/hosts
基于不同IP的虚拟主机
ifconfig ens33:0 192.168.217.40/24
--------------------------------------------------------
vim /usr/local/nginx/conf/nginx.confserver {
listen 192.168.217.40:80;
server_name www.kgc.com;charset utf-8;
access_log logs/kgc.com.access.log;
location /test {
root html/kgc;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}}
server {
listen 192.168.217.100:80;
server_name www.accp.com;charset utf-8;
access_log logs/accp.com.access.log;
location /tset {
root html/accp;
index index.html index.htm;
}error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}nginx -t
systemctl restart nginxhttp://192.168.217.40
http://192.168.217.100
基于端口的虚拟主机
修改nginx主配置文件,仅修改监听端口
vim /usr/local/nginx/conf/nginx.conf
server {
listen 192.168.217.100:666;
server_name www.kgc.com;charset utf-8;
access_log logs/kgc.com.access.log;
location /test {
root html/kgc;
index index.html index.htm;
}error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}}
server {
listen 192.168.217.100:888;
server_name www.accp.com;charset utf-8;
access_log logs/accp.com.access.log;
location /tset {
root html/accp;
index index.html index.htm;
}error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}nginx -t
systemctl restart nginx
总结
##Nginx和Apache的差异:
轻量级,nginxt比apache占用更少的内存及资源:
静态处理,Nginx静态处理性能比Apache高;
Nginx可以实现无缓存的反向代理加速,提高网站运行速度;
Nginx的性能和可伸缩性不依赖于硬件,Apache依赖于硬件;
Nginx支持热部署,启动速度迅速,可以在不间断服务的情况下,对软件版本或者配置进行升级;
nginx是异步进程,多个连接可以对应一个进程:apache是同步多进程,一个连接对应一个进程:
Nginx高度模块化,编写模块相对简单,且组件比Apache少
高并发下nginx能保持低资源低消耗高性能;
Nginx配置简洁,Apache配置复杂:
Nginx的搭建和优化相关推荐
- Nginx配置和内核优化 实现突破十万并发
nginx指令中的优化(配置文件) worker_processes 8; nginx进程数,建议按照cpu数目来指定,一般为它的倍数. worker_cpu_affinity 00000001 00 ...
- 使用varnish + nginx + lua搭建网站的降级系统
前言 通常一个网站数据库挂掉后,后果将是非常严重的.基本上整个网站基本不可用了.对于一些网站来说,当数据库挂掉后,如果能提供基本的浏览服务,也是不错的.本文将尝试使用 varnish + nginx ...
- Nginx配置——搭建 Nginx 高可用集群(双机热备)
Nginx配置--搭建 Nginx 高可用集群(双机热备) https://blog.csdn.net/zxd1435513775/article/details/102508573?utm_medi ...
- 深入浅析nginx部署及简单优化
Nginx是lgor Sysoev为俄罗斯访问量第二的rambler.ru站点设计开发的.本文重点给大家介绍nginx部署及简单优化方案,感兴趣的朋友一起看看吧 Nginx是lgor Sysoev为俄 ...
- linux nginx 安装stream,Centos7下Nginx简单搭建与stream模块简单配置
一:Nginx安装 搭建环境:Centos 7 需要软件包:openssl.zlib.pcre.nginx (软件包下载地址个人网盘可能会失效:https://pan.baidu.com/s/1qYN ...
- Nginx高并发系统内核优化
Nginx高并发系统内核优化 Socket优化 Nginx 系统内核 文件优化 Nginx 系统内核 配置文件优化 Nginx配置文件 内核配置文件 PHP7配置文件 PHP-FPM配置文件 php- ...
- nginx轻松搭建自己的flv流媒体服务器
nginx搭建flv流媒体服务器 <!--[if !supportLists]-->一.<!--[endif]-->FLV视频发布方式简介 FLV视频有两总发布方式 <! ...
- Nginx+Tomcat搭建集群环境
Nginx+Tomcat搭建集群环境 ZeroOne01关注0人评论33534人阅读2018-05-05 14:15:39 集群概述与架构介绍 Tomcat集群能带来什么: 提高服务的性能,例如计算处 ...
- 服务器搭建和使用 ubuntu_Ubuntu中使用Nginx+rtmp搭建流媒体直播服务
一.背景 本篇文章是继上一篇文章<Ubuntu中使用Nginx+rtmp模块搭建流媒体视频点播服务>文章而写,在上一篇文章中我们搭建了一个点播服务器,在此基础上我们再搭建一个直播服务器, ...
最新文章
- virsh 常用操作
- 数据中心的清洁与扫除
- BeanUtils的方法
- mall整合RabbitMQ实现延迟消息
- 网络服务器监测系统,网络服务器监测系统研究与开发
- 写Java代码分别使堆溢出,栈溢出
- gamma软件linux安装图示,[转载]linux下安装GAMMA软件
- 网友神总结:我们继续用 XP 的十大理由
- LTE下行物理层传输机制(8)-DCI2A格式和下行双流的流量制约
- php报修小程序,微信小程序报修管理系统
- matlab 最小二乘法拟合_最小二乘法与高斯马尔科夫定理(无偏性、有效性)
- Git | git的简单使用教程
- UVALive - 5713
- 一种新型分割图像中人物的方法,基于人物动作辨认
- office2019卸载组件_office2019软件安装教程
- python报错NameError: name 'NA' is not defined
- 致广大而尽精微,极高明而道中庸
- java设置httpheaders_HttpClient 请求添加Header头部信息
- 对青浦区专利工作试点和示范企业给予20万元和30万元资助
- 关于判断力 - 正视事实是基础
热门文章
- 腾讯计算机视觉实习面经
- 什么用计算机算出自己在世界上活.了几天,据说科学家发现了一个“寿命计算器”,来算一下自己能活多久?...
- 【天光学术】研究生“憋论文”实况:别问,问就是在写论文
- Vue3使用组件库的tab切换echarts图表,图表出现宽度压缩变窄的问题
- 双十一什么数码好物值得买?双十一最值得买的数码好物分享
- SDNU1703字谜
- pytest.fixture详解
- 如何开发手机APP软件?
- [个人记录]半月板撕裂及SLAP损伤
- Vscode上使用opencv(C++接口,Windows篇)