Home » Nginx Hosting » Tips and Tricks in Configuration and Optimizing of Nginx and PHP-FPM

Tips and Tricks in Configuration and Optimizing of Nginx and PHP-FPM

NGINX hostingThis article is a part of our series of “Tips and Tricks”. To avoid any confusion we broke the article down to two parts. The first, being about Nginx and the second, being about PHP-FPM.

Please keep in mind that these tips may not work in every situation, they are based entirely on a personal experience!

Nginx tip 1 – The files for configuration of Nginx are usually located at /etc/nginx path.

Here is a good way to configure and organize files use Debian/Ubuntu Apache style setup:

## Main configuration file ##
/etc/nginx/nginx.conf

## Virtualhost configuration files on ##
/etc/nginx/sites-available/
/etc/nginx/sites-enabled/

## Other config files on (if needed) ##
/etc/nginx/conf.d/

If your host is virtual than there is a second path, this is because the site can have any kind of content.

## Load virtual host conf files. ##
include /etc/nginx/sites-enabled/*;

## Load another configs from conf.d/ ##
include /etc/nginx/conf.d/*;

Nginx Tip 2. – Determine Nginx worker_processes and worker_connections

Default set up works great for worker_processes and worker_connections, but they can still be a little more optimized right?

Basic Nginx set up:

worker_processes  1;
worker_connections  1024;

Normally this 1000 concurrent connection is great, but sometimes It can still be slow and that might cause Nginx to be locked on 1/0 operations. To avoid this from happening use the following: one worker_precess / per processor core, like:

Worker Processes

worker_processes [number of processor cores];

Use this command to determinate how many running cores you have

cat /proc/cpuinfo |grep processor
processor : 0
processor : 1
processor : 2
processor : 3

You can see that here are 4 running cores

Final set up

worker_processes 4;

Worker connections
A lot of people like to stick with 1024 connection, but if for you its not enough you can double it to 2048 connection per seconds.

worker_connections 1024;

Some admins are doing different configuration to try and raise the traffic like setting the connections to 20000, but that will do more harm than good.

Nginx Tip 3 – Hide Nginx Server Tokens/ Hide Nginx version number

Mostly this process is being used for security reasons, especially if the Nginx you are using is outdated

server_tokens off;

Nginx Tip 4. – Nginx Request / Upload Max Body Size (client_max_body_size)

If you want users to be able to upload content on your site do this configuration:

client_max_body_size 20m;
client_body_buffer_size 128k;

“Request Entity Too Large” (413) – If this error shows up on your screen than the client_max_size is slow.

Nginx Tip 5. – Nginx Cache Control for Static Files (Browser Cache Control Directives)

Follow this set up:

location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ {
access_log        off;
log_not_found     off;
expires           360d;
}

If you want save, resources and bandwith.

Nginx Tip 6. – Nginx Pass PHP requests to PHP-FPM

# Pass PHP scripts to PHP-FPM
location ~* \.php$ {
fastcgi_index   index.php;
fastcgi_pass    127.0.0.1:9000;
#fastcgi_pass   unix:/var/run/php-fpm/php-fpm.sock;
include         fastcgi_params;
fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
}

Nginx Tip 7. – Prevent (deny) Access to Hidden Files with Nginx

Following deny access and turn off logging for all hidden files.

location ~ /\. {
access_log off;
log_not_found off;
deny all;
}

Well folks those were the tricks and tips we found for Nginx. Stay tuned for the second part of this article – PHP-FPM Configuration tips and tricks

First Tip – Configuration Files

PHP-FPM configuration files are at etc/php-fpm.conf file and /etc/php-fpm.d path. For a better configuration you need to add an include line.

Here is how it should look:

include=/etc/php-fpm.d/*.conf

Second Tip – Global Configuration Tweaks

Configure an emergency restart and its interval and also a process control.

emergency_restart_threshold 10
emergency_restart_interval 1m
process_control_timeout 10s

Third Tip – Pools Configuration

The following is an example of changing the files to a three different parts of a site:

/etc/php-fpm.d/site.conf
/etc/php-fpm.d/blog.conf
/etc/php-fpm.d/forums.conf

Configuration for every pool:

[site]
listen = 127.0.0.1:9000
user = site
group = site
request_slowlog_timeout = 5s
slowlog = /var/log/php-fpm/slowlog-site.log
listen.allowed_clients = 127.0.0.1
pm = dynamic
pm.max_children = 5
pm.start_servers = 3
pm.min_spare_servers = 2
pm.max_spare_servers = 4
pm.max_requests = 200
listen.backlog = -1
pm.status_path = /status
request_terminate_timeout = 120s
rlimit_files = 131072
rlimit_core = unlimited
catch_workers_output = yes
env[HOSTNAME] = $HOSTNAME
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp

[blog]
listen = 127.0.0.1:9001
user = blog
group = blog
request_slowlog_timeout = 5s
slowlog = /var/log/php-fpm/slowlog-blog.log
listen.allowed_clients = 127.0.0.1
pm = dynamic
pm.max_children = 4
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
pm.max_requests = 200
listen.backlog = -1
pm.status_path = /status
request_terminate_timeout = 120s
rlimit_files = 131072
rlimit_core = unlimited
catch_workers_output = yes
env[HOSTNAME] = $HOSTNAME
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp

[forums]
listen = 127.0.0.1:9002
user = forums
group = forums
request_slowlog_timeout = 5s
slowlog = /var/log/php-fpm/slowlog-forums.log
listen.allowed_clients = 127.0.0.1
pm = dynamic
pm.max_children = 10
pm.start_servers = 3
pm.min_spare_servers = 2
pm.max_spare_servers = 4
pm.max_requests = 400
listen.backlog = -1
pm.status_path = /status
request_terminate_timeout = 120s
rlimit_files = 131072
rlimit_core = unlimited
catch_workers_output = yes
env[HOSTNAME] = $HOSTNAME
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp

Fourth Tip – Pool Process Manager (pm) Configuration

Use a dynamic process management that way different processes will start only when they are needed. The configuration style is just the same as Nginx with a few very little differences.

Also you need to test your server to see how many processes it can handle at once, because the server may not be able to take all of processes you want to run.

Here is an example of the configuration:

pm.max_children = 9
pm.start_servers = 3
pm.min_spare_servers = 2
pm.max_spare_servers = 4
pm.max_requests = 200

Well this is for now folks. We hope this was of any help to you. If you know any other tips feel free to leave a comment about them.

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*