##PHP Web side safe operation Nginx configuration and hot restart
##Foreword Previously, I helped customers build an automated AB station based on GeoIP2 (Nginx Geoip2 handles access from different countries (or cities)
), the customer recently wants to manually control AB station switching through the management terminal
It is not recommended to use system, exec and other functions that execute shell commands
Requires complex privilege escalation operations
- These functions are prohibited in general projects
- As a qualified Phper, unless there are special circumstances, it is strictly prohibited to enable some security-related functions in the project. Function
-
Solution idea
Nginx vhost configuration file
include- Fragment configuration
Backend switching When station AB, modify the PHP logic to introduce fragment configuration in the first step
- Nginx Reload
- The first solution: small projects use crontab to execute nginx -s reload regularly (used with worker_shutdown_timeout)
- Second solution (recommended): After modification, the mark requires reload status (File or DB or Cache), and the timer queries whether reload is needed through the python script to execute nginx -s reload
-
1. Create a fragment configuration fileCreate an independent fragment Nginx configuration file, for example
How to operate Nginx configuration on PHP web side.conf
, then include
How to operate Nginx configuration on PHP web side.conf
root /www/wwwroot/ahost;
Copy after login
in
nginx vhost
2.Configuration fileHow to operate Nginx configuration on PHP web side.conf
site.conf
## in the site configuration file #
server {
listen 80;
server_name 0.0.0.0;
index index.html;
include /www/wwwroot/abhost/How to operate Nginx configuration on PHP web side.conf;
}
Copy after login
3. Operation in background logic
How to operate Nginx configuration on PHP web side.confif($data['site_set'] == AbHostSiteEnum::Ahost) {
//开启A站
$ahostPath = AbHostSiteEnum::AhostPath;
file_put_contents('./How to operate Nginx configuration on PHP web side.conf',"root {$ahostPath};");}else {
//开启B站
$bhostPath = AbHostSiteEnum::BhostPath;
file_put_contents('./How to operate Nginx configuration on PHP web side.conf',"root {$bhostPath};");}
Copy after login
4.Set worker_shutdown_timeout
in Nginx global configuration Nginx cannot exit smoothly within 30s, so it will forcefully close the process
nginx.conf
...worker_shutdown_timeout 30;
Copy after login
5. Execute Nginx hot restart regularly
crontab -e
*/5 * * * * nginx -s reload
Copy after login
Recommended tutorial: "
PHP"
The above is the detailed content of How to operate Nginx configuration on PHP web side. For more information, please follow other related articles on the PHP Chinese website!