Nginx.pid Permission Denied

I am using nginx through brew and it appears to be configured and working correctly - to a point. I have multiple host files (around 20 sites) and almost all of them work fine, however, any new sites added will not work.

All old host files seem to load just fine, for example with server name site.test on port 80, but any new host files added return with a "Server not found" in my web browsers.

After much troubleshooting and Google searching, I've finally found something that seems odd in my setup and it was this post that triggered it: nginx.conf and nginx.pid users and permissions. Although it doesn't seem to solve my exact problem.

When I run nginx -t I get:

nginx: [warn] the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /usr/local/etc/nginx/nginx.conf:1
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: [emerg] open() "/usr/local/var/run/nginx.pid" failed (13: Permission denied)
nginx: configuration file /usr/local/etc/nginx/nginx.conf test failed

However, when I run sudo nginx -t I get:

nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful

I have both killed nginx and stopped it gracefully with brew and restarted it with sudo, but I appear to get the same issue with nginx.pid.

When I start nginx, I use: sudo brew services start nginx

Running ps aux | grep nginx, returns:

Media32           7444   0.0  0.0  4339424   2016   ??  S    12:16pm   0:00.01 nginx: worker process  
Media32           7443   0.0  0.0  4339424   1836   ??  S    12:16pm   0:00.00 nginx: worker process  
root              7440   0.0  0.0  4297440   4876   ??  Ss   12:16pm   0:00.01 nginx: master process /usr/local/opt/nginx/bin/nginx -g daemon off;

Which should be correct as my user is setup in nginx.conf to be user Media32 staff.

I can provide any other code necessary, but I have been at a bit of a loss for days on this now and any help would be appreciated.

Update

I appear to have solved this by editing /etc/hosts and adding a record for the new host files, ie. 127.0.0.1 site.test and then after reloading nginx it worked, but I have never had to do this before, can anyone shed any light on why this is needed and wasn't needed prior?


I have solved this issue in macOS Catalina.

sudo brew services stop nginx

sudo chmod 777 /usr/local/var/run/nginx.pid

sudo brew services start nginx

nginx -t