e-Zest members share technology ideas to foster digital transformation.

Troubleshooting with Nginx

Written by Anagha Thorat | Nov 22, 2016 12:50:00 AM
  • Introduction

What is Nginx?

Nginx is a high performance HTTP Server, in other words it is a software that provides a webserver. It is free and open source which works as a reverse proxy server. It is known for its simple configurations and can also be used for load balancing.

This document explains Nginx and configuration changes needed to be done as per errors.

It also explains error faced while configuring Nginx for load balancing and handling requests in a bulk and also provides solution to respective errors.

Configurations of Nginx

The configuration file is /etc/nginx/nginx.conf

  • Error 502 Bad Gateway:

http {

...

fastcgi_buffers 8 16k;

fastcgi_buffer_size 32k

fastcgi_connect_timeout 300;

fastcgi_send_timeout 300;

fastcgi_read_timeout 300;

...

}

  • Increase buffer and timeouts inside http block in the nginx.conf file:
  • Ensure your php-fpm service is listening according to what you have configured in nginx.

Edit www.conf file (in CentOS it is located at /etc/php-fpm.d/www.conf and try with one of the below two options:

listen = /var/run/php5-fpm.sock 

                    or

listen = 127.0.0.1:9000

service nginx restart

service php-fpm restart

After that, just restart the nginx and php-fpm service.

  • Error nginx errors “recv() failed (104: Connection reset by peer) while reading response header from upstream”
    Make the following changes in nginx.conf

proxy_read_timeout    1500s;

proxy_send_timeout    1500s;

proxy_connect_timeout 1500s;

send_timeout         1500s;

proxy_buffer_size   128k;

proxy_buffers   4 256k;

proxy_busy_buffers_size   256k;

try_files $uri $uri/ /index.php?$args; # make index.php handle requests for

access_log on;

  • Set the following parameters in nginx.conf file

fastcgi_buffers 256 16k;

fastcgi_buffer_size 128k;

fastcgi_busy_buffers_size 256k;

  • Set the buffers and Buffer_Size
  • Increase the worker_connections:

worker_connections 20000;

  • To set the cache to max :

Set

expires max;

Error: Too many files open/Large Files Open

If you are getting this error, it means you application has reached the maximum limit of open files. You need to increase open files limit.

To do so follow the following steps:

  • Edit file /etc/sysctl.conf, enter:

vi /etc/sysctl.conf

  • Append / modify the following line:

fs.file-max = 70000

  • Save and close the file. Edit /etc/security/limits.conf, enter:

# vi /etc/security/limits.conf

nginx       soft    nofile   10000

nginx       hard    nofile  30000

  • Set soft and hard limit for all users or nginx user as follows:
  •  Save and close the file. Finally, reload the changes with sysctl command

# sysctl –p

  • nginx worker_rlimit_nofile Option (Increase Open FD Limit at Nginx Level)

vi /etc/nginx/nginx.conf

  • set open fd limit to 30000

worker_rlimit_nofile 30000;

  • Reload the nginx server

service nginx reload

  • Final nginx.conf File

user nginx;

worker_processes auto;

error_log /var/log/nginx/error.log;

pid /var/run/nginx.pid;

 

#set open fd limit to 30000

worker_rlimit_nofile 30000;

 

events {

worker_connections 20000;

}

 

http {

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

'$status $body_bytes_sent "$http_referer" '

'"$http_user_agent" "$http_x_forwarded_for"';

 

access_log  /var/log/nginx/access.log  main;

 

sendfile            on;

tcp_nopush          on;

tcp_nodelay         on;

keepalive_timeout   65;

types_hash_max_size 2048;

 

include             /etc/nginx/mime.types;

default_type        application/octet-stream;

 

# Load modular configuration files from the /etc/nginx/conf.d directory.

# See http://nginx.org/en/docs/ngx_core_module.html#include

# for more information.

include /etc/nginx/conf.d/*.conf;

 

index   index.html index.htm;

 

server {

listen      8080;

# listen       [::]:80 default_server;

server_name  localhost;

root         /usr/share/nginx/html;

#passenger_enabled on;

# Load configuration files for the default server block.

include /etc/nginx/default.d/*.conf;

 

location / {

proxy_pass  http://internal-test-ecom-internalelb-elb-936852263.us-west-2.elb.amazonaws.com;

proxy_set_header Host $http_host;

# proxy_redirect off;

proxy_buffer_size   128k;

proxy_buffers   4 256k;

proxy_busy_buffers_size   256k;

proxy_connect_timeout       1500s;

proxy_send_timeout          1500s;

proxy_read_timeout          1500s;

send_timeout                1500s;

expires max;

#proxy_read_timeout 150;

}

location ~ \.(jpeg\|jpg\|gif\|png\|css\|js\|ico\|swf)$ {

try_files $uri $uri/ @proxy; # look for static files in root directory and ask backend if not successful

expires max;

access_log off;

}

 

# redirect server error pages to the static page /40x.html

#

error_page 404 /404.html;

#r] 29880#0: *737 upstream prematurely closed connection while reading response header from upstream

location = /40x.html {

}

 

# redirect server error pages to the static page /50x.html

#

error_page 500 502 503 504 /50x.html;

location = /50x.html {

}

 

# proxy the PHP scripts to Apache listening on 127.0.0.1:80

#

#location ~ \.php$ {

#    proxy_pass   http://127.0.0.1;

#}

 

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

#

location ~ \.php$ {

#    root           html;

#expires max;

#    fastcgi_pass   127.0.0.1:9000;

#    fastcgi_index  index.php;

#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

 

fastcgi_buffers 16 16k;

fastcgi_buffer_size 32k;

fastcgi_read_timeout 1500s;

fastcgi_ignore_client_abort on;

#    include        fastcgi_params;

}

 

# deny access to .htaccess files, if Apache's document root

# concurs with nginx's one

#

#location ~ /\.ht {

#    deny  all;

#}

}

}