TIL: Fixing HTTP 502 Bad Gateway error (nginx & WordPress)

Armno P. 🇹🇭
2 min readFeb 22, 2017

My wife has a WordPress blog hosted on my server. Few days ago, MySQL server went down for some reasons (just like that) and of course her blog was broken. I was trying to recover but had no luck. So I reinstalled MySQL server on the server and setup the blog with a backup DB. The blog is working now.

Then I found out that I couldn’t change permalink format to “pretty URLs” because rewrite rules were not set up properly. With my very little knowledge on configuring virtual host nginx, in the end I got 502 — Bad Gateway error on the blog.

I wasn’t surprised at all though …

Basically I put wrong virtual host configurations to the config file. I was googling and found this article very helpful:

However it didn’t solve the problem until I came across another blog post:

And it turned out that all I had to do is changing the value of fastcgi_pass from a Unix socket value to an IP address + port like mentioned in the post, even though the .sock file exists on the server.

To summarize, here is how I fix the 502 error:

  • In the file /etc/php/7.0/fpm/pool.d/www.conf , change listen = /run/php/php7.0-fpm.sock to listen = 127.0.0.1:9000
  • Set Virtual Host config file like this
upstream php {
server unix:/tmp/php-cgi.socket;
server 127.0.0.1:9000;
}
server {
listen 80;
root /var/www/html/nemo.in.th;
index index.php index.html index.nginx-debian.html;
server_name nemo.in.th;

location / {
try_files $uri $uri/ /index.php?$args;
}

location ~ \.php$ {
#NOTE: You should have “cgi.fix_pathinfo = 0;” in php.ini
include fastcgi.conf;
include fastcgi_params;
fastcgi_pass php;
}
}

My wife’s blog is back to life again.

--

--