2

When I create a new webapp conf in Nginx I use the following template:

server {
    root /var/www/html/${domain};
    server_name ${domain} www.${domain};
    location ~*  \.(jpg|jpeg|png|gif|ico|css|js|ttf|woff|pdf)$ {
        expires 365d; 
    }
    location / {
        index index.php index.html index.htm fastcgi_index;
        try_files $uri $uri =404 $uri/ /index.php?$args;
    }
    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

Note the following location block in that template:

location ~ \.php$ {
    fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

Is there a "safe" way to make it version agnostic (that is, not to target version 7.0 specifically, or any other version, for that matter).

Do you know a way to do that? I'm not sure regex is the best way to go, maybe there's just another way to config this.

Update

BTW, these 7.0 directives can also be a bit painful - you sometimes just want to run a script, without starting to measure versions:

sed -i 's/post_max_size \= .M/post_max_size \= 200M/g' /etc/php/7.0/fpm/php.ini
sed -i 's/upload_max_filesize \= .M/upload_max_filesize \= 200M/g' /etc/php/7.0/fpm/php.ini
sed -i "s/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/g" /etc/php/7.0/fpm/php.ini
/etc/init.d/php7.0-fpm restart && systemctl restart nginx.service
Arcticooling
  • 1
  • 12
  • 44
  • 103
  • The idea of using an asterisk (`*`) seems promising: https://askubuntu.com/questions/992464/version-agnostic-php-fpm-restart – Arcticooling Jan 05 '18 at 04:01

1 Answers1

1

Use an upstream block. This can be placed into your main nginx.conf file which is inherited by all of your server specific configuration files.

For example:

upstream php {
    server unix:/var/run/php/php7.0-fpm.sock;
}

server {
    ...
    location ~ \.php$ {
        fastcgi_pass php;
        ...
    }
}

See this document for more.

Richard Smith
  • 1,243
  • 1
  • 8
  • 13
  • If I understand correct, this is still isn't version agnostic, just easier to manage as it effects all server blocks. – Arcticooling Jan 04 '18 at 23:40
  • The idea of using an asterisk (`*`) seems promising: https://askubuntu.com/questions/992464/version-agnostic-php-fpm-restart – Arcticooling Jan 06 '18 at 10:09