文章目录
一、LNMP架构
LNMP是指一组通常一起使用来运行动态网站或者服务器的自由软件名称首字母缩写。L指Linux,N指Nginx,M一般指MySQL,也可以指MariaDB,P一般指PHP,也可以指Perl或Python。
Linux是一类Unix计算机操作系统的统称,是目前最流行的免费操作系统;
Nginx是一个高性能的HTTP和反向代理服务器;
Mysql是一个小型关系型数据库管理系统。
PHP是一种在服务器端执行的嵌入HTML文档的脚本语言。
这四种软件均为免费开源软件,组合到一起,成为一个免费、高效、扩展性强的网站服务系统。
二、nginx功能配置
在上篇博客中,有详细的nginx安装编译过程,这里就不再赘述。为了实验方便,添加一个软链接,添加一个nginx用户:
ln -s /usr/local/nginx/sbin/nginx /usr/local/bin #/usr/local/nginx为安装路径
useradd -M -d /usr/local/nginx -s /sbin/nologin nginx #添加用户nginx
1. 并发优化
编辑nginx的配置文件:
[root@servera ~]# vim /usr/local/nginx/conf/nginx.conf
user nginx;
worker_processes 2; //工作进程数,一般情况下设置成CPU的可用核数
worker_cpu_affinity 01 10; //在高并发情况下,通过设置将CPU和具体的进程绑定来降低由于多核CPU切换造成的寄存器等现场重建带来的性能损耗。
#最多开启8个,cpu有多少核,就有几位数,1代表内核开启,0代表内核关闭
worker_rlimit_nofile 65535; //worker进程的最大打开文件数限制,如果没有设置,这个值为操作系统的限制.
events { //事件模块
worker_connections 65535; //单个工作进程并发连接数
multi_accept on //告诉nginx收到一个新连接通知后接收尽可能多的连接
use epoll; //use是个事件模块指令,用来指定Nginx的工作模式。对于Linux系统,epoll高校工作模式是首选。
}
httpd{
sendfile on; //开启文件高效传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,减少用户空间到内核空间的上下文切换。
#对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。
tcp_nopush on; //可以防止网络和磁盘IO阻塞
tcp_nodelay on; //可以防止网络和磁盘IO阻塞
}
对于单个工作进程能够并发处理的最大连接数
nginx作为http服务器时,最大连接数 = worker_processes * worker_connections
nginx作为反向代理服务器,最大连接=worker_processes*worker_connections/2
nginx内核优化
在内核层面,Linux下高并发socket最大连接数所受的各种限制:
(1)修改用户进程 可打开文件数限制
[root@servera ~]# vim /etc/security/limits.conf
nginx - nofile 65535
(2)为了防止开机后,命令设定的内核层面的限制不生效,可以将参数写在文件中,使其永久生效,注意文件必须以.conf结尾:
[root@servera ~]# cd /etc/sysctl.d/
[root@servera sysctl.d]# vim nginx.conf #文件必须以.conf
net.ipv4.ip_local_port_range = 1024 65535 //修改网络内核对TCP连接的有关限制,设定本地端口区间为1024-65536
net.core.somaxconn = 2048 //限制接收新TCP连接侦听队列的大小
net.ipv4.tcp_tw_recycle = 1 //启用tcp连接timewait快速回收和重用
net.ipv4.tcp_tw_reuse = 1
[root@servera sysctl.d]# sysctl --system //热更新内核参数
2. 日志
2.1 日志轮询
通过脚本的方式完成
[root@servera ~]# vim /opt/nginxlog.sh
#!/bin/bash
cd /usr/local/nginx/logs && mv access.log access_$(date +%F -d -1day).log //日志重命名,每天一次
kill -USR! `cat /usr/local/nginx/logs/nginx.pid`
[root@servera opt]# crontab -e //加入crontab定时任务
00 00 * * * /opt/nginxlog.sh &> /dev/null
[root@servera opt]# chmod +x /opt/nginx //给脚本执行权限
/测试
[root@servera logs]# /opt/nginxlog.sh //执行脚本
[root@workstation ~]# ab -c 1 -n 10000 http://192.168.1.136/index.html //用户端增加访问测试,一个并发一万次请求
2.2 日志禁用
在配置文件中可以禁用不必要的日志记录,以节省磁盘IO的消耗
[root@servera ~]# vim /usr/local/nginx/conf/nginx.conf
在server字段里面
location = /status { 设定访问/status时禁用不必要的日志,
//注意使用status模块,需要在安装编译时就添加参数 --with-http_stub_status_module
stub_status;
access_log off; //不查看正确的日志
error_log /dev/null; //错误日志导入垃圾箱
}
[root@servera ~]# nginx -s reload
此时 当访问http://192.168.1.136/status时,cat /user/local/lnmp/nginx/logs/access.log查看日志,不会有访问记录
3. nginx限流
创造测试环境,建立目录并放置访问内容:mkdir /usr/local/nginx/html/Download
3.1 控制单个IP并发连接数
[root@servera ~]# vim /usr/local/nginx/conf/nginx.conf
limit_conn_zone $binary_remote_addr zone=addr:10m; //放到http字段,都可以调用
//$binary_remote_addr 表示通过remote_addr这个标识来做限制
//zone=addr:10m 表示生成一个大小为10M,名字为one的内存区域
server {
listen 80;
server_name localhost;
location /Download/ { //在server字段添加,当访问Download目录时才会限流
limit_conn addr 1; //限制并发数
limit_rate 50k; //限制宽带
}
location / {
root html;
index index.html;
}
}
[root@servera ~]# nginx -s reload
3.2 限制单位时间内的请求数目以及速度限制
[root@servera ~]# vim /usr/local/nginx/conf/nginx.conf
/////http字段中设置 ///
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s; rate=1r/s表示允许相同标识的客户端的访问频次,这里表示每秒一次
/////在server字段限制//////
location / {
root html;
index index.html;
limit_req zone=one; //一秒钟只处理一个请求
limit_req zone=one burst=5 nodelay; //burst参数设定队列长度,可以等待处理,但是此时速率慢;
//添加nodelay参数后,只处理队列里面的,队列后的不处理。
}
[root@servera ~]# nginx -s reload
4. 自动索引
[root@servera ~]# vim /usr/local/nginx/conf/nginx.conf
http {
charset utf-8; //如果目录里面的内容存在Windows的字体,设定可以解决中文乱码
server {
location /Download/ {
limit_conn addr 1;
# limit_rate 50k;
autoindex on; //添加自动索引
}
[root@servera ~]# nginx -s reload
5. 静态文件缓存时间
缓存可以降低网站宽带 加速用户访问
[root@servera ~]# vim /usr/local/nginx/conf/nginx.conf
location ~ .*\.(jpg|gifpng)$ { //针对静态文件
expires 365d; //缓存时间设为一年
}
[root@servera ~]# nginx -s reload
6. 限制访问
6.1 站点目录和文件的限制
[root@servera ~]# vim /usr/local/nginx/conf/nginx.conf
location ~ ^/Download/.*\.(sh|php)$ { //禁止访问/Download下的文件
deny all;
}
[root@servera ~]# nginx -s reload
/////测试条件////
在/usr/local/nginx/html/Download中添加一个测试脚本,设定执行权限
6.2 限制IP
[root@servera ~]# vim /usr/local/nginx/conf/nginx.conf
/////第一种方式//////
location / {
root html;
index index.html;
allow 192.168.1.136; //设定允许的IP 也可以设定网段
allow 127.0.0.1; //设定本机可以访问
deny all; //除了允许的IP 其他IP限制访问
////第二种方式////////
location / {
root html;
index index.html ;
if ($remote_addr = 192.168.1.116) { //限制远程主机IP访问
return 403; //设定返回值为403
}
[root@servera ~]# nginx -s reload
7. nginx实现https访问
首先要确保nginx支持ssl模块,然后生成证书,编辑配置文件,进行https访问
[root@servera ~]# cd /etc/pki/tls/certs/
[root@servera certs]# make cert.pem //生成证书
[root@servera certs]# mv cert.pem /usr/local/nginx/conf //将证书移动到配置目录下
[root@servera ~]# vim /usr/local/nginx/conf/nginx.conf //编辑配置文件
# HTTPS server //删除以下注释
server {
listen 443 ssl;
server_name localhost;
ssl_certificate cert.pem;
ssl_certificate_key cert.pem; //将cert.kay改为cert.pem
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root html;
index index.html index.htm;
}
}
[root@servera ~]# nginx -s reload
8. nginx重定向
8.1 将禁止访问页面重定向到指定页面
防止域名恶意解析到服务器ip,不允许通过IP访问
[root@servera ~]# vim /usr/local/nginx/conf/nginx.conf
http {
server_tokens off; //禁止访问时 不显示nginx版本号
server {
listen 80;
server_name _;
return 500; //第一种 直接返回值
rewrite ^(.*)http://www.westos.org permanent; //第二种 使用rewrite可以让禁止访问的页面重定向到指定域名
}
}
[root@servera ~]# nginx -s reload
8.2 80重定向到443
[root@servera ~]# vim /usr/local/nginx/conf/nginx.conf
http{
.........
server {
listen 80;
server_name www.westos.org;
rewrite ^/(.*)$ https://www.westos.org/$1 permanent; //$1表示不重定向请求,只重定向域名
}
server {
listen 443 ssl;
server_name www.westos.org;
ssl_certificate cert.pem;
ssl_certificate_key cert.pem;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root html;
index index.html index.htm;
}
}
}
8.3 访问以bbs结尾重定向到bbs站点
示例:www.westos.org/bbs重定向到bbs.rhel7.com
[root@servera ~]# vim /usr/local/nginx/conf/nginx.conf
http{
.........
server {
listen 80;
server_name www.westos.org;
rewrite ^/bbs$ http://bbs.rhel7.com permanent;
rewrite ^/bbs/(.*)$ http://bbs.rhel7.com/$1 permanent; //当/bbs后面还有访问请求时,请求不发生变化
}
server {
listen 80;
server_name bbs.rhel7.com;
location / {
root /bbs;
index index.html;
}
}
}
[root@servera ~]# nginx -s reload
[root@servera ~]# mkdir /bbs //建立重定向的发布目录
[root@servera ~]# vim /bbs/index.html
[root@servera bbs]# cat index.html
bbs.rhel7.com
8.4 访问bbs域名重定向到主站的bbs
示例:bbs.rhel7.com重定向到www.westos.org/bbs
[root@servera ~]# vim /usr/local/nginx/conf/nginx.conf
http{
.........
server {
listen 80;
server_name bbs.rhel7.com;
if ($host = "bbs.rhel7.com"){
rewrite ^/(.*)$ http://www.westos.org/bbs/$1 permanent;
}
location / {
root html;
index index.html index.htm;
}
}
}