0

I feel like I must be missing something that's right in front of me, but here's the situation. My nginx server continues to return a 502 Bad Gateway error every time I attempt to access a .php file from the browser (for example, subdomain1.example.com/test.php, subdomain2.example.com/test2.php, etc).

Note: I've tried removing the nested root directives from within the different location blocks as described in this answer, then reloading/restarting nginx, but it doesn't help.

Here's the nginx.conf file:

user nginx nginx;
worker_processes 5;
pid /var/run/nginx.pid;

events {
  worker_connections 2048;
}

http {
  ##
  # Basic Settings
  ##

  sendfile on;

  keepalive_timeout 5;

  types_hash_max_size 2048;

  include /etc/nginx/mime.types;
  include /etc/nginx/fastcgi.conf;
  default_type application/octet-stream;
  #default_type text/html;

  ## Detect when HTTPS is used
  map $scheme $fastcgi_https {
    default off;
    https on;
  }

  ##
  # Logging Settings
  ##
  access_log /var/log/nginx/access.log;

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

  rewrite_log on;

  log_format main '$remote_addr - $remote_user [$time_local] $request "$status" $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for"';


  ##
  # Gzip Settings
  ##
  gzip on;
  gzip_disable "msie6";
  gzip_vary on;
  gzip_proxied any;
  gzip_comp_level 5;
  gzip_buffers 16 8k;
  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;

  upstream fpm_backend {
   #server 127.0.0.1:9000; # backend server:port address
   server unix:/var/run/php-fpm.sock;
  }

  ##
  # Virtual Host Configs
  ##

  #
  # Server 1
  #
  server {
    listen     80 default_server;
    server_name  subdomain1.example.com;
    root   /usr/share/nginx/html;

    location / {
      index  index.html index.htm;
    }

    error_page  404        /404.html;
    location = /404.html {
      root   /usr/share/nginx/html;
    }

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

    location ~ \.php$ {
      try_files $uri =404; # if reference to php executable is invalid return 404
      expires off; # no need to cache php executable files
      fastcgi_read_timeout 600;
      fastcgi_pass fpm_backend; # configured in nginx.conf
      fastcgi_keep_conn on; # use persistent connects
      include fastcgi_params;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
  }


  #
  # Server 2
  #
  server {
    listen     80;
    listen     443 ssl;
    server_name subdomain2.example.com;
    root   /sites/subdom2;
    ssl_certificate   ssl/ex-wildcard.crt;
    ssl_certificate_key ssl/ex-wildcard.key;
    ssl_crl       ssl/ex_bundle-g2-g1.crt;

    location / {
      index index.php;
    }

    error_page  404        /404.html;
    location = /404.html {
      root   /usr/share/nginx/html;
    }

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

    location /content/ {
      root   /sites/subdom2;
      ## mp4 streaming
      mp4;

      secure_link $arg_a,$arg_b;

      secure_link_md5 ABCDEFGHIJK$uri$arg_b$remote_addr;

      if ($secure_link = "") {
        return 403;
      }

      if ($secure_link = "0") {
        return 403;
      }
    }

    location /videos/ {
      ## mp4 streaming
      mp4;

      secure_link $arg_a,$arg_b;

      secure_link_md5 ABCDEFGHIJK$uri$arg_b$remote_addr;

      if ($secure_link = "") {
        return 403;
      }

      if ($secure_link = "0") {
        return 403;
      }
    }

    location /otherdir/content/ {
      root   /sites/subdom2/;
      ## mp4 streaming
      mp4;

      secure_link $arg_a,$arg_b;

      secure_link_md5 ABCDEFGHIJK$uri$arg_b$remote_addr;

      if ($secure_link = "") {
        return 403;
      }

      if ($secure_link = "0") {
        return 403;
      }
    }

    location ~ \.php$ {
      try_files $uri =404; # if reference to php executable is invalid return 404
      expires off; # no need to cache php executable files
      fastcgi_read_timeout 600;
      fastcgi_pass fpm_backend; # configured in nginx.conf
      fastcgi_keep_conn on; # use persistent connects
      include fastcgi_params;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
  }
}

Feel free to request any other info that might be helpful in figuring this out.

jerdiggity
  • 101
  • 2

1 Answers1

0

This is probably quite obvious, but your "fastcgi_pass fpm_backend" is likely the problem.

I find Unix sockets can be more fiddly to get working, things like permissions are important.

Try an http socket rather than unix. Here's how I do it on my server.

upstream php56-fpm {
  server 127.0.0.1:9000;
}

Inside the PHP location

fastcgi_pass php56-fpm;

You can download my full configuration files here.

Tim
  • 31,888
  • 7
  • 52
  • 78
  • Thanks. I've tried that too actually, but when I do it and then visit the same page(s), they just time out. That's after reloading/restarting both php-fpm and nginx. Any other insight you could provide? Also, LMK if there's any other info I can provide which might help. – jerdiggity Oct 08 '17 at 20:36
  • You need to go through things carefully, look at error message and work out what they mean. This is typically fairly easy, but with lots of little traps. Get it working yourself and you'll learn a lot. Your main tool is logs, Nginx access and error, PHP access and error. – Tim Oct 08 '17 at 21:13