1

i'm using nginx with php-cgi. lately a problem has arose where if you don't view my site for a while, like 3-4 minutes, and then open it again, the first request you send will return connection reset by peer in the browser. if you refresh, operation is normal for all subsequent requests. this happens every time and it isn't just an isolated incident, it happens to everyone using my site. i've tried to restart nginx and php-cgi but to no avail. does anyone know what the problem could be? i can provide whatever information necessary.

it's worth noting that there's nothing in error log besides that message about client closing the connection early.

nginx.conf

user  nobody;
worker_processes  4;
error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;


events {
    worker_connections  2048;
}


http {
    include       /etc/nginx/mime.types;
    error_page 404 /404.html;
    error_page 403 /403.html;
        error_page 444 /444.html;
error_page 502 /502.html;

    default_type  application/octet-stream;

    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;
        large_client_header_buffers 8 8k;
    sendfile        on;
    tcp_nopush     on;
        tcp_nodelay on;
    keepalive_timeout  30;
        server_tokens off;

    gzip  on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 64 8k;
gzip_min_length 1024; gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
    include /etc/nginx/conf.d/*.conf;
}

default.conf

server {
    listen  80;
    server_name  domain.com;
    error_log /var/log/nginx/error.log debug;   
    access_log  /var/log/nginx/access.log;


    location / {
    if ($request_method !~ ^(GET|HEAD|POST)$ ) {
        return 444;
    }

    if ($http_user_agent ~* Havij|hvj|acunetix|wget|HTtrack) {
       return 403;
    }

    root   /home/admin06/public_html;
    autoindex off;        
    index  index.php;


# Images and static content is treated different
    location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$ {
      access_log        off;
      expires           30d;
      root /home/admin06/public_html;
    }

location /nginx_status {
    stub_status on;
access_log off;]
deny all;
}

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
#try_files $uri =404;
    fastcgi_pass backend;


        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /home/site/public_html$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_param  QUERY_STRING     $query_string;
        fastcgi_param  REQUEST_METHOD   $request_method;

       fastcgi_param  CONTENT_TYPE     $content_type;
        fastcgi_param  CONTENT_LENGTH   $content_length;
        fastcgi_intercept_errors        on;
        fastcgi_ignore_client_abort     off;
        fastcgi_connect_timeout 60;
        fastcgi_send_timeout 60;
        fastcgi_read_timeout 60;
        fastcgi_buffer_size 128k;
        fastcgi_buffers 4 256k;
        fastcgi_busy_buffers_size 256k;
        fastcgi_temp_file_write_size 256k;
    }

## Disable viewing .htaccess & .htpassword
    location ~ /\.ht {
        deny  all;
    }
location ~ error_log {
    deny all;
}

    location ~ access_log {
    deny all;
}
location ~ \.cgi {
deny all;
}

location ~ \.db {
deny all;
}
}
2
  • Please confirm it is happening to multiple browser brand(Firefox/Chrome/Opera/etc) or only IE.
    – John Siu
    Nov 12, 2012 at 1:42
  • It is happening in all browsers.
    – Tar
    Nov 12, 2012 at 21:09

2 Answers 2

1

You should first determine, which layer this fault is occurring in, i.e. at the server level (Linux machine configuration), at PHP (php.ini, php-cgi) level, or at your application level. HTTP errors can be caused by incorrect configuration at any of these layers, so you need to first isolate and identify where exactly the server fault (pun intended) is occurring.

My suggestion is you trace the HTTP request:

  1. From the moment it arrives at your server, through the Linux configuration.
  2. Then through Nginx configuration (nginx.conf, default.conf).
  3. Then through PHP and PHP-CGI.
  4. Lastly analyze it once it reaches your application and check what your application does with it for sending the http response.

In this manner you will be able to identify exactly where the problem is, before the good folks at server fault will be able to help you resolve it correctly!

Also post your nginx.conf, default.conf, php-fpm.conf Some detail about your application would also be nice.

2
  • I have updated my original post with default.conf and nginx.conf. The error I get when I I turn debugging on in nginx says that the client prematurely terminated the connection. this results in those 400 bad requests errors also. How might I go about watching it at server level?
    – Tar
    Nov 12, 2012 at 21:15
  • You can watch the connection at your system level, by monitoring the connection through netstat. Source- manpages.ubuntu.com/manpages/lucid/man8/netstat.8.html You would also want to check out your various sources such as your iptables, host, hosts.conf etc files, most of which are in /etc for network configuration on your system. Some network configuration is dependent on the applications that you are using with nginx as it may route connections internally or effect various rules on listen and response behaviour. If you are using php-fpm then you are already using internal routing... Nov 18, 2012 at 16:47
0

A new nginx update came out (1.2.5) and updating to the newer version fixed the problem..

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .