1.Nginx概述
一款高性能、轻量级Web服务软件
稳定性高
系统资源消耗低
对HTTP并发连接的处理能力高
单台物理服务器可支持30000~50000个并发请求
2.编译安装Nginx服务
2.1 关闭防火墙将nginx所需软件包到/opt目录下
systemctl stop firewalld
systemctl disable firewalld
setenforce 0nginx-1.12.0.tar.gz
tar zxvf nginx-1.12.2.tar.gz
2.2 安装依赖包
yum -y install pcre-devel zlib-devel gcc gcc-c++ make
2.3 创建运行用户、组
(Nginx 服务程序默认以 nobody 身份运行,建议为其创建专门的用户账号,以便更准确地控制其访问权限)
useradd -M -s /sbin/nologin nginx
2.4 编译安装Nginx
cd /opt
tar zxvf nginx-1.12.0.tar.gz -C /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 && make installln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ #让系统识别nginx的操作命令
2.5 检查、启动、重启、停止 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 nginxkill -1 <PID号> #重载
kill -s HUP <PID号>
killall -1 nginx
killall -s HUP nginx
#日志分隔,重新打开日志文件
kill -USR1 <PID号>
#平滑升级
kill -USR2 <PID号>
2.6 添加 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 1esac
exit 0chmod +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.targetchmod 754 /lib/systemd/system/nginx.service
systemctl start nginx.service
systemctl enable nginx.service
3.添加Nginx系统服务并赋予执行权限
chmod +x /etc/init.d/nginx
chkconfig --add nginx
4.Nginx主配置文件
user_nobody 运行用户,若编译时未指定则默认为nobdy
worker_processes 1; 工作进程数量 可配置为内核数 * 2[一般1就够了]
error_log logs/error.log; 错误日志的位置
pid logs/nginx.pid PID文件位置
4.1 如何提高性能
I/O事件配置
events {use epoll;#2.6以上版本的系统核心建议使用epoll模型以提高性能worker _connections 4095#每个进程处理 4096 个连接
4.2 如何提高每个进程的连接数?
在Linux平台进行高并发TCP连接处理时,最高的并发数量都要受到系统对
用户单一进程同时可打开文件数量的限制。每个TCP连接都要创建一个socket
每个socket句柄同时也是一个文件句柄
ulimit -n 65535 临时修改本地每个进程可以同时打开的最大文件数
ulimit -a 可以查看当前系统用户可以打开最大文件数[open files项]
5.HTTP配置
http {##文件扩展名与文件类型映射表include mime.types;##默认文件类型default_type application/octet-stream;##日志格式设定#log_format main '$remote_ addr - $remote. user [stime_ local] "$request" '# '$status $body_ bytes_ sent "Shttp_ 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服务的监听配置##Web服务的监听配置server (##监听地址及端口listen 80;##站点域名,可以有多个,用空格隔开server.name www.gt0604.com;##网页的默认字符集charset utf-8;##根目录配置location / {##网站根目录的位置/usr/local/nginx/htmlroot html;##默认首页文件名index index.html index.php;}##内部错误的反馈页而error_page 500 502 503 504 /50x.html;#错误页而配置location = /50x.html {root html;}}
}
5.1 测试
6.日志格式设定
$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_passlocation常见配置指令,root、alias、proxy_passroot(根路径配置):请求www.gt0604.com/hello,会返回文件/usr/local/nginx/html/test/index.htmlalias(别名配置):请求www.gt0604.com/hello,会返回文件/usr/local/nginx/html/index.html
7. 访问状态统计
7.1 查看是否有HTTP_STUB_STATUS模块
nginx -t
7.2 进入nginx主配置文件并添加stub_status
cd /usr/local/nginx/conf
cp nginx.conf{,.bak}
vim /usr/local/nginx/conf/nginx.conf
.....
http {
.....server {listen 80;server_name www.gt0604.com;charset utf-8;location / {root html;index index.html index.html;}##添加stub_status 配置##location /status { #访问位置为/statusstub_status on; #打开状态统计功能access_log off; #关闭此位置的日志记录}}
}
8.Nginx访问控制
8.1 基于授权的访问控制
生成用户密码认证文件
也可通过在线生成
yum install -y httpd-toolshtpasswd -c /usr/local/nginx/passwd.db bb
8.2 如果删除了普通用户的权限就需要设置文件的属主
chown nginx /usr/local/nginx/passwd.db
chmod 700 /usr/local/nginx/passwd.db
8.3 修改主配置文件相对应目录,添加认证配置项
vim /usr/local/nginx/conf/nginx.conf
......server {location / {......##添加认证配置##auth_basic "secret" ;auth_basic_user_file /usr/local/nginx/passwd.db;}}
8.4 基于客户端的访问控制
访问控制规则如下
deny IP/IP 段:拒绝某个 IP 或 IP 段的客户端访问
allow IP/IP 段:允许某个 IP 或 IP 段的客户端访问
规则从上往下执行,如匹配则停止,不再往下匹配
vim /usr/local/nginx/conf/nginx.confserver {location / {#添加控制规则#192.168.1.1#192.168.1.0/24deny 192.168.0.10;#拒绝访问的客户端IPallow all;#允许其它IP客户端访问}}systemctl restart nginx
9.Nginx虚拟主机
9.1 基于域名的虚拟主机
echo "192.168.148.10 www.gt.com www.ggtt.com" >> /etc/hosts
9.2 为虚拟主机准备网页文档
mkdir -p /var/www/html/accp
mkdir -p /var/www/html/benet
echo "<h1>www.gt.com</h1>" > /var/www/html/gt/index.html
echo "<h1>www.ggtt.com</h1>" > /var/www/html/ggtt/index.html
9.3 修改Nginx的配置文件
vim /usr/local/nginx/conf/nginx.conf
http {server {listen 80;server_name www.ggtt.com;charset utf-8;access_log logs/www.ggtt.com.access.log;location / {root /var/www/html/ggtt;index index.html index.html;}error_page 500 502 503 504 /50x.html;location = 50x.html{root html;}}server {listen 80;server_name www.gt.com;charset utf-8;access_log logs/www.gt.access.com.log;location / {root /var/www/html/gt;index index.html index.html;}error_page 500 502 503 504 /50x.html;location = 50x.html{root html;}}
}
nginx -t
systemctl restart nginx
9.4 基于IP的虚拟主机
ifconfig ens33:0 192.168.148.20/24
vim /usr/local/nginx/conf/nginx.conf
http {server {listen 192.168.148.10:80;server_name www.gt.com;charset utf-8;access_log logs/www.gt.access.log;location / {root /var/www/html/gt;index index.html index.php;}error_page 500 502 503 504 /50x.html;location = 50x.html{root html;}}server {listen 192.168.148.20:80;server_name www.ggtt.com;charset utf-8;access_log logs/www.ggtt.access.log;location / {root /var/www/html/ggtt;index index.html index.html;}error_page 500 502 503 504 /50x.html;location = 50x.html{root html;}}
}
nginx -t
systemctl restart nginx
9.5 基于端口的虚拟主机
vim /usr/local/nginx/conf/nginx.conf
http {server {listen 192.168.148.10:233;server_name www.ggtt.com;charset utf-8;access_log logs/www.ggtt.access.log;location / {root /var/www/html/ggtt;index index.html index.html;}error_page 500 502. 503 504 /50x.html;location = 50x.html{root html;}}server {listen 192.168.148.10:8080;server_name www.gt.com;charset utf-8;access_log logs/www.gt.access.log;location / {root /var/www/html/gt;index index.html index.php; }error_page 500 502 503 504 /50x.html;location = 50x.html{root html;}}
}
nginx -t
systemctl restart nginx.service