nginx常见典型故障( 四 )

1.演示环境准备
[root@Nginx ~]# echo "Try-Page" > /soft/code/index.html[root@Nginx ~]# echo "Tomcat-Page" > /soft/app/Apache-tomcat-9.0.7/webapps/ROOT/index.html#启动tomcat[root@Nginx ~]# sh /soft/app/apache-tomcat-9.0.7/bin/startup.sh#检查tomcat端口[root@Nginx ~]# netstat -lntp|grep 8080tcp6 0 0 :::8080 :::* LISTEN 104952/java 2.配置Nginx的tryfiles
[root@Nginx ~]# cat /etc/nginx/conf.d/try.conf server { listen 80; server_name try.bgx.com; root /code; index index.html; location / { try_files $uri @java_page; } location @java_page { proxy_pass http://127.0.0.1:8080; }}#重启Nginx[root@Nginx ~]# nginx -s reload3.测试`tryfiles`
[root@Nginx ~]# curl http://try.bgx.com/index.htmlTry-Page#将/code/index.html文件移走[root@Nginx ~]# mv /code/{index.html,index.html_bak}#发现由Tomcat吐回了请求[root@Nginx ~]# curl http://try.bgx.com/index.html Tomcat-Page21.使用lvs+keepalived可以直接负载到后端的tomcat,nginx也可以负载到后端的tomcat,lvs是4层负载比nginx七层负载效率要高,为什么网上有很多资料要使用lvs+keepalived来负载nginx呢?直接使用lvs+keepalived负载到后端的tomcat不就可以了吗?
首先nginx是一个静态服务器,通常用来处理静态资源,比如你网页中的html,图片,css等,这些都属于静态资源,但nginx也可以作为反向代理,将你的动态资源请求给你分发到后端的应用服务器(此处就是分发到你的tomcat),由于nginx的高性能,尤其是处理静态资源的高效性!
其实两种方案都可以,一种是nginx+tomcat,这种通常用于中小型站点,
第二种是lvs+nginx+tomcat,这种适用于大型站点,因为lvs的并发能力远远大于nginx,所以用在最前端,接收用户请求,后面的nginx可以用于会话保持
两者是不同的应用场景,对于有session保持的场景,LVS是做不到的,因此LVS也替代不了Nginx……但如果你的应用不需要更复杂的7层控制,那LVS的性能会远远大于nginx!
22.nginx挂维护页面后,所有用户访问网站都会自动跳转至维护页面 。但公司内部IP需要能正常访问网站,不授维护页面干扰 。
1.代码如下
[root@web01 conf.d]# cat wh.confserver { listen 80; server_name limit.bgx.com; root /code; #------------------> #1.在server层下设定ip变量值为0 set $ip 0; #2.如果来源IP是10.0.0.101 102则设定变量为ip变量为1 #注意如果remote_addr无法获取真实客户端IP,请使用$http_x_forwarded_for if ($remote_addr ~* "10.0.0.101|10.0.0.102") { set $ip 1; } #3.如果ip变量为0,则跳转至/code/wh.html这个页面,否则不做任何处理 if ($ip = 0) { rewrite ^(.*)$ /wh.html break; } #------------------->如果想针对某个location进行操作,则将如上配置写入location中即可 location / { index index.html; }}2.效果展示

nginx常见典型故障

文章插图
 

【nginx常见典型故障】


推荐阅读