Nginx---笔记六

Nginx常见问题:
1.相同server_name多个虚拟主机优先级访问
server{
listen 80;
server_name testserver1 phantom.wgw.io;
location{
...
}
}

server{
listen 80;
server_name testserver2 phantom.wgw.io;
location{
...
}
}

实验:
conf.d目录下新建:
test_server1.conf ,test_server2.conf
#两者的区别是:
>> diff test_server1.conf test_server2.conf
server_name testserver1 phantom.wgw.io;
server_name testserver2 phantom.wgw.io;

root /opt/app/code1;
root /opt/app/code2;
>> nginx -tc /etc/nginx/nginx.conf
>> nginx -s reload -c /etc/nginx.conf
结论:
相同server_name多个虚拟主机,nginx会最先读取他先读到的那一个

2.location匹配优先级
= 进行普通字符精确匹配,也就是完全匹配
^~ 表示普通字符匹配,使用前缀匹配
~ \~* 表示执行一个正则匹配()

实验:
server{
listen 80;
server_name testserver1 phantom.wgw.io;

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;
}

说明:
首先会判断请求的url的内容是否存在,如果存在返回,
如果没有则在请求后加"/",类似于重定向,然后取寻找这个路径下面是否有
,如果没有,交给index.php处理

实验:
server{
listen 80;
server_name testserver1 phantom.wgw.io;

location / {
root /opt/app/code/cache;
try_files $uri @jave_page;
}

location @jave_page{
proxy_pass http://127.0.0.1:9090;
}
}

4.Nginx的alias和root的区别:

location /request_path/image/ {
root /local_path/image/;
}

说明:
当请求http://www....com/request_path/image/cat.png时,
它实际上请求的是:/local_path/image/request_path/image/cat.png

location /request_path/image/ {
alias /local_path/image/;
}

说明:
当请求http://www....com/request_path/image/cat.png时,
它实际上请求的是:/local_path/image/cat.png


5.用什么方法传递用户真实的IP地址:

IP1(用户)<---->IP2(代理1)[set x_real_ip=$remote_addr]<--->IP3 IPn(代理n)<---->IP5(后端服务)[$x_real_ip=IP1]


6.其他:
Nginx: 413 Request Entity Too Large
1.用户上传文件限制 client_max_body_size
502 bad gateway
2.后端服务无响应
504 Gateway Time-out
3.后端服务执行超时
Nginx性能优化:
1.性能优化考虑点
1.1了解当前系统结构瓶颈
1.1.1 观察指标、压力测试
1.2了解业务模型
1.2.1 接口业务类型、系统层次化结构
1.3性能与安全
2.ab接口压力测试工具:
安装:
yum install httpd-tools
使用
ab -n 2000 -c 2 http://127.0.0.1

说明:
-n 表示总的请求数
-c 并发数
-k 是否开启长连接

实验:
server{
listen 80;
server_name testserver1 phantom.wgw.io;

location / {
root /opt/app/code/cache;
try_files $uri @python_page;
}

location @python_page{
proxy_pass http://127.0.0.1:9090;
}
}

