SOLVED Adding custom configuration for / in NGINX

gvard

Well-Known Member
PartnerNOC
Dec 22, 2003
211
10
168
Athens/GREECE
cPanel Access Level
DataCenter Provider
Hello,

We want to forward status.domain.tld (a cPanel account with that domain name is created) to domaintld.freshstatus.io in order to serve a status page from Freshstatus using a domain name with SSL. In their instructions at How can I host my Freshstatus status page on a custom domain? they have an NGINX directive.

The code we added is the following:

Code:
location / {
        proxy_set_header HOST $host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_pass https://domaintld.freshstatus.io;

    }
When the code is added on /etc/nginx/conf.d/users/USERNAME.conf replacing the current code for "location /" everything works like a charm, however that file gets ovewritten from cPanel periodically. At NGINX with Reverse Proxy | cPanel & WHM Documentation we see that we can use includes (in the main config file the line "include conf.d/users/USER/status.domain.tld/*.conf;" is already there), so we created /etc/nginx/conf.d/users/USERNAME/status.domain.tld/fresh.conf with the above contents.

-When we comment out the "location /" lines fron /etc/nginx/conf.d/users/USERNAME.conf and restart NGINX, the include file works, however when we do not comment them out, we get the following error while trying to start NGINX:

nginx: [emerg] duplicate location "/" in /etc/nginx/conf.d/users/USERNAME/status.domain.tld/fresh.conf:1


How can I add custom directive for / without editing the main file (to remove the existing entries) since it gets ovewritten by cPanel?
 

cPRex

Jurassic Moderator
Staff member
Oct 19, 2014
7,519
1,028
313
cPanel Access Level
Root Administrator
Hey there! I did some homework on this and I wasn't able to get this to work either. I spoke with one of the developers on the webserver team and it seems you aren't able to override /. It might be best to do this in a different way, such as a 302 redirect to /status or something similar, as anything I tried on my end resulted in a similar error with the Nginx / warning.
 

cPRex

Jurassic Moderator
Staff member
Oct 19, 2014
7,519
1,028
313
cPanel Access Level
Root Administrator
I spoke with our developers about this and they found that this worked well:

Code:
rewrite ^/$ http://www.domain.com/status redirect;

location /status {
        proxy_set_header HOST $host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_pass https://domaintld.freshstatus.io;

    }
although this is specifically for /status and not / itself. Could you try that and see if that works for your situation?
 
  • Like
Reactions: gvard