标签:nec for 权限 vim cti ams erer 字符 普通用户
[root@web02 ~] # vim /application/nginx/conf/nginx.conf
worker_processes 1;????????????????????????main区域
events {????????????????????????????????even区域
worker_connections 1024;
}
http {????????????????????????????????????http7层区域
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"‘
‘"$request_uri" "$args" "$url"‘;
access_log logs/access.log main;
sendfile on;
keepalive_timeout 65;
expires max;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
#gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php;
server {????????????????????????????server区域
listen 80;
server_name localhost;
set $url $scheme://$host$request_uri;
location / {????????????????????????????????location区域
root /application/nginx/html;
index index.html index.htm;
}
#if ($http_user_agent ~* "android|ios|iphone") {????if区域
# return 200 "OK \n";
#}
$remote_addr????????????????????客户端IP地址
$time_local????????????????????客户访问网站的时间
$request????????????????????????请求报文请求行的内容
$status????????????????????????响应报文的状态码信息
$body_bytes_sent????????????????文件的大小(访问文件所需的流量信息)
$http_referer????????????????????用户请求的内容是从哪访问过来的
$http_user_agent????????????????用户访问服务端所使用的的设备信息
$http_x_forwarded_for????????????前端负载均衡,后端的web服务器想知道真实的客户端地址
$http_uri????????????????????????用户请求uri的部分
$args????????????????????????用户请求中的参数 uri中?后面的内容
$scheme????????????????????????http或https
$host????????????????????????用户请求的域名
log_format main ‘$remote_addr - $remote_user [$time_local] "$request" ‘
‘$status $body_bytes_sent "$http_referer" ‘
‘"$http_user_agent" "$http_x_forwarded_for"‘
‘"$request_uri"‘;????????????????配置http_uri
access_log logs/access.log main;
?
[root@web02 ~] # curl -I 10.0.0.8/oldboy.jpg
HTTP/1.1 404 Not Found
Connection: keep-alive
log_format main ‘$remote_addr - $remote_user [$time_local] "$request" ‘
‘$status $body_bytes_sent "$http_referer" ‘
‘"$http_user_agent" "$http_x_forwarded_for"‘
‘"$request_uri" "$args"????????????????????http或者https?后面的内容
?
[root@web02 ~] # curl -I "10.0.0.8/oldboy.jpg?name=liangyuxing && password=123456"
HTTP/1.1 404 Not Found
Connection: keep-alive
Syntax:????set $variable value;
Default:????—
Context:????server, location, if
log_format main ‘$remote_addr - $remote_user [$time_local] "$request" ‘
‘$status $body_bytes_sent "$http_referer" ‘
‘"$http_user_agent" "$http_x_forwarded_for"‘
‘"$request_uri" "$args" "$url"‘;
?
server {
listen 80;
server_name localhost;
set $url $scheme://$host$request_uri;????????$url变量名 $scheme表示http或者https
?
[root@web02 ~] # curl -I "10.0.0.8/oldboy.jpg?name=liangyuxing"
HTTP/1.1 404 Not Found
Connection: keep-alive
匹配用户请求的uri信息
location /????????????????????????默认匹配
location /doc????????????????????????匹配/doc
location ~ /doc????????????????????模糊匹配doc
location ~*/doc????????????????????忽略大小写匹配doc,进行正则匹配
location = /doc????????????????????精确匹配doc
location ^~????????????????????????优先匹配,不识别正则
?
location / {
return ?200 ?"default \n";
}
location ~* ?"\.(jpg|jpeg|bmp|gif|png)$" {
return 200 "pic \n";
}
?
?!属于perl正则,可以(man?perlretut) 来查看更多的perl语言规则
?
[0-9]????????????????字
[^0-9]????????????字以外符号
[ \t\n\r\f] ?\s 匹配空字符 空格 tab 回车
[0-9a-zA-Z_] \w 数字 大小写字母 下划线
rewrite ^/batch_no/([0-9a-zA-Z]+)\.jpeg http://10.0.0.8/email_open_check?batch_no=$1 redirect;
if一般与nginx内置变量联合起来使用(可以做取反的操作)
Syntax:????if (condition) { ... }
Default:????—
Context:????server, location
server {
listen 80;
server_name localhost;
set $url $scheme://$host$request_uri;
root /application/nginx/html;
index index.html index.htm;
if ($http_user_agent ~* "android|ios|iphone") {????????????????如果是安卓或者ios或者iphone,就返
return 200 "OK \n";????????????????????????????????回200
}
?
[root@web02 ~] # curl -A iPhone 10.0.0.8
OK ????????????????????????????????????结果是OK
[root@web02 ~] #
location ~* "\.(jpg|jpeg|bmp|gif|png)$" {
if ($request_uri ~* "png|gif") {
return 404 "OK \n";
}
}
思路:采用标记法来做,先设置一个标记(flag=0),如果设置的变量=网段,标记为(flag=01),如果设置的变量=客户端oldboy,标记为(flag=012),最后在判断标记是不是012,如果是,则成功
set $flag 0;????????????????????0
if ( $remote_addr ~ "^172\.1\."){ ?
set $flag "${flag}1";????????????????01
}
if ($http_user_agent ~ "yucheng"){
set $flag "${flag}2";????????????????012
}
if ($flag = "012"){
return 200;????????????????????????如果是变量是012,则返回200
}
root????????????????????????指定用户的站点目录
alias????????????????????????替换用户请求的uri
location /oldboy {
root /app/alex;
}
?
curl -v http://www.oldboy.com/oldboy/lyx.html????????????curl -v http://www.oldboy.com/app/alex/oldboy/lyx.html
location /oldboy {
alias /app/alex
}
?
curl -v http://www.oldboy.com/oldboy/lyx.html????????????curl -v http://www.oldboy.com/app/alex/lyx.html
server_tokens off;????????????????????http服务区域
?
在重新配置,编译与安装
user www www; #main区域
client_max_body_size 10M;????????????????????将上传图片限制为10M
盗链:别人把你图片或者其他资源的链接复制下来,写入到他们的网站访问代码里面,这样客户在访问这张图片或者资源的时候可以正常的访问,但是消耗的流量是自己的流量
在自己的照片上面加上水印信息
根据$http_referter来判断,这个请求图片是不是通过大型网站跳转过来( ???????????????? if ( $http_referer !~* "baidu.com|google.com") {
return 403;
}
根据cookie防盗链(cookie存放在客户端的,你第一次访问网站,服务端会给你个cookie,下次再访问的时候,通过cookile来验证你是不是能匹配到服务端存放的session)
项目 | 共同点 | 区别 |
cookie | 存放用户的信息 |
|
session | 键值对的方式出现 |
|
?
网站 root root file 644 dir 755
上传 www www file 644 dir 755
爬虫工具
防止爬虫
if ($http_user_agent ~* "sogou|spider|bot") {
return 403;
}
location /static {
if ($request_method ~* "POST" ) {
return 403;
}
}
limit_conn_zone connection ????????????(连接数量)even区域
利用limit_conn_zone参数和$binary_remote_addr变量限制nginx单IP地址并发连接数
利用limit_conn_zone参数和$server_name r变量限制nginx虚拟主机总连接数
limit_conn_zone connection ????????????(连接速率)even区域
nginx工作进程数量修改为本身的cpu核心数或者cpu核心数2倍
worker_processes 1;????????????在主区域上
worker_cpu_affinity 0101 1010;????????????????????2核心
worker_cpu_affinity 0001 0010 0100 1000; ????????????4核心
worker_cpu_affinity 00000001 00000010 ………
worker_cpu_affinity auto????????????????????????表示自动配置cpu的亲和力,工作场景一般使用这个
?
数据库可以使用taskset -c 1,2,3来指定不同的核心来处理事情
nginx采用的是epoll模型(异步)
apache采用的是select模型(同步)
events {
use epoll;
}
events {
use epoll;
worker_connections 10240;????????????????????配置为比默认值大
}
worker_rlimit_nofile 65535;????????????????main区域
扩展:linux下的文件打开数
参数语法????????sendfile on | off;????????????????on为开启,off为关闭
放置位置????????http,server,location,if in location
keepalive_timeout 65;????????????????http区域
注意:压缩之前的文件一定要大点,不要什么大小的文件都压缩,有时候压缩只会适得其反
gzip on;????????????????????????????开启压缩功能
gzip_min_length 1k;????????????????适合压缩的文件最小为1K
gzip_buffers 4 16k;????????????????设置压缩缓存
#gzip_http_version 1.0;
gzip_comp_level 2;????????????????压缩级别,压缩数字越大,压缩率(占用空间)越小,cpu消耗越高
gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php;????????????哪些类型的文件进行压缩
location ~* \.(gif|jpg|jpeg|png|bmp|ico)$ {
root /var/www/img/;
expires 30d;????????????????????????设置只要匹配上这些后缀的信息,在浏览器缓存30天
expires max;????????????????????????设置只要匹配上这些后缀的信息,在浏览器缓存100年
}
proxy_cache_path /soft/cache levels=1:2 keys_zone=code_cache:10m
max_size=10g inactive=60m use_temp_path=off;
?
server {
listen 80;
server_name cache.oldboy.com;
#proxy_cache????????????????????开启缓存
#proxy_cache_valid????????????????状态码200|304的过期为12h, 其余状态码10分钟过期
#proxy_cache_key????????????????缓存key
#add_header????????????????????增加头信息, 观察客户端respoce是否命中 Nginx-Cache
"$upstream_cache_status";????????????缓存命中情况的信息 .
#proxy_next_upstream????????????????出现502-504或错误, 会跳过此台服务器访问下台
location / {
proxy_pass http://cache;
proxy_cache code_cache;
proxy_cache_valid 200 304 12h;
proxy_cache_valid any 10m;
add_header Nginx-Cache "$upstream_cache_status";
proxy_next_upstream error timeout invalid_header
http_500 http_502 http_503 http_504;
include proxy_params;
}
}
?
stream {????????????????????????????????配置在主区域
upstream ports {
server 10.0.0.7:9999;
server 10.0.0.51:6666;
}
server {
listen 8888;
proxy_pass ports;
}
[root@db ~] # nc -lk 6666
发现4层适合是nginx的负载均衡(反向代理)
标签:nec for 权限 vim cti ams erer 字符 普通用户
原文地址:https://www.cnblogs.com/liangyuxing/p/12079580.html