title: Nginx 502 error with any Wordpress page, but other php webapps are working
body: <p>I've been banging my head against the wall with this one for some time now with no success.</p>
  
  <p>I'm loosely following Ars' Web Served series of articles to install Nginx, PHP and MariaDB on OS X. Everything has been working fine until Wordpress. I've installed Wordpress on Apache plenty of times without incident so I'm sure it's some Nginx concept I haven't grasped.</p>
  
  <p>My site is called "garden". I can run <code>garden:8080/phpinfo.php</code> no worries and it shows PHP is working, and that it is talking to the homebrew version of PHP in <code>/usr/local/</code> that I want it to be talking to rather than the one that comes with OS X.</p>
  
  <p>I can also run <code>garden:8080/sqlbuddy/</code> and SQL buddy runs and talks to the database no worries.</p>
  
  <p>I currently have the WordPress files in the webroot ('garden'), but have them in a sub-directory as well with no success.</p>
  
  <p>If I try <code>garden:8080/wp-admin/install.php</code> or any other WP file I get a 502 with the following error in the Nginx log:</p>
  
  <p><code>2016/01/10 13:33:47 [error] 2170#0: *484 upstream prematurely closed connection while reading response header from upstream, client: 127.0.0.1, server: garden, request: "GET /wp-admin/install.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "garden:8080"</code></p>
  
  <p>Interestingly I can go to <code>garden:8080/index.php</code> and I get a WP page telling me that <code>wp-config.php</code> cannot be found. I tried adding a <code>wp-config.php</code> to appease it but now I get a Safari redirect loop error.</p>
  
  <p>So given all of that and following every Nginx 502 troubleshooting guide I can find I think it's some form of error with my location and/or try files configs in Nginx rather than a PHP-fpm issue. But happy to be proven wrong.</p>
  
  <p>Apologies for state of conf files, but they've seen a bit of work.</p>
  
  <p><strong>nginx.conf</strong></p>
  
  <pre><code>#user  nobody;
  worker_processes  2;
  
  #error_log  logs/error.log;
  #error_log  logs/error.log  notice;
  #error_log  logs/error.log  info;
  
  #pid        logs/nginx.pid;
  
  
  events {
  worker_connections  768;
  }
  
  
  http {
  include       mime.types;
  include  sites-enabled/*; #load virtuals config
  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  logs/access.log  main;
  
  sendfile        on;
  #tcp_nopush     on;
  
  client_max_body_size 4096k;
  client_header_timeout 10;
  client_body_timeout 10;
  #keepalive_timeout  0;
  keepalive_timeout  10 10;
  send_timeout 10;
  
  #php-fpm params
  fastcgi_buffers 4 16k;
  fastcgi_buffer_size 16k;
  fastcgi_connect_timeout 300;
  fastcgi_send_timeout 300;
  fastcgi_read_timeout 300;
  
  gzip  on;
  gzip_disable "msie6";
  gzip_min_length 1100;
  gzip_vary on;
  gzip_proxied any;
  gzip_buffers 16 8k;
  gzip_types text/plain text/css application/json application/x-javascript
  text/xml application/xml application/rss+xml text/javascript
  image/svg+xml application/x-font-ttf font/opentype
  application/vnd.ms-fontobject;
  
  server_tokens off;
  
  server {
      listen       8080;
      server_name  localhost;
  
      #charset koi8-r;
  
      #access_log  logs/host.access.log  main;
  
      location / {
          root   html/garden;
          index  index.html index.htm index.php;
  try_files  $uri  $uri/  /index.php?$args;
  }
  
  # configure *.PHP requests
  
  #    location ~ \.php$ {
   #       root  html/garden;
    #      try_files  $uri  $uri/  /index.php?$args ;
     #     index  index.html index.htm index.php;
      #    fastcgi_param PATH_INFO $fastcgi_path_info;
       #   fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
        #  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  #
  #        fastcgi_pass 127.0.0.1:9000;
  #        fastcgi_index index.php;
  #        fastcgi_split_path_info ^(.+\.php)(/.+)$;
  #        fastcgi_intercept_errors on;
  #        include fastcgi_params;
  
  # }
  
  location ~ /\. { access_log off; log_not_found off; deny all; }
  location ~ ~$ { access_log off; log_not_found off; deny all; }
  
      #error_page  404              /404.html;
  
      # redirect server error pages to the static page /50x.html
      #
      error_page   500 502 503 504  /50x.html;
      location = /50x.html {
          root   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;
      #    fastcgi_pass   127.0.0.1:9000;
      #    fastcgi_index  index.php;
      #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
      #    include        fastcgi_params;
      #}
  
      # deny access to .htaccess files, if Apache's document root
      # concurs with nginx's one
      #
      #location ~ /\.ht {
      #    deny  all;
      #}
  }
  
  
  # another virtual host using mix of IP-, name-, and port-based configuration
  #
  #server {
  #    listen       8000;
  #    listen       somename:8080;
  #    server_name  somename  alias  another.alias;
  
  #    location / {
  #        root   html;
  #        index  index.html index.htm;
  #    }
  #}
  
  
  # HTTPS server
  #
  #server {
  #    listen       443 ssl;
  #    server_name  localhost;
  
  #    ssl_certificate      cert.pem;
  #    ssl_certificate_key  cert.key;
  
  #    ssl_session_cache    shared:SSL:1m;
  #    ssl_session_timeout  5m;
  
  #    ssl_ciphers  HIGH:!aNULL:!MD5;
  #    ssl_prefer_server_ciphers  on;
  
  #    location / {
  #        root   html;
  #        index  index.html index.htm;
  #    }
  #}
  include servers/*;
  }
  </code></pre>
  
  <p><strong>Garden (website conf. file.)</strong></p>
  
  <pre><code>server {
  listen                *:8080;
  server_name           garden;
  #access_log                  /Users/{username}/{webfolder}/{hostfile}/log/access.log;
  # error_log            /html/garden/log/error.log;
  root  html/garden;
  index  index.html index.htm index.php ;
  
  location / {
  try_files  $uri  $uri/  /index.php?$args;
  }
  
  location ~ \.php$ {
  try_files  $uri  $uri/ /index.php?$args ;
  #commented out this block troubleshooting earlier
  fastcgi_param PATH_INFO $fastcgi_path_info;
  fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  fastcgi_pass 127.0.0.1:9000;
  fastcgi_index index.php;
  fastcgi_split_path_info ^(.+\.php)(/.+)$;
  fastcgi_intercept_errors on;
  include fastcgi_params;
  
  #This block is direct from Ars Web Served Part 3
  #       try_files $uri =404;
  #       include fastcgi_params;
  #       fastcgi_pass 127.0.0.1:9000;
  #       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  #       fastcgi_intercept_errors on;
  }
  
  
  #location /blog/ {
  #  try_files $uri $uri/ /blog/index.php?$args;
  #allow 192.168.1.0/24;
  # allow 127.0.0.1;
  # deny all;
  #}
  
  #location ~ /blog/.*\.php$ {
  # allow 192.168.1.0/24;
  # allow 127.0.0.1;
  # deny all;
  #   try_files $uri =404;
  # include fastcgi_params;
  # fastcgi_index index.php;
  #  fastcgi_pass 127.0.0.1:9000;
  # fastcgi_param PATH_INFO $fastcgi_path_info;
  # fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
  # fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  # fastcgi_split_path_info ^(.+\.php)(/.+)$;
  # fastcgi_intercept_errors on;
  #}
  }
  </code></pre>
  
  <p><strong>fastcgi-params</strong></p>
  
  <pre><code>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_param  SCRIPT_NAME        $fastcgi_script_name;
  fastcgi_param  REQUEST_URI        $request_uri;
  fastcgi_param  DOCUMENT_URI       $document_uri;
  fastcgi_param  DOCUMENT_ROOT      $document_root;
  fastcgi_param  SERVER_PROTOCOL    $server_protocol;
  fastcgi_param  HTTPS              $https if_not_empty;
  
  fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
  fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;
  
  fastcgi_param  REMOTE_ADDR        $remote_addr;
  fastcgi_param  REMOTE_PORT        $remote_port;
  fastcgi_param  SERVER_ADDR        $server_addr;
  fastcgi_param  SERVER_PORT        $server_port;
  fastcgi_param  SERVER_NAME        $server_name;
  
  # PHP only, required if PHP was built with --enable-force-cgi-redirect
  fastcgi_param  REDIRECT_STATUS    200;
  
  fastcgi_buffer_size 128k;
  fastcgi_buffers 4 256k;
  fastcgi_busy_buffers_size 256k;
  </code></pre>
  
  <p><strong>php-fpm.conf</strong> relevant bit. I tried changing the user and group to match the same user and group that Nginx is running as but no success.</p>
  
  <pre><code>;;;;;;;;;;;;;;;;;;;;
  ; Pool Definitions ;
  ;;;;;;;;;;;;;;;;;;;;
  
  ; Multiple pools of child processes may be started with different listening
  ; ports and different management options.  The name of the pool will be
  ; used in logs and stats. There is no limitation on the number of pools which
  ; FPM can handle. Your system will tell you anyway :)
  
  ; Start a new pool named 'www'.
  ; the variable $pool can we used in any directive and will be replaced by the
  ; pool name ('www' here)
  [www]
  
  ; Per pool prefix
  ; It only applies on the following directives:
  ; - 'access.log'
  ; - 'slowlog'
  ; - 'listen' (unixsocket)
  ; - 'chroot'
  ; - 'chdir'
  ; - 'php_values'
  ; - 'php_admin_values'
  ; When not set, the global prefix (or /usr/local/Cellar/php56/5.6.16) applies instead.
  ; Note: This directive can also be relative to the global prefix.
  ; Default Value: none
  ;prefix = /path/to/pools/$pool
  
  ; Unix user/group of processes
  ; Note: The user is mandatory. If the group is not set, the default user's group
  ;       will be used.
  user = _www
  group = _www
  
  ; The address on which to accept FastCGI requests.
  ; Valid syntaxes are:
  ;   'ip.add.re.ss:port'    - to listen on a TCP socket to a specific IPv4     address on
  ;                            a specific port;
  ;   '[ip:6:addr:ess]:port' - to listen on a TCP socket to a specific IPv6 address on
  ;                            a specific port;
  ;   'port'                 - to listen on a TCP socket to all IPv4 addresses on a
  ;                            specific port;
  ;   '[::]:port'            - to listen on a TCP socket to all addresses
  ;                            (IPv6 and IPv4-mapped) on a specific port;
  ;   '/path/to/unix/socket' - to listen on a unix socket.
  ; Note: This value is mandatory.
  listen = 127.0.0.1:9000
  
  ; Set listen(2) backlog.
  ; Default Value: 65535 (-1 on FreeBSD and OpenBSD)
  ;listen.backlog = 65535
  
  ; Set permissions for unix socket, if one is used. In Linux, read/write
  ; permissions must be set in order to allow connections from a web server.   Many
  ; BSD-derived systems allow connections regardless of permissions.
  ; Default Values: user and group are set as the running user
  ;                 mode is set to 0660
  ;listen.owner = _www
  ;listen.group = _www
  listen.mode = 666
  ; When POSIX Access Control Lists are supported you can set them using
  ; these options, value is a comma separated list of user/group names.
  ; When set, listen.owner and listen.group are ignored
  ;listen.acl_users =
  ;listen.acl_groups =
  
  ; List of addresses (IPv4/IPv6) of FastCGI clients which are allowed to connect.
  ; Equivalent to the FCGI_WEB_SERVER_ADDRS environment variable in the original
  ; PHP FCGI (5.2.2+). Makes sense only with a TCP listening socket. Each address
  ; must be separated by a comma. If this value is left blank, connections will be
  ; accepted from any IP address.
  ; Default Value: any
  ;listen.allowed_clients = 127.0.0.1
  
  ; Specify the nice(2) priority to apply to the pool processes (only if set)
  ; The value can vary from -19 (highest priority) to 20 (lower priority)
  ; Note: - It will only work if the FPM master process is launched as root
  ;       - The pool processes will inherit the master process priority
  ;         unless it specified otherwise
  ; Default Value: no set
  ; process.priority = -19
  
  ; Choose how the process manager will control the number of child processes.
  ; Possible Values:
  ;   static  - a fixed number (pm.max_children) of child processes;
  ;   dynamic - the number of child processes is set dynamically based on the
  ;             following directives. With this process management, there will be
  ;             always at least 1 children.
  ;             pm.max_children      - the maximum number of children that can
  ;                                    be alive at the same time.
  ;             pm.start_servers     - the number of children created on startup.
  ;             pm.min_spare_servers - the minimum number of children in 'idle'
  ;                                    state (waiting to process). If the number
  ;                                    of 'idle' processes is less than this
  ;                                    number then some children will be created.
  ;             pm.max_spare_servers - the maximum number of children in 'idle'
  ;                                    state (waiting to process). If the number
  ;                                    of 'idle' processes is greater than this
  ;                                    number then some children will be killed.
  ;  ondemand - no children are created at startup. Children will be forked when
  ;             new requests will connect. The following parameter is used:
  ;             pm.max_children           - the maximum number of children that
  ;                                         can be alive at the same time.
  ;             pm.process_idle_timeout   - The number of seconds after which
  ;                                         an idle process will be killed.
  ; Note: This value is mandatory.
  pm = dynamic
  
  ; The number of child processes to be created when pm is set to 'static' and the
  ; maximum number of child processes when pm is set to 'dynamic' or 'ondemand'.
  ; This value sets the limit on the number of simultaneous requests that will be
  ; served. Equivalent to the ApacheMaxClients directive with mpm_prefork.
  ; Equivalent to the PHP_FCGI_CHILDREN environment variable in the original PHP
  ; CGI. The below defaults are based on a server without many resources. Don't
  ; forget to tweak pm.* to fit your needs.
  ; Note: Used when pm is set to 'static', 'dynamic' or 'ondemand'
  ; Note: This value is mandatory.
  pm.max_children = 10
  </code></pre>
  
  <p>Many for any and all help.</p>