search

Home  >  Q&A  >  body text

Is it possible to use Nginx to configure a random PHP version?

I have an nginx with two different php-fpm versions (7.4 and 8.1).

I want nginx to load these two php versions randomly.

For example, the info.php loaded at this time is version 7.4, and version 8.1 should be displayed next time.

Is this possible?

P粉521748211P粉521748211432 days ago683

reply all(1)I'll reply

  • P粉245489391

    P粉2454893912023-09-08 12:14:44

    Today, someone asked me a similar question, I searched here and found the question here, but no one answered it. I hope this answer helps others in the future too!

    The answer to this question is, yes, it is possible. First, you need to define two separate upstream blocks for each PHP version, then use the random directive in the upstream block to enable random load balancing between the available PHP versions, and finally, in your server block, you can use the proxy_pass directive and the corresponding upstream block to proxy the request to the PHP version!

    like this:

    http {
        upstream php7 {
            random;
            server unix:/var/run/php7.4-fpm.sock;
        }
    
        upstream php8 {
            random;
            server unix:/var/run/php8.1-fpm.sock;
        }
    
        server {
            listen 80;
            server_name example.com;
    
            location / {
                proxy_pass http://php7;
            }
    
            location /info.php {
                proxy_pass http://php8;
            }
        }
    }

    As you can see, requests to the root URL (/) will be randomly routed to PHP version 7.4 or 8.1 using the proxy_pass directive and php7 upstream.

    reply
    0
  • Cancelreply