I'm unable to convert a simple nginx config that uses cgi - NUT server

1. The problem I’m having:

I’m unable to convert a simple php config from NGNIX to Caddy for the NUT (Network UPS Tools) web site.
Although I can get the first page to work, the links that are .cgi don’t.
I’m pretty sure it’s just a config thing as the page can’t be found, but I don’t know how to fix it as I don’t know much about cgi or nginx.

I think the issue is that the main page is located in one location
/usr/share/nut/www

And the cgi scripts are located in another place?
/usr/lib/cgi-bin/nut

2. Error messages and/or full log output:

For the cgi link (http://host:port/cgi-bin/nut/upsset.cgi) which works in Ngnix 

< HTTP/1.1 404 Not Found
< Server: Caddy
< Date: Sat, 11 May 2024 19:57:00 GMT
< Content-Length: 0

The main page (http://host:port) which does work in Caddy


> User-Agent: curl/7.88.1
> Accept: */*
> 
< HTTP/1.1 200 OK
< Accept-Ranges: bytes
< Content-Length: 503
< Content-Type: text/html; charset=utf-8
< Etag: "rp1bludz"
< Last-Modified: Wed, 25 Jan 2023 09:27:30 GMT
< Server: Caddy
< Date: Sat, 11 May 2024 19:56:21 GMT
< 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Network UPS Tools -- http://www.networkupstools.org</title>
</head>

<frameset rows="110,*" frameborder="NO" border="0" framespacing="0">
  <frame src="header.html" name="topFrame" scrolling="NO" noresize>
  <frame src="bottom.html" name="mainFrame">
</frameset>
<noframes><body>
</body></noframes>
</html>

Log output
status":404,"err_id":"n69znbrq3","err_trace":"fileserver.(*FileServer).notFound (staticfiles.go:629)"}



3. Caddy version:

2.7.6

4. How I installed and ran Caddy:

a. System environment:

Raspberry Pi

b. Command:

Using Caddy as the system service install from the repository and using a Caddy file

c. Service/unit/compose file:

caddy.service - Caddy
     Loaded: loaded (/lib/systemd/system/caddy.service; enabled; preset: enabled)
     Active: active (running) since Sat 2024-05-11 20:52:41 BST; 13min ago
       Docs: https://caddyserver.com/docs/
   Main PID: 462533 (caddy)
      Tasks: 10 (limit: 9253)
        CPU: 139ms
     CGroup: /system.slice/caddy.service
             └─462533 /usr/bin/caddy run --environ --config /etc/caddy/Caddyfile


d. My complete Caddy config:

#NUT
:9877 {
root * /usr/share/nut/www/
php_fastcgi  unix//var/run/fcgiwrap.socket {
root /usr/lib/cgi-bin/nut/*
}
file_server
}

5. Links to relevant resources:

This is what the NGINX config is

server {
listen 9876;
location = / {
return 301 /nut;
}
location /nut {
alias /usr/share/nut/www/;
try_files $uri $uri/ /index.html;
}
location /cgi-bin/ {
gzip off;
root /usr/lib;
include fastcgi_params;
fastcgi_pass unix:/var/run/fcgiwrap.socket;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}

I think you probably want this plugin:

I will try and have a go at building a custom Caddy.

If I do:

Does the xcaddy command produce a caddy program?
Do I just replace the current caddy problem with this?
How do I keep it up to date?
Do I need to remove my repsoitory version of caddy?

New to this so sorry for the questions.

Yes.

Essentially yes, but follow these steps to not have it compete with the apt package:

Re-run xcaddy build when a new version is released. You can click Watch > Custom > check Releases on the Caddy Github repo to get alerts for when new releases are out.

No, as long as you follow the steps in the link above.

2 Likes

That’s great thanks for your help.

I was able to follow your guide and create a custom caddy version with the module you suggested.

I followed your guide to use the custom version and after a bit of testing (using the awesome “inspect” feature of the plugin) I was able to get it working.

2 Likes

I just wanted to say how seamless the upgrade has been too, thanks for the info.

As Caddy is still installed from the repository but the system uses the custom version I created using xcaddy due to the

sudo update-alternatives --install /usr/bin/caddy caddy /usr/bin/caddy.custom 50

I’m able to know when an update is available.

When there is, I’ve created a simple script that just reruns the command

./xcaddy build --with

Copy the newly created caddy file across to /usr/bin/caddy.custom and restart the service and I’m running the latest version.

Brilliant :smile:

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.