nginx负载均衡,redis,虚拟主机,动静分离,rewrite,读写分离
虚拟主机安装nginx,看脚本
配置conf文件
设置虚拟主机
redis
nginx lua脚本连接redis
负载均衡
在keepalive后面/server(虚拟主机)前面加上upstream
upstream jf1 {
server 192.168.1.25:80;
server 192.168.1.26:80;
server 192.168.1.27:80;
server 192.168.1.28:80;
server 192.168.1.29:80;
server 192.168.1.30:80;
server 192.168.1.31:80;
server 192.168.1.32:80;
}
然后location后面加上
location / {
proxy_pass http://yinxingyouyou;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
proxy_pass http://jf1;
可以在后端均衡的apache上把index.html分别写成自己的ip,方便测试时验证
·作业 nginx均衡lamp,搭建两个lamp环境,分别是discuz和wordpress,用nginx均衡
注意,php环境会把负载均衡模块proxy_pass http://当成主机,必须在proxy_pass前面加
proxy_set_header Host $host; ($host把上面的server_name www.jf.com加到请求头里面)
(防止请求的头网址变成模块jfedu_web)
proxy_set_header X-Real-IP $remote_addr; (这一行是带上用户IP)
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; (这一行是防止模块变成主机)
动静分离
多location
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://jf1;
}
location ~.*\.(php|jsp|cg|asp|do)$
{
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://jf1;
}
location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$
{
root /usr/local/nginx/html/discuz;
expires30d;
#expires定义用户浏览器缓存的时间为3天,如果静态页面不常更新,可以设置更长,可以节省带宽和环节服务器压力,缓存到本地的文件状态码是304,可以ctrl+F5强制刷新清除缓存
}
.* 代表一个或多个字符 | 或的意思$结尾
静态文件走本地,动态文件走后端apache
location里的正则匹配
'=' > '完整路径' > '^~' > ~* > '/' = /还是/,优先级最低=加绝对路径,优先级最高
有的网站是把图片文件专门放在一个发布目录,音乐文件放在一个发布目录,视频文件放在一个发布目录。也有的综合性网站是把文件全部放在一个目录,比如discuz
304是缓存 报错:404:找不到页面 403:拒绝访问 502:bad gateway后端Apache停了
Rewrite
server_namewww.yinxing.com www.yinxingyouyou.com ;
if ( $host = www.yinxing.com ) {
rewrite ^/baidu$ http://www.yinxing.com/search permanent;
}
if ( $host = www.yinxingyouyou.com ) {
rewrite ^/(.*)$ http://www.yinxing.com/ permanent;
}
伪静态需要学习
日志切割
#!/bin/bash
#auto mv nginx log shell
#by author wugk
S_LOG=/usr/local/nginx/logs/access.log
D_LOG=/data/backup/`date +%Y%m%d`
echo -e "\033[32mPlease wait start cut shell scripts...\033[1m"
sleep 2
if [ ! -d $D_LOG ];then
mkdir -p$D_LOG
fi
mv$S_LOG$D_LOG
kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
echo "-------------------------------------------"
echo "The Nginx log Cutting Successfully!"
echo "You can access backup nginx log $D_LOG/access.log files."
最后在crontab中添加如下代码即可,每天晚上自动去切割日志:
0 0* * *
/bin/sh /data/sh/auto_nginx_log.sh>>/tmp/nginx_cut.log 2>&1
也可以用cronlog切割,自行百度
nginx防盗链
防止自己服务器的资源被别人引用
防盗链测试:在目标机器上新建页面
www.jf1.com/test.png
然后本机设置如下
TEST Nginx PNG
就可以盗取
禁止方法:在location后加一行
if ($host !='*.jf1.com') {
return 403; 也可以写200,就是虚假成功了
}
F12再看已经forbidden
NGINX
可以作为反向代理服务器,还可以按照调度规则实现动静分离,可以按照轮询、ip_hash、URL——hash、权重等多种方式对后端服务器做负载均衡,还支持后端服务器的健康检查。
用upstream均衡,均衡的时候有很多算法
upstream yinxingyouyou_web {
ip_hash
server 192.168.1.100 weight=1 max_fails=2 fail_timeout=10s;
server 192.168.1.101 weight=1 max_fails=2 fail_timeout=10s;
}
ip_hash是保持会话登录不变,但是不能保证平均均衡,所以一般这里都是rr轮询,然后在redis里设置回话保持,加在数据库前面的一层缓存
nginx+tomcat+redis+mysql-AB tomcat连redis,redis连tomcat有驱动,开发会写好
·upstream的 fail_timeout和max_fails,用来判断负载均衡upstream中的某个server是否失效。在fail_timeout的时间内,nignx与upstream中某个server的连接尝试失败了max_fails次,则nginx会认为该server已经失效。在接下来的 fail_timeout时间内(不宜太短,网络有延迟),nginx不再将请求分发给失效的server。
·如果max_fails=0,即关闭后端服务器健康检查,如果权重一样,那么每次请求都会有机会发到后端不可用的服务器。
·proxy_connect_timeout nginx与后端连接的超时时间,单位为秒,默认为60秒。我们在nginx错误日志里面看到的(110: Connection timed out),就是指nginx与后端连接已经超时。报502错误。
·proxy_read_timeout 建立连接后,nginx等候读取后端服务器响应的时间,默认为60秒。在一些比较繁忙的后端,比如线程数经常达到峰值了的tomcat,这个值注意不要设得太低,虽然线程数已经用光,但请求已经进入待队列之中。
·proxy_send_timeout nginx转发请求到后端的超时时间,默认为60秒,在这段时间内nginx没将请求数据发到后端将关闭连接。这个在网站有比较多像提交表单(post)上传内容之类的需要留意一下.
·keepalive_timout 时间值意味着:一个http产生的tcp连接在传送完最后一个响应后,还需要等待keepalive_timeout秒后,才开始关闭这个连接。就是会话保持。
·关闭atimevim /etc/fstab里
在根后面的defaults,后面加noatime,再后面1 1,是开始的时候检查磁盘,写成0就是不质检磁盘
关闭系统日志,系统日志warm,debug,info,error,2万台机器,ELK日志搜集平台
页:
[1]