A
A
Anton B2015-11-10 18:43:32
PHP
Anton B, 2015-11-10 18:43:32

How not to duplicate php-fpm settings in each location?

Good afternoon.
All PHP files should go to PHP-FPM, but for some files and directories, some individual parameters may change, for example, fastcgi_read_timeout. Now for each location you have to duplicate the same code:

include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php5-fpm.sock;

Is it possible to somehow remove this code into a function and connect it in each separately customizable location?
Thank you.
server {

    listen 80 default_server;

    root /var/www/site.ru/www;

    index index.php;

    server_name site.ru;

    access_log /var/www/site.ru/log/nginx.access.log;
    error_log /var/www/site.ru/log/nginx.error.log;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
    }

    location = /task.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_read_timeout 300;
    }

    location ~ /cron/.+\.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_read_timeout 600;
    }
}

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Anton B, 2015-12-11
@bigton

Understood my issue. Maybe someone will come in handy.
1. Use named location

location / {
    try_files $uri $uri/ =404;
}

# именованный location
location @php {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
}

location ~ \.php$ {
    try_files /no @php;
}

location = /task.php$ {
    try_files /no @php;
    fastcgi_read_timeout 300; # не сработает
}

location ~ /cron/.+\.php$ {
    try_files /no @php;
    fastcgi_read_timeout 600; # не сработает
}

Minus - you can not apply individual settings for location.
2. Connecting via include
# /etc/nginx/php.conf
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php5-fpm.sock;

location / {
    try_files $uri $uri/ =404;
}

location ~ \.php$ {
    include /etc/nginx/php.conf;
}

location = /task.php$ {
    include /etc/nginx/php.conf;
    fastcgi_read_timeout 300; # сработает
}

location ~ /cron/.+\.php$ {
    include /etc/nginx/php.conf;
    fastcgi_read_timeout 600; # сработает
}

T
Timofey, 2015-11-11
@mr_T

include, as it were, hints that you can insert pieces of configs from separate files. You create a file with what you need and just write include my-awsome-config-file.

K
kompi, 2015-11-10
@kompi

Replace duplicate parameters with a named section.

N
Nikolai Kovalev, 2015-11-19
@nkmail

Install centminmod on the test server, see how it works ;) Inclusions in fact, as mentioned above.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question