>> ab -n 2000 -c 2 http://127.0.0.1/wgw.html
输出如下:
...
Concurrency Level: 2
Time taken for tests: 11.014 seconds # 总耗时
Complete requests: 2000
Failed requests: 0
Write errors: 0
Non-2xx responses: 2000 # 非200请求
Total transferred: 764000 bytes
HTML transferred: 464000 bytes
Requests per second: 181.59 [#/sec] (mean) # qps每秒多少请求
Time per request: 11.014 [ms] (mean) # 单个请求花费的时间
Time per request: 5.507 [ms] (mean, across all concurrent requests) # 服务端处理单个请求的时间
Transfer rate: 67.74 [Kbytes/sec] received # 传输速率,判断网络是否存在瓶颈

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.1 0 1
Processing: 4 11 3.3 10 34
Waiting: 4 11 3.3 10 34
Total: 4 11 3.3 10 34
...
3.系统与Nginx的性能优化
网络:
流量,丢包等
系统:
硬件【磁盘的损坏,速率,系统负载饱和,内存使用率,系统稳定性】
服务:
连接的优化,内核性能的优化,对于http服务请求的优化
程序:
接口性能,处理速度,执行效率
数据库,底层服务


3.1 文件句柄:

文件句柄:
linux/unix ---一切皆文件,文件句柄就是一个索引
设置方式:
系统全局性修改,用户局部性修改,进程局部性修改

>> vi /etc/security/limits.conf
如加入以下两句:
root (用户) soft nofile 65535 # 局部
root hard nofile 65535
*(所有用户) soft nofile 65535 # 全局
* hard nofile 65535
>> vi /etc/nginx/nginx.conf
worker_rlimit_nofile 35535 # 针对nginx进程的文件句柄的限制
3.2 cpu亲和:

把进程通常不会在处理器之间频繁迁移进程迁移的频率小,减少性能损耗
实验:
# 查看系统有多少个cpu
>> cat /proc/cpuinfo | grep "physical id" |sort|uniq(去重)|wc -l(统计)
# 查看cpu核心
>> cat /proc/cpuinfo | grep "cpu cores" | uniq
# 输入top,按键盘1键就可以展示cpu的核心数量,以及他们的进程使用率是怎样的
>> vi /etc/nginx/nginx.conf
work_processes 16; # 启动的进程数需要和当前主机cpu的核心数一致
worker_cpu_affinity 0000000000000001 # (总共16位对应cpu核心数,这个代表第一个进程在第一个核心上面运行) 00000000000000010 ....以此类推;

worker_cpu_affinity auto; # nginx会自动帮我们固定好cpu不需要写一大串了
>> ps -eo pid,args(进程名),psr(使用的cpu是哪一个) | grep [n]ginx

10638 nginx: master process nginx 0
22428 nginx: worker process 0
22429 nginx: worker process 1
3.3 Nginx的通用配置优化:
user nginx;
worker_processes 2; # 启动的进程数需要和当前主机cpu的核心数一致
worker_cpu_affinity auto; # nginx会自动帮我们固定好cpu不需要写一大串了

error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;

worker_rlimit_nofile 35535 # 针对nginx进程的文件句柄的限制,建议调整到10000以上

events {
use epoll;
worker_connections 10240; # 限制每一个work的进程能够处理多少个连接
}


http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
charset utf-8; # 字符集

log_format main '$http_user_agent' '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log /var/log/nginx/access.log main; # 对于不需要的日志进行关闭

sendfile on;
#tcp_nopush on; # 如果是静态资源服务器的话可以把它打开
#tcp_nodeny on; # 在动态接口,keepalive打开的情况下可以把他打开

keepalive_timeout 65;

gzip on;
gzip_disable "MISE [1-6]\."; # 兼容,对于IE6及以下的不进行压缩
gzip_http_verson 1.1;

include /etc/nginx/conf.d/*.conf;
}
Nginx安全篇:

1.常见的恶意行为:
1.1 爬虫行为和恶意抓取、资源盗用
1.1.1 基础防盗链功能 --目的不让恶意用户能轻易的爬取网站对外数据
1.1.2 secure_link_module --对数据安全性提高加密验证和失效性,适合如核心重要数据
1.1.3 access_module --对后台、部分用户服务的数据提供IP防控
2.常见的攻击手段:
2.1 后台密码撞库:
通过猜测密码字典不断对后台系统登陆性尝试,获取后台登陆密码
方法一:后台密码复杂度
方法二:access_module -对后台提供IP防控
方法三:预警机制
2.2 文件上传漏洞:
利用这些可以上传的接口将恶意代码植入到服务器中,再通过url去访问以执行代码
如:http://www.wgw.com/upload/1.jpg/1.php
此时,他会将1.jpg作为php代码执行
location ^~ /upload {
root /opt/app/images;

if ($request_filename ~* (.*)\.php){
set $php_url $1;
}
}
2.3 SQL注入:
利用未过滤/未审核用户输入的攻击方法,让应用运行本不应该运行的SQL代码
场景:
用户---http-->Nginx+LUA--Fastcgi-->PHP---sql-->mariadb

环境准备:
#1、安装环境
yum install mariadb-server mariadb
yum install php php-fpm php-mysql

systemctl start mariadb
#2、建立表格、插入测试数据
create database info;
use info;

create table users(id int(11),username varchar(64), password varchar(64), email varchar(64));

insert into users (id,username,password,email) values(1,'jeson',md5('123'),'jeson@imoocc.com');

3.配置phpserve.conf

>> vi /etc/nginx/conf.d/phpserver.conf
server {
listen 80;
server_name localhost;
root /opt/app/code;

#charset koi8-r;
access_log /var/log/nginx/log/host.access.log main;


location / {
index index.php index.htm;
}

error_page 500 502 503 504 404 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME /opt/app/code/$fastcgi_script_name;
include fastcgi_params;
}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
>> php-fpm -D #启动php
>> nginx -s reload -c /etc/nginx/nginx.conf
访问:http://192.168.205.10/login.html
账户: ' or 1=1#
密码: 随意
竟然登录成功
sql:
select * from users where username=" or 1=1#' and password='asffdafsd'

# 这里会判断or 1=1?返回true,后面有个#号,所以后面的代码都被注释不能执行了
如何防止:
1. Nginx+LUA防火墙:

===>waf[Nginx LUA]{拦截cookie类型攻击;拦截异常post请求;拦截cc攻击;
拦截url攻击; 拦截arg}===>后台服务[JAVA PHP PYTHON]

http://github.com/loveshell/ngx_lua_waf
Nginx漏洞和新版本特性:
查看版本更新描述
基于 Nginx的中间件架构:
1.了解需求
1.1 定义Nginx在服务体系中的角色
静态资源服务,代理服务, 动静分离
1.2 静态资源服务的功能设计
类型分类,浏览器缓存,防盗链,流量限制,防资源盗用,压缩
1.3 代理服务
协议类型,正向代理,反向代理,负载均衡,代理缓存,头信息处理,LNMP,Proxypass,分片请求
2.设计评估
硬件: 动态资源服务:CPU,内存要求比较高
静态资源服务:硬盘的容量转速要求,读写效率比较高
系统: 用户权限,日志目录存放
关联服务:LVS,keeplive,syslog,Fastcgi

3.配置注意事项
合理配置
了解原理
关注日志

转载于:https://www.cnblogs.com/wgwblogs/p/11207520.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值