PHP网站首页打不开的原因讲起
最近有个网站首页打不开,偶尔报504错误,如图所示,这是nginx直接返回的。
今天下午16:00多又出现了,看了下阿里云数据库连接,其实在晚上2:00也出现了一次。这个图是后来问题已经解决了获取的,数据库连接的请求数已经降下来了。
现在似乎是数据库压力很大,由于我们的数据库服务器同时有好几个库在线上,并不确定是其他系统把数据库拖垮还是当前这个系统的原因。
使用语句查询数据库的会话数
SQLSERVER 查看当前连接数
Select * from sys.dm_exec_connections --此命令可以看到有多少人在连
Select * from sys.dm_exec_sessions --此命令可以看到有多少会话,一个连接可以有多个会话
从图中大致定位到还是之前报504的网站对数据库的请求数很多。
定位到具体应用就可以上对应的服务器去进一步去查
先看CPU是比较正常的
再查PHP-FPM的进程数
正常情况下php进程只有10个左右,一下子变成这么多肯定说明是外界有很大的请求数。
但是为了确定是不是php出现什么异常导致进程无法结束,所以结束所有进程,重启php-fpm.。
发现没过多久又出现很多的进程数,所以可以得出结论:这说明nginx报504的原因是外界的请求是持续的,导致数据库压力巨大请求不过来,所以nginx报504。
然后查这个时候的nginx日志最近500条数据
#tail -n 500 /usr/local/nginx/logs/access.log >>500_20160218-1712.txt
从日志里看出主要是百度和搜狗的蜘蛛在爬行,百度是一直在爬行的,应该是比较稳定不会导致网站突然流量剧增,而且据说百度会控制访问的频率,保证不把网站爬挂。
看来后面的日志,发现这个时间段搜狗的蜘蛛爬行数量是非常多的,由于蜘蛛是用集群在爬,所以屏蔽IP是没用的,只能在nginx里屏蔽请求头。
server { listen 80; root /www/; index index.php; #禁止特定爬虫的抓取 if ($http_user_agent ~* "Sogou web spider") { return 403; }
然后重新结束php进程,启动php进程,发现整个进程是非常平稳的维持在10个
说明就是搜狗搞的事情,当务之急只能先这么着了,谁让搜狗没有多少用户群呢,后续的解决方案还是使用缓存,所以从这个事情得出一条非常重要的结论,尽量使用缓存,不要因为网站现在很稳定就不使用缓存,因为网站随时会遇到这种访问量突发的情况。
屏蔽搜狗之前:
屏蔽搜狗之后:
下面是从nginx日志中分析出来的各种蜘蛛对网站的爬行数据排名靠前的几个,其实已经屏蔽了谷歌等很多用处不大的爬虫,但是还是有很多爬虫在爬:
看的出来搜狗一天怕了不少。
- 1楼SugaryoTT昨天 19:19
- 给楼主一个赞~来自菜鸟~顺便顶了!

PHPidentifiesauser'ssessionusingsessioncookiesandsessionIDs.1)Whensession_start()iscalled,PHPgeneratesauniquesessionIDstoredinacookienamedPHPSESSIDontheuser'sbrowser.2)ThisIDallowsPHPtoretrievesessiondatafromtheserver.

The security of PHP sessions can be achieved through the following measures: 1. Use session_regenerate_id() to regenerate the session ID when the user logs in or is an important operation. 2. Encrypt the transmission session ID through the HTTPS protocol. 3. Use session_save_path() to specify the secure directory to store session data and set permissions correctly.

PHPsessionfilesarestoredinthedirectoryspecifiedbysession.save_path,typically/tmponUnix-likesystemsorC:\Windows\TemponWindows.Tocustomizethis:1)Usesession_save_path()tosetacustomdirectory,ensuringit'swritable;2)Verifythecustomdirectoryexistsandiswrita

ToretrievedatafromaPHPsession,startthesessionwithsession_start()andaccessvariablesinthe$_SESSIONarray.Forexample:1)Startthesession:session_start().2)Retrievedata:$username=$_SESSION['username'];echo"Welcome,".$username;.Sessionsareserver-si

The steps to build an efficient shopping cart system using sessions include: 1) Understand the definition and function of the session. The session is a server-side storage mechanism used to maintain user status across requests; 2) Implement basic session management, such as adding products to the shopping cart; 3) Expand to advanced usage, supporting product quantity management and deletion; 4) Optimize performance and security, by persisting session data and using secure session identifiers.

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver CS6
Visual web development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
