架构篇
一、Nginx常用问题
1、相同server_name多个虚拟主机优先级访问
2、location匹配优先级
3、try_files使用
4、Nginx的alias和root区别
5、用什么方法传递用户的真实IP
相同server_name多个虚拟主机优先级访问
test_server1.conf
server {
listen 80;
server_name testserver1 jeson.t.imooc.io;
location / {
root /opt/app/code1;
index index.html index.htm;
}
}
test_server2.conf
server {
listen 80;
server_name testserver2 jeson.t.imooc.io;
location / {
root /opt/app/code2;
index index.html index.htm;
}
}
检验格式是否正确时,会出现warn,启动时也会出现warn.
读取结果的是以文件所在的位置决定的,test_server1.conf 排在 test_server2.conf 前,所以读取到test_server1.conf的响应。
如果文件重命名,会读取到test_server2.conf的响应,因为它排在test_server3.conf前。
mv test_server1.conf test_server3.conf
location匹配优先级
~ \~* 优先级低,匹配到会继续匹配,直到匹配结束,如果没有比它优先级高的就选择本身匹配到的。
~区分大小写,~*不区分大小写
test_server1.conf
server {
listen 80 default_server;
server_name jeson.t.imooc.io;
access_log /var/log/nginx/log/host.access.log main;
root /opt/app;
location = /code1/ {
rewrite ^(.*)$ /code1/index.html break;
}
location ~ /code.* {
rewrite ^(.*)$ /code3/index.html break;
}
location ^~ /code {
rewrite ^(.*)$ /code2/index.html break;
}
}
3、Nginx的try_files的使用
按顺序检查文件是否存在
location / {
try_files $uri $uri/ /index.php;
}
test_server1.conf
location / {
root /opt/app/code/cache;
try_files $uri @java_page;
}
location @java_page {
proxy_pass http://127.0.0.1:9090;
}
4、Nginx的alias 和root区别
location /request_path/image/ {
root /local_path/image/;
}
htpp://www.imooc.com/request_path/image/cat.png
http://www.imooc.com/local_path/image/request_path/image/cat.png
location /request_path/image/ {
alias /local_path/image/;
}
htpp://www.imooc.com/request_path/image/cat.png
http://www.imooc.com/local_path/image/cat.png
5、用什么样的方法传递用户的真实IP地址
5、其他
Nginx: 413 Request Entity Too Large
1、用户上传文件限制 client_max_body_size
502 bad gateway
2、后端服务无响应
504 Gateway Time-out
3、后端服务执行超时
二、Nginx性能优化
1、性能优化考虑点
2、压测工具ab
3、系统与Nginx性能优化
性能优化考虑点
1、当前系统结构瓶颈
观察指标、压力测试
ab接口压力测试工具
1、安装
yum install httpd-tools
2、 使用
ab -n 2000 -c 2 http://127.0.0.1/
-n 总的请求数
-c 并发数
-k 是否开启长连接
系统与Nginx的性能优化
1、网络
2、系统
3、服务
4、程序
5、数据库、底层服务
系统优化
文件句柄
linux\Unix一切皆文件,文件句柄就是一个索引,默认设置为1024个,但对nginx中间件来说太少了,nginx的请求会达到上万
设置方式
系统全局性修改、用户局部性修改、进程局部性修改
/etc/security/limits.conf
结尾的几行,soft表示超过规定的数会提醒,hard表示超过规定的数会直接限制,一般设置一万以上就够了, * 表示全局,所有用户, root表示针对用户进行设置
root soft nofile 65535
root hard nofile 65535
* soft nofile 65535
* hard nofile 65535
针对进程进行设置
对nginx.conf进行添加
#针对进程的文件句柄的限制,放在http结构外的上面
worker_rlimit_nofile 35535;
CPU的亲和
把进程通常不会在处理器之间频繁迁移进程迁移的频率小、减少性能损耗。
双CPU表示我们有两个物理CPU能够同时工作,
查看当前有多少个物理CPU
[root@Manka ~]# cat /proc/cpuinfo|grep "physical id" |sort|uniq|wc -l
1
查看CPU的核心
[root@Manka ~]# cat /proc/cpuinfo|grep "cpu cores"|uniq
cpu cores : 1
输入top命令,再按键盘上的e键,能够展示当前核心的数量。
1 * 1 =1
如果有2个物理CPU,cpu cores:8,那么核心为
2 * 8 = 16
把work进程绑定到不同的cpu核心上去(忽略我上面的真实数据,买阿里云太垃圾了,还是个学生)
假设有2个物理CPU,cpu cores:8,核心为16
nginx.conf的配置如下
#user nobody;
#表示当前启动两个worker进程,官方建议我们启动的进程需要和我们CPU的核心一致,性能达到最好
worker_processes 16;
#cpu亲和配置
worker_cpu_affinity 0000000000000001 0000000000000010 0000000000000100 0000000000001000
0000000000010000 0000000000100000 0000000001000000 0000000010000000 0000000100000000 0000001000000000
0000010000000000 0000100000000000 0001000000000000 0010000000000000 0100000000000000
1000000000000000;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 10240;
}
http {
include mime.types;
default_type application/octet-stream;
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
重启nginx,再输入指令
ps -eo pid,args,psr | grep [n]ginx
结果如下:
如果下面配置,表示第一个worker进程可以在1,3,5,7,9,11,13,15的cpu核心上运行(用的比较少)
worker_processes 2;
worker_cpu_affinity 1010101010101010 0101010101010101;
新的版本出现自动配置,不需要我们配置,帮我们固定好
worker_cpu_affinity auto;
事件驱动器的使用,使用了epoll模型,优于selector模型
events {
use epoll;
#限制每一个worker进程能够处理多少个连接
worker_connections 10240;
}
Gzip module ,不支持IE6,会造成图片打不开,所以要除去,做兼容
gzip on;
gzip_disable "MSIE [1-6]\.";
gzip_http_version 1.1;
2、了解业务模式
接口业务类型、系统层次化结构
3、性能与安全
基于Nginx架构的安全篇
1、常见的恶意行为
爬虫行为和恶意抓取、资源盗用
基础防盗链功能-目的不让恶意用户能轻易的爬取网站对外数据
secure_link_module-对数据安全行提高加密验证和失效性,适合如核心重要数据
access_module-对后台、部分用户服务的数据提供IP防控
2、常见的应用层攻击手段
后台密码撞库-通过猜测密码字典不断对后台系统登录性尝试,获取后台登录密码
方法一、后台登录密码复杂度
方法二、access_module-对后台IP进行防控
方法三、预警机制
文件上传漏洞-利用这些可以上传的接口将恶意代码植入到服务器中,再通过url去访问以执行代码
早期nginx版本存在漏洞
http://www.imooc.com/upload/1.jpg/1.php
Nginx将1.jpg作为php代码执行
如何解决
location ^~ /upload {
root /opt/app/images;
if($request_filename ~* (.*)\.php) {
return 403;
}
}
SQL注入-利用未过滤/未审核用户输入的攻击方法,让应用运行本不应该运行的SQL代码
SQL注入的场景
场景实现:1、安装环境
yum install mariadb-server mariadb
yum install php php-fpm php-mysql
2、建立表格、插入测试数据
create table users(id int(11), username varchar(64), password varchar(64), email varchar(64));
insert into users (id, username, password, emial) values(1, 'jeson', md5('123'), 'jeson@imoocc.com');
启动数据库连接
systemctl start mariadb
查看连接状态
systemctl status mariadb
连接数据库
mysql -uroot -p
建立库和表
show databases;
create database info;
show tables;
use info;
create table users(id int(11), username varchar(64), password varchar(64), email varchar(64));
insert into users (id, username, password, emial) values(1, 'jeson', md5('123'), 'jeson@imoocc.com');
#退出
Bye
phpserver.conf的主要内容
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /opt/app/code/$fastcgi_script_name;
include fastcgi_params;
}
启动php-fpm的指令
php-fpm -D
php测试页面 phpinfo.php
<?php
phpinfo();
?>
浏览器输入ip/phpinfo.php 查看环境是否配置成功
login.html
validate.php
测试,当用户名输入 ‘ or 1 = 1 # 时,密码随便来个12321,这时能登陆成功
因为整条sql 语句变成了
select * from users where username = '' or 1 = 1 #' password = '12321';
这条语句能成功执行获取到用户数据。
4、场景:Nginx + LUA的安全waf防火墙
别人写的现成代码进行改写
https://github.com/loveshell/ngx_lua_waf
可以在linux上进行下载
#安装git命令
yum install git
#下载文件
git clone https://github.com/loveshell/ngx_lua_waf
结合github的文档说明进行设置就ok了
关于nginx的学习到这里结束了,从基础到进阶,到深入再到架构,挺系统的了解了一下,更多的还是要会用。加油呗!