1.基本配置
worker_processes auto|3; 指定使用的核数,默认auto,也可指定 一般为自身核数-1,可用lscpu查看
events {
worker_connections 1024; 最大并发连接数,最大并发响应 数 worker_processes * worker_connections
}
http {
keepalived_timeout 65 0表示禁止长连接
keepalived_request 长连接最大资源数,默认100
keepalived_disable none|browser
server {
listen 127.0.0.1:80 default_server 默认虚拟主 机
server_name www.magedu.com
匹配优先级,精确-左匹配-右匹配-正则匹配
www.magedu.com
*.magedu.com
www.magedu.*
~^.*\.magedu\..*$
root /usr/share/nginx/html
location / {
}
匹配优先级,精确-左匹配-正则匹配-不带符号
= 精确
^~ 左匹配
~ 正则匹配,区分大小写
~* 正则匹配,不区分大小写
error_page 404 /404.html;
location=/40x.html{
}
}
}
2.状态页
location /status {
stub_status;
}
Active connections 活动客户端链接数
Accepts 已接收的客户端链接总数
Handled 已处理完的客户端请求总数
Requests 总请求数
Request-handled 被拒绝的总数
Reading 处于读取客户端报文首部的数量
Writing 发送响应报文数
Walting 处于等待客户端发出请求的空闲连接,值过大有可能是
keepalive timeout设置过长
3.防止盗链
location ~* \.(gif|jpg|png|bmp)$ {
valid_referers none blocked *.magedu.com server_names ~\.google\. ~\.baidu\.;
if ($invalid_referer) {
return 403;
}
}
4.php-fpm
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000
fastcgi_index index.php
fastcgi_param SCRIPT_FILENAME $document_root $fastcgi_script_name;
include fastcgi_params;
}
5.动静分离
location /{
proxy_pass http://192.168.1.11;
}
location ~ \.php${
proxy_pass http://192.168.1.12;
}
6.负载均衡
upstream backend {
server 192.168.1.11 weight=1;
server 192.168.1.12 weight=1;
ip_hash 相当于SH | least_conn 最少链接;
down; 手动标记下线
backup 192.168.1.13; 相当于sorry server
}
location /{
proxy_pass http://backend;
}