Intermittent 502 and 404 errors

  • Posted on March 19, 2014
  • cgreeneAsked by cgreene


I have an Ubuntu 12.04, Nginx 1.4.4, PHP 5.5.9, running Wordpress, running on an 8GB, 60GB droplet.

The site works great, some of the time, but we are experiencing intermittent 502 and 404 errors, and don’t know where to look. Here is my nginx configuration:

` server { listen 80; root /usr/share/nginx/www; index index.php; server_name; autoindex off; include conf.sites/wordpress-both.conf; access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log error;

set $cache_uri $request_uri;

POST requests and urls with a query string should always go to PHP

if ($request_method = POST) { set $cache_uri ‘null cache’; } if ($query_string != “”) { set $cache_uri ‘null cache’; }

Don’t cache uris containing the following segments

if ($request_uri ~* "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-loca$ set $cache_uri ‘null cache’; }

Don’t use the cache for logged in users or recent commenters

if ($http_cookie ~* “comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_logged_in”) { set $cache_uri ‘null cache’; }

Use cached or actual file if they exists, otherwise pass request to WordPress

location / { try_files /wp-content/cache/supercache/$http_host/$cache_uri/index.html $uri $uri/ /index.php ; # auth_basic “closed site”; # auth_basic_user_file .htpasswd; }

Erase this block if the phpmyadmin security configuration below works

    location /phpmyadmin {
            root /usr/share/nginx/www;
            index index.php index.html index.htm;
            location ~ ^/phpmyadmin/(.+\.php)$ {
                    try_files $uri =404;
                    include fastcgi_params;
           auth_basic           "closed site";
            auth_basic_user_file .htpasswd;

#location /phpmyadmin {

root /usr/share/;

index index.php index.html index.htm;

location ~ ^/phpmyadmin/(.+.php)$ {

try_files $uri =404;

fastcgi_pass unix:/var/run/php5-fpm.sock;

include fastcgi_params;



location ~ .php$ { # try_files $uri = 404; fastcgi_split_path_info ^(.+.php)(.*)$; fastcgi_index index.php; #fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_pass; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; fastcgi_intercept_errors on; fastcgi_ignore_client_abort off; fastcgi_connect_timeout 60; fastcgi_send_timeout 180; fastcgi_read_timeout 300; fastcgi_buffer_size 128k; fastcgi_buffers 4 256k; fastcgi_busy_buffers_size 256k; fastcgi_temp_file_write_size 256k;

auth_basic “Password Protected Folder”;

auth_basic_user_file /etc/nginx/.htpasswd;


location = /favicon.ico { log_not_found off; access_log off; } location = /robots.txt { log_not_found off; access_log off; }

    error_page 502 503 504 /50x.html;
    location = /50x.html {
            root /usr/share/nginx/www;

error_page 500 /500.html;

location = /500.html {

root /usr/share/nginx/www;


#location /nginx_status {
    # Turn on stats
      #stub_status on;
      #access_log off;
    # only allow access from #
      #deny all;


Cache static files for as long as possible

location ~* .(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ { expires max; log_not_found off; access_log off; } ` One of the PHP errors we are getting is:

2014/03/19 18:53:02 [error] 768#0: *666936 recv() failed (104: Connection reset by peer) while reading response header from upstream, client:, server:, request: "GET /article-url/?cf_action=sync_comments&post_id=4713 HTTP/1.1", upstream: "fastcgi://", host: "", referrer: ""

Thanks, Paul

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

502 errors means php5-fpm is crashing. Try checking its error logs (/var/log/php-fpm.log) for more info on why it’s crashing.