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

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 from a Unix socket value to an IP address + port like mentioned in the post, even though the file exists on the server.

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

  • In the file , change to
  • 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.

blogs at https://armno.in.th. #leftmedium.

blogs at https://armno.in.th. #leftmedium.