1,worker_processes cpu*核数;#工作进程数
2,events {
worker_connections 1024;
}#最大连接数
3,日志
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';#系统默认的日志格式
log_format simple '$remote_addr------------$remote_user[$time_local]"$request"'
'"$http_user_agent" "$http_x_forwarded_for"';#simple 自命名格式
#access_log logs/access.log simple;#默认使用格式
相关说明解释
1.$remote_addr 与$http_x_forwarded_for 用以记录客户端的ip地址;2.$remote_user :用来记录客户端用户名称;
3.$time_local : 用来记录访问时间与时区;
4.$request : 用来记录请求的url与http协议;
5.$status : 用来记录请求状态;成功是200,
6.$body_bytes_s ent :记录发送给客户端文件主体内容大小;
7.$http_referer :用来记录从那个页面链接访问过来的;
8.$http_user_agent :记录客户端浏览器的相关信息;
4,rewrite
location / {
root /var/www/maiji/yii/basic/web;
index index.php index.html index.htm;
if (!-e $request_filename ){#!-e $request_filename:路径下的文件、文件夹不存在
rewrite ^/(.*)$ /index.php?r=$1 last;#从写到指定路径下
}
}
5,动静分离
location ~ \.(jpg|png|gif|jpeg)${
proxy_pass http://ip:port;
}
6,负载均衡,集群
upstream testserver{
server 192.168.1.2:80 weight=1 max_fail=2 fail_timeout=30s
server 192.168.1.3:80 weight=1 max_fail=2 fail_timeout=30s
server 192.168.1.4:80 weight=1 max_fail=2 fail_timeout=30s
}
1,负载均衡策略要做到:session 共享
PHP.ini设置session保存路径
session.save_handler = redis
session.save_path ="tcp://localhost:6379",
2,每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
#--问题是达不到觉对的均衡
upstream backserver {ip_hash;
server 192.168.0.14:88;
server 192.168.0.15:80;
}