控制以及常用命令:
1. 信号:
http://nginx.org/en/docs/control.html
2. 命令
nginx -t #检查配置文件语法
nginx -c file #检查指定路径的配置文件语法
nginx -s reload #热重载,修改配置文件后使用
nginx -s stop #强制停止
nginx -s quit #优雅的停止
nginx -h #查看帮助
nginx -v #版本
nginx -V #查看编译参数
http段
#nginx 的 default_server 指令可以定义默认的 server 出处理一些没有成功匹配 server_name 的请求,如果没有显式定义,则会选取第一个定义的 server 作为 default_server。
server{
listen 80;
server_name z.com;
location / {
root /home/html; #通常用绝对路径
index index.html;
}
}
server{
listen 80;
server_name 192.168.1.200;
location / {
root /var/www/html; #通常用绝对路径
index index.html;
}
}
日志管理
server中或者http中
eg: 记录在此位置的main类型日志
#access_log logs/access.log main;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#注:通常在同目录下有error.log可供调试时查看
location
分类:
精准匹配 = [优先]
正则匹配 ~
普通匹配 [匹配长的]
#demo1 index之坑
location =/ {
root /var/www/html; #通常用绝对路径
index index.htm;
}
location / {
root /usr/www/html; #通常用绝对路径
index index.html;
}
#访问 xxx/ --->精准匹配==> xxx/index.htm ---> /[普通匹配] --> usr/www/html/index.htm
#总结:对于目录类型的匹配,index之后有一次内部重定向
!!# location 匹配流程
首先进行精准匹配,成功则返回。
如果精准匹配不成功,对多条普通匹配进行验证并记录匹配长度最长的。
普通匹配成功后进行正则匹配,匹配成功则返回,如果不成功则返回普通匹配中匹配长度最长的。
rewrite 重写
if空格(条件){
}
set #设置变量
return #返回状态码
break #跳出rewrite
rewrite #重写
条件:
= 判断相等
~ 正则匹配
~* 不区分大小写的正则
-f,-d,-e 判断是否为文件,目录,存在
demo 封ip
location /test {
if ($remote_addr = 223.88.164.114){
return 403;
}
}
demo Edge重定向
location /test {
root /root/nngx;
index index.html;
if ($http_user_agent ~* Edge){
rewrite ^.*$ /test/ie.html break; #!!!===>此处将 所有的URI替换,因此相当于无上下文,直接变成 /test/ie.html,另外 此处的break不能少,否则将出现循环重定向(500)。
}
demo set用法
location /test {
if (条件){
set $site 1;
}
if (条件2){
set $site 2;
}
if (site == 1){
...
}
}
if (!-e $....)
demo
location /ecshop {
rewrite goods-(\d+)\.html /ecshop/goods.php?id=$1; #正则后向引用
rewrite ....N
}
注意:url重写时,如果正则中有 {} 则必须加 ""
root && alias && proxy_pass 的路径问题
【 root 】——根路径配置,用于访问文件系统,在匹配到location配置的URL路径后,指向【root】配置的路径,并把location配置路径附加到其后.
location ^~ /t/ {
root /www/root/html/;
}
如果一个请求的URI是/t/a.html时,web服务器将会返回服务器上的/www/root/html/t/a.html的文件。
【 alias 】——别名配置,用于访问文件系统,在匹配到location配置的URL路径后,指向【alias】配置的路径。
location ^~ /t/ {
alias /www/root/html/new_t/;
}
如果一个请求的URI是/t/a.html时,web服务器将会返回服务器上的/www/root/html/new_t/a.html的文件。注意这里是new_t,因为alias会把location后面配置的路径丢弃掉,把当前匹配到的目录指向到指定的目录。
注意:
使用alias时,目录名后面一定要加"/"且只能位于location块。
【 proxy_pass 】
是否会附加location配置路径与【proxy_pass】配置的路径后是否有"/"有关,有"/"则不附加
#例如
location ^~ /hello{
proxy_pass http://localhost:5000/;
#asdsad/hello/haha----------> 到后端的路由 /haha
proxy_pass http://localhost:5000;
#asdsad/hello/haha----------> 到后端的路由 /hello/haha
}
gzip
压缩类型:
gzip deflate compress sdch[Google推出,支持较少]
context: server,http,location
gzip on|off
gzip_buffers 32 4K #[压缩在内存中缓冲多少再输出,32块4K]
gzip_comp_level [1-9] #通常取6
gzip_min_length 4000 #低于此值的不压缩,4000字节起
gzip_types text/plain application/xml application/x-javascript text/css #对哪些类型进行压缩 /etc/nginx/mime.types
gzip_proxied #如果请求者是代理服务器如何缓存
gzip_disable #正则表达式,满足的URI不压缩
gzip_vary on|off #是否传输gzip压缩标志
gzip_http_version 1.1 #满足版本的进行压缩,此选项意义不大
#对于 图片/音乐 等资源文件压缩意义不大。比较小的文件压缩意义不大
expires
#缓存 提高网站性能
context: server location
expires 30s / m h d #===> 无304 not modified
#缓存图片demo
location ~* \.(jpg|jpeg|gif|png)
{
expires 3d;
}
扩展:304原理: etag [文件签名标签], last_modified_since 协议头
负载均衡
Syntax: upstream name { ... }
Default: —
Context: http
#demo
http{
upstream myser{
server localhost:1234;
server localhost:1235;
}
server {
location / {
proxy_pass http://myser;
}
}
}
策略:
1. 轮询[默认]
2. weight
{
server localhost:1234 weight=10
}
3. iphash===>解决session共享问题
{
ip_hash;
server localhost:1234;
}
4. fair(第三方):按照响应时间分配,响应时间短的优先分配
{
fair
}
第三方模块编译 --add-module=path
高性能服务器整体思路
对于开发:
减少请求--->合并CSS,背景图片,减少MySQL查询等
对于运维:
利用 expires 等使用浏览器缓存
其它:
CDN
对于最后不可避免的请求使用 集群 + 负载均衡解决
解决问题:
socket:
nginx :worker_connections
keepalive_timeout 0
system:
最大连接数:echo 10000 > /proc/sys/net/core/somaxconn
加快tcp回收:echo 1 > /proc/sys/net/ipv4/tcp_tw_recycle
空tcp重用:echo 1 > /proc/sys/net/ipv4/tcp_tw_reuse
洪水攻击:echo 0 >/proc/sys/net/ipv4/tcp_syncookies
文件:
nginx: 子进程允许打开文件数: worker_rlimit_nofile 10000
系统: ulimit -n 100000
#dmesg
使用ab测压
#yum install httpd-tools
ab -c[并发] -k[keep alive] -n[number]
高可用
高可用:
nginx宕机
keepalived,虚拟IP
yum install keepalived -y