How to operate Nginx configuration on PHP web side

Guanhui
Release: 2023-03-01 15:48:02
forward
2596 people have browsed it

How to operate Nginx configuration on PHP web side

##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


How to operate Nginx configuration on PHP web side

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
  1. Fragment configurationBackend switching When station AB, modify the PHP logic to introduce fragment configuration in the first step
  2. Nginx Reload
  3. 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

Option 1

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 includeHow to operate Nginx configuration on PHP web side.conf

root /www/wwwroot/ahost;
Copy after login
in

nginx vhost

2.includeConfiguration 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.conf
if($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!

Related labels:
php
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template