Nginx

From Dikapedia
Jump to: navigation, search

https://www.tecmint.com/useful-nginx-command-examples/
https://serversforhackers.com/c/redirect-http-to-https-nginx
https://linuxize.com/post/redirect-http-to-https-in-nginx/


How to find and check Nginx configuration


$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful


How to find php.ini and www.conf


Find those full file paths using a find command:

find / \( -iname "php.ini" -o -name "www.conf" \)

The output should look similar to:

root@localhost:~# find / \( -iname "php.ini" -o -name "www.conf" \)
/etc/php/7.0/fpm/php.ini
/etc/php/7.0/fpm/pool.d/www.conf
/etc/php/7.0/cli/php.ini

https://www.linode.com/docs/guides/serve-php-php-fpm-and-nginx/

NGINX Performance Tuning



[6] https://myshell.co.uk/blog/2012/07/adjusting-child-processes-for-php-fpm-nginx/

https://www.kinamo.be/en/support/faq/determining-the-correct-number-of-child-processes-for-php-fpm-on-nginx

https://serverfault.com/questions/479443/php5-fpm-server-reached-pm-max-children/578673

If PHP has reached the maximum children setting, in which this is due to the setting in the pool directives file (www.conf). Pool Directives are a PHP-FPM convention where multiple "pools" of child processes can be started and have different configurations.

To address this issue, we followed document [6] to calculate the proper value for the max_children setting in the pool directives file (www.conf). The formula we used to calculate this value:

pm.max_children = Total RAM dedicated to the web server / Max child process size
       

We ran the following commands:

  • To get the average memory usage of a single php-fpm process:
$ ps --no-headers -o "rss,cmd" -C php-fpm | awk '{ sum+=$1 } END { printf ("%d%s\n", sum/NR/1024,"M") }'
25M
  • As your instance has over 30GB of memory, in this calculation I just used 10GB of memory to reserve memory for other processes)
pm.max_children = 10000MB / 25MB = 400 

This is the final configuration we made based on our calculation above:

$ sudo vi /etc/php/7.3/fpm/pool.d/www.conf 
           pm.max_children = 400
           pm.start_servers = 20
           pm.min_spare_servers = 20
           pm.max_spare_servers = 35
           pm.max_requests = 500


  • After making the above configuration changes, we restarted Nginx and php-fpm:
           $ sudo systemctl restart php7.3-fpm.service 
           $ sudo systemctl restart nginx


Optimizing PHP-fpm

https://geekflare.com/php-fpm-optimization/


504 gateway timeout


[5] https://easycloudsupport.zendesk.com/hc/en-us/articles/360002057472-How-to-Fix-504-Gateway-Timeout-using-Nginx

Nginx is timing out with a 504. A 504 Gateway Timeout error in Nginx can be generated often by a number of reasons on the backend connection that is serving content. I found that 504 is quite common, and are generated most probably by the PHP max execution time limit or by the FastCGI read timeout settings.

To address the Timeout issue, we ran the following commands. We changed the max_execution_time in php.ini to 300 and also changed request_terminate_timeout to 300 as per article [5]:

$ sudo vi /etc/php/7.3/fpm/php.ini 
max_execution_time = 300 

$ sudo vi /etc/php/7.3/fpm/pool.d/www.conf 
request_terminate_timeout = 300