目录
修改nginx.conf配置文件,指定访问位置并添加stub_status配置
引文:一款轻量级的Http服务器软件Nginx,稳定、高效
Nginx服务的基本构建、访问控制方式和虚拟主机的搭建。
Nginx简介
-
Nginx是一个高性能的HTTP和反向代理服务器。
-
是一款轻量级的高性能的web服务器/反向代理服务器/电子邮件(IMAP/POP3)代理服务器,具有稳定性高、系统资源消耗低、对HTTP并发连接的处理能力高
-
单台物理服务器可支持30 000~50 000个并发请求。
Nginx并发连接受2个因素的影响:
①cpu个数
②本地物理服务器系统的最大打开文件数
(补充)Apache:Apache是以进程为基础的结构,进程要比线程消耗更多的系统开支,不太适用于多处理器环境,因此,在一个apache Web站点扩容时,通常是增加服务器或扩充群集节点而不是增加处理器。
Nginx和Apache的区别
Nginx是一个基于事件的web服务器,Apache是一个基于流程的服务器
Nginx避免子进程的概念,Apache是基于子进程的
Nginx在内存消耗和连接方面更好,Apache在内存消耗和连接方面一般
Nginx的性能和可伸缩性不依赖于硬件,Apache依赖于CPU和内存等硬件
Nginx支持热部署,Apache不支持热部署
Nginx对于静态文件处理具有更高效率,Apache相对一般
Nginx在反向代理场景具有明显优势,Apache相对一般
所谓的热升级也叫做热部署,或者平滑升级,也就是说,在不停止nginx服务的情况下,完成nginx的升级工作。但是这里有一个注意点,就是使用这种方法有一个前提,前提就是你在启动nginx时使用的是nginx二进制文件的绝对路径,而不是直接在命令行中输入"nginx"的方式启动的nginx服务,不通过绝对路径启动的方式通常是为了方便,配置了nginx相关的环境变量,如果没有通过绝对路径启动nginx,那么当你向nginx进程发送更新的信号时,nginx进程可能会无法找到新的二进制程序(由于没有找新版本的二进制程序,所以没有任何反应)
Nginx和Apache的优缺点比较
1、nginx相对于apache的优点∶
-
轻量级,同样起web服务,比apache占用更少的内存及资源
-
抗并发,nginx处理请求是异步非阻塞的,而apache是阻塞型的在高并发下,nginx能保持低资源低消耗高性能
-
高度模块化的设计,编写模块相对简
2、apache相对于nginx的优点∶
-
Rewrite比nginx的rewrite强大 (rewrite的主要功能就是实现统一资源定位符URL的跳转)
-
模块多,基本想到的都可以找到
-
少bug, nginx的bug相对较多
-
超稳定
总结:一般来说,需要性能的web服务,用nginx。若不需要性能只求稳定,就选用apache。
编译安装Nginx服务
关闭防火墙,将安装Nginx所需的软件包传到/opt目录下
安装依赖包
#安装依赖关系包
[root@zmj opt]#yum -y install pcre-devel zlib-devel gcc gcc-c++ make
创建运行用户、组便于管理
#新建用户 nginx 服务程序默认 以 nobody 身份运行,建议为其创建专门的用户账户,以便更准确的控制访问权限
[root@zmj opt]#useradd -M -s /sbin/nologin nginx
编译安装Nginx
cd /opt
tar zxvf nginx-1.12.2.tar.gz -C /opt/
cd nginx-1.12.2/
./configure \
--prefix=/usr/local/nginx \ #指定nginx的安装路径
--user=nginx \ #指定用户名
--group=nginx \ #指定组名
--with-http_stub_status_module #启用 http_stub_status_module 模块以支持状态统计
make && make install
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ #让系统识别nginx的操作命令
./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_stub_status_module
做软连接并启动nginx
做软连接,让系统识别nginx的操作命令
检查、启动、重启、停止Nginx服务
nginx -t #检查配置文件是否配置正确
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 -1 <PID号> #重载
kill -s HUP <PID号>
killall -1 nginx
killall -s HUP nginx
#日志分隔,重新打开日志文件
kill -USR1 <PID号>
#平滑升级
kill -USR2 <PID号>
版本升级
在官网下载需要升级的安装包
将需要升级的安装包拉入/opt目录下
解压
开始编译安装
安装Nginx相关依赖包
cd nginx-1.20.2
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module \
--with-http_ssl_module
[root@zmj nginx-1.20.2]# yum -y install gcc openssl openssl-devel pcre-devel zlib zlib-devel
进行备份
启动nginx服务并查看nginx版本
添加Nginx系统服务
法一、刷脚本
vim /etc/init.d/nginx
#!/bin/bash
#chkconfig: - 99 20
#description:Nginx Service Control Script
COM="/usr/local/nginx/sbin/nginx"
PID="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$COM
;;
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
exit 0
chmod +x /etc/init.d/nginx
chkconfig --add nginx #添加为系统服务
systemctl stop nginx
systemctl start nginx
法二
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
ExecrReload=/bin/kill -s HUP $MAINPID
ExecrStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
chmod 754 /lib/systemd/system/nginx.service
systemctl start nginx.service
systemctl enable nginx.service
赋权,除了root用户都不能修改
但是在我重启nginx的时候出现了错误报错
先检查nginx配置文件正否正确
输入nginx -t 命令,如果反回 successful表示配置文件无错误,否则说明配置文件有错误
如果配置文件有错误,修改配置文件后,先执行 nginx -t 命令检查配置文件无错误后,再执行nginx -s reload 重新加载配置文件命令
查看nginx服务状态,根据服务状态去判断报错原因
输入systemctl status nginx.service 命令,查看服务状态
看到80 端口被占用netstat -nap | grep 80
kill -9 pid
认识Nginx服务的主配置文件 nginx.conf
vim /usr/local/nginx/conf/nginx.conf
全局配置
#user nobody; #运行用户,若编译时未指定则默认为 nobody
worker_processes 1; #工作进程数量,可配置成服务器内核数 * 2
#error_log logs/error.log; #错误日志文件的位置
#pid logs/nginx.pid; #PID 文件的位置
I/O事件配置
events {
use epoll; #使用 epoll 模型,2.6及以上版本的系统内核,建议使用epoll模型以提高性能
worker_connections 4096; #每个进程处理 4096 个连接
}
#如提高每个进程的连接数还需执行“ulimit -n 65535”命令临时修改本地每个进程可以同时打开的最大文件数。
#在Linux平台上,在进行高并发TCP连接处理时,最高的并发数量都要受到系统对用户单一进程同时可打开文件数量的限制(这是因为系统为每个TCP连接都要创建一个socket句柄,每个socket句柄同时也是一个文件句柄)。
#可使用ulimit -a命令查看系统允许当前用户进程打开的文件数限制
/etc/security/limits.conf
2.6及以上内核 漏写
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;
##此选项允许或禁止使用socke的TCP_CORK的选项(发送数据包前先缓存数据),此选项仅在使用sendfile的时候使用
#tcp_nopush on;
##连接保持超时时间,单位是秒
#keepalive_timeout 0;
keepalive_timeout 65;
##gzip模块设置,设置是否开启gzip压缩输出
#gzip on;
##Web 服务的监听配置
server {
##监听地址及端口
listen 80;
##站点域名,可以有多个,用空格隔开
server_name www.lic.com;
##网页的默认字符集
charset utf-8;
##根目录配置
location / {
##网站根目录的位置/usr/local/nginx/html
root 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地址;
$remote_user:用来记录客户端用户名称;
$time_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.lic.com/test,会返回文件/usr/local/nginx/html/test/index.html
alias(别名配置):请求www.lic.com/test,会返回文件/usr/local/nginx/html/in
这里的index我打错了,注意
访问状态统计配置
先使用命令先使用命令/usr/local/nginx/sbin/nginx -V 查看已安装的Nginx是否包含HTTP_STUB_STATUS模块查看已安装的Nginx是否包含HTTP_STUB_STATUS模块
修改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 www.ly.com;
charset utf-8;
location / {
root html;
index index.html index.php;
}
##添加 stub_status 配置##
location /status { #访问位置为/status
stub_status on; #打开状态统计功能
access_log off; #关闭此位置的日志记录
}
}
}
重启服务、访问测试
可curl -s http://192.168.10.20/status结合awk与if语句进行性能监控
基于授权的访问控制
生成用户密码认证文件
yum install -y httpd-tools #安装密码认证工具
htpasswd -c /usr/local/nginx/passwd.db zhang #用户名为访问http的用户名,而不是系统用户
创建用户zahng,-c创建一个/usr/local/nginx/passwd.db用于存储用户信息
修改属主和权限,这一样除了root和nginx用户可读,其他用户和组都不可读,nginx用户登录后不可登录
[root@localhost nginx]# chmod 400 passwd.db
[root@localhost nginx]# chown nginx passwd.db
修改主配置文件相对应目录、添加认证配置项
vim /usr/local/nginx/conf/nginx.conf
......
server {
location / {
......
##添加认证配置##
auth_basic "secret";
auth_basic_user_file /usr/local/nginx/passwd.db;
}
}
重启服务,访问测试
会出现啥呢
基于客户端的访问控制
访问控制规则如下:
deny IP/IP 段:拒绝某个 IP 或 IP 段的客户端访问。
allow IP/IP 段:允许某个 IP 或 IP 段的客户端访问。
规则从上往下执行,如匹配则停止,不再往下匹配。
vim /usr/local/nginx/conf/nginx.conf
......
server {
location / {
......
##添加控制规则##
allow 192.168.10.19; #允许访问的客户端 IP
deny all; #拒绝其它IP客户端访问
}
}
systemctl restart nginx
1
老样子重启
基于域名的nginx虚拟主机
利用虚拟主机,不用为每个要运行的网站提供一台单独的 Nginx 服务器或单独运行一 组 Nginx进程,虚拟主机提供了在同一台服务器,同一组 Nginx 进程上运行多个网站的功 能。跟 Apache 一样,Nginx也可以配置多种类型的虚拟主机,分别是基于 IP 的虚拟主机、 基于域名的虚拟主机、基于端口的虚拟主机。 使用 Nginx搭建虚拟主机服务器时,每个虚拟 Web 站点拥有独立的“server{}”配置段, 各自监听的 IP地址、端口号可以单独指定,当然网站名称也是不同的。
为虚拟主机提供域名解析
echo "192.168.10.19 www.ming.com www.jie.com" >> /etc/hosts
为虚拟主机准备网页文档
mkdir -p /var/www/html/ming
mkdir -p /var/www/html/jie
echo "<h1>www.ming.com</h1>" > /var/www/html/ming/index.html
echo "<h1>www.jie.com</h1>" > /var/www/html/jie/index.html
修改nginx的配置文件
vim /usr/local/nginx/conf/nginx.conf
......
http {
......
server {
listen 80;
server_name www.ming.com; #设置域名www.ming.com
charset utf-8;
access_log logs/www.ming.access.log;
location / {
root /var/www/html/ming; #设置www.ming.com 的工作目录
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
server {
listen 80;
server_name www.jie.com; #设置域名www.jie.com
charset utf-8;
access_log logs/www.jie.access.log;
location / {
root /var/www/html/da;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
}
重启服务,访问测试
systemctl restart nginx
基于ip的nginx虚拟主机
ifconfig ens33:0 192.168.10.29 netmask 255.255.255.0
vim /usr/local/nginx/conf/nginx.conf
......
http {
......
server {
listen 192.168.10.19:80; #设置监听地址
server_name www.ming.com;
charset utf-8;
access_log logs/www.ming.access.log;
location / {
root /var/www/html/ming;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
server {
listen 192.168.10.29:80; #设置监听地址
server_name www.jie.com;
charset utf-8;
access_log logs/www.jie.access.log;
location / {
root /var/www/html/jie;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
}
systemctl restart nginx
设置虚拟网卡
编辑配置文件
重启服务
测试
基于端口的nginx虚拟主机
vim /usr/local/nginx/conf/nginx.conf
......
http {
......
server {
listen 192.168.10.29:8080; #设置监听地址
server_name www.ming.com;
charset utf-8;
access_log logs/www.ming.access.log;
location / {
root /var/www/html/ming;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
server {
listen 192.168.10.29:8088; #设置监听地址
server_name www.jie.com;
charset utf-8;
access_log logs/www.jie.access.log;
location / {
root /var/www/html/jie;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
}
systemctl restart nginx
修改配置文件
修改主机映射文件
重启服务,进行测试
总结
生产环境中我们可以根据不同的需求进行合理的配置