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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.