Need help converting NGINX config

All together:

(proxy_options) {
  header_up X-Real-IP {remote_host}
  header_up X-Forwarded-Proto {scheme}
  header_up Access-Control-Allow-Origin *
  header_up Access-Control-Allow-Credentials true
  header_up Access-Control-Allow-Headers Cache-Control,Content-Type
}

localhost {
  route /weather/* {
    uri replace /weather /socket.io
    reverse_proxy http://localhost:3010 {
      import proxy_options
    }
  }
  route /ptz/* {
    uri replace /ptz /socket.io
    reverse_proxy http://localhost:3006 {
      import proxy_options
    }
  }
  route /liveview/* {
    uri replace /liveview /socket.io
    reverse_proxy http://localhost:3004 {
      import proxy_options
    }
  }
  route /archive/* {
    uri replace /archive /socket.io
    reverse_proxy http://localhost:3003 {
      import proxy_options
    }
  }
  route /alarms/* {
    uri replace /alarms /socket.io
    reverse_proxy http://localhost:3002 {
      import proxy_options
    }
  }
  route /console_socket/* {
    uri replace /console_socket /console/socket.io
    reverse_proxy http://localhost:3001 {
      import proxy_options
    }
  }
  route /web_app_socket/* {
    uri replace /web_app_socket /web/socket.io
    reverse_proxy http://localhost:3001 {
      import proxy_options
    }
  }
  route {
    reverse_proxy http://localhost:3001 {
      import proxy_options
    }
  }
}

(Make sure to double-check I didn’t make any mistakes!! :smiley:)

3 Likes

I had seen something like this and was looking for it this morning, so your response is timely! Thank you so much!

1 Like

When we use NGINX, we have dynamic upstream servers (camera devices). So, you can add a new camera in the interface. This writes out some NGINX upstream config files, then NGINX is retarted. The NGINX main config (default) has this:

# upstreams
include sites-available/*.upstream;
include sites-available/*.conf;

So when NGINX is restarted, it reads the new config files.

They look like this:
conf file

location /dcam8/ivt-hvr-web-app/ {
    proxy_pass http://10.10.100.100:80/;
}
location /dcam8_socket/ {
    proxy_pass http://10.10.100.100:80/socket.io/; 
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
}
location /dcam8_socket_data/{
    proxy_pass http://dcam8_data/socket.io/;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
}
location /dcam8_files/ {
    proxy_pass http://dcam8_data/;
}

upstream file:

upstream dcam8_socket { 
	ip_hash;
	server 10.10.100.100:80;
}
upstream dcam8_data { 
	ip_hash;
	server 10.10.100.100:8080;
}

This is my conumdrum. Use Caddyfile and restart caddy, or use the API to modify the in-memory JSON. I would prefer a single source of truth.

Wondering if a Caddyfile can load another Caddyfile? I haven’t found anything in the docs in regards to this.

You can use the Caddyfile with the API, but you lose the ability to make granular config changes when using a config adapter. (Also: don’t restart the server for config changes, reload the config instead!)

For more advanced, dynamic setups like what you have, is suggest possibly using json with the API, it makes it very easy to add and remove backends. Managing multiple config files is usually more trouble than it’s worth anyway.

1 Like

Yes, the import directive both handles snippets or pulling from other files.

Like Matt said, use caddy reload --config path/to/Caddyfile to do graceful reloads of the Caddy config instead of doing a full restart.

2 Likes

At work, I am writing up a Confluence page on why we should switch to Caddy from NGINX. I have to do due diligence and provide Pros and Cons. Even for Caddy itself, vis a vis Caddyfile vs JSON configs, etc.

I have learned a LOT in the past few days. I really appreciate you guys answering my questions and giving help, so we can make this decision soon.

I love dynamic communities where people pitch in to help others.

For our needs, I think we will be going the JSON route, so we can add upstream servers dynamically.

One thing I don’t understand, is when a computer is reboooted, will the service start up similar to caddy run --reload?

2 Likes

Cool, thanks for putting effort into learning about it :slight_smile:

Using our standard service file, which has the --resume flag you’ll notice, Caddy will resume its last active configuration upon starting up, overriding the --config flag if present. This avoids data (config) loss when using the API after the initial config file is loaded. :+1:

Were our docs helpful in your journey? Anything significant we should improve about them?

@matt I have to say, the docs were very confusing for beginners trying to find something. In fact, I’d go so far to say, if you can’t find the information readily available right away, some would leave (like waiting for a browser page to display - I think the attention span is like 6 seconds). Anyway, what I did learn was from you and @francislavoie (thank goodness!). Once I started learning some things from you guys, I was able to traverse the docs a bit better to find more information.

I think there needs to be more examples. The problem with that is everyone’s needs are a bit different. And, I know from experience, if you put up examples, they copy directly and then complain when it doesn’t work for them.

I really like Caddy, or at least the concept of it, which kept me pursuing what needed to be done. Obviously, I am still learning. I was given a couple more days for the investigation, because a Thursday meeting we had about it I was able to show promising results. If we decide to go with Caddy, I will be back and get a bit more involved. Unfortunately, they won’t be doing any sponsoring, considering the circumstances. Last Tuesday, everyone’s salaries dropped 40% so no one would be let go. :frowning:

But, I would certainly help out in the docs area, from a newbie perspective. As you can see, I am hugely motivated in FOSS (hawkeye64 (Jeff Galbraith) · GitHub).

What exactly was confusing? The Getting Started guide? The numerous quick-start guides? The other tutorials? Be specific, and we can fix it.

What couldn’t you find in ~6 seconds?

I’m worried about this, too. I suppose we could make examples into screenshots and then they can’t be blindly copied+pasted.

No worries. That’s a tough situation. Take care!

Be specific, and we can fix it.

Specifically, this page. JSON Config Structure - Caddy Documentation and this page: JSON Config Structure - Caddy Documentation . It left me very confused, even after reading the explanations below. I felt very lost. A lot of “yeah, but how
?” came to mind. I think that you have to drill down (which is an interesting concept) is part of it. I wouldn’t change the docs, but maybe a “related” araea or something. Trying to find the “uri” part (for uri replace /web_app_socket /web/socket.io is still a bit of a chore).

In fact, I still haven’t found that information. I get this far to routes but not route
image

What couldn’t you find in ~6 seconds?

That’s not what I meant actually. That’s in correlation to a web page spining up aka lighthouse info.

No worries. That’s a tough situation. Take care!

Yes, and you take care as well. The world is definiately not right currently and precautions all around are needed. We just did our first online grocery shopping. It’s all backed up, so we get it a week from monday (tomorrow), so an 8 day wait.

Hmm, pretty easy. In the sidebar, Caddyfile > Directives > in the table > uri

That’s fine, that’s really meant to be a structure reference than a “how to use”.

I’m confused, why are you looking in the JSON docs for Caddyfile information? Those are two separate things. Caddyfile syntax adapts to JSON, but it is not a 1:1 match at all, it has all kinds of tricks to improve config UX.

Really? Every page loads in <1s for me.

To be fair, we could do a better job at linking the two, at least in the descriptions/paragraphs, so that people can find some correspondence between them.

But yeah, it’s tricky because directives don’t necessarily map 1:1 with JSON structures. They’re two totally different ways at configuring Caddy.

I’ll see what we can do about this. Maybe it’s just a matter of adding a sentence near the top like “To craft a basic HTTP server, 
”

Really? Every page loads in <1s for me.

Again, I am misunderstood with this respect. I was just giving an analogy on attention span only. For different items, like web pages, it’s low. For other things it’s high. I still don’t think I am conveying properly. Except to say, if the reviewer doesn’t find answers right away they are likely to get 1) frustration, followed by 2) desparation, then 3) give up.
To be honest, I am not really criticizing, just trying to help.

I’m confused, why are you looking in the JSON docs for Caddyfile information? Those are two separate things. Caddyfile syntax adapts to JSON, but it is not a 1:1 match at all, it has all kinds of tricks to improve config UX.

As I mentioned before, I had to write docs with pros and cons. That included Caddyfile vs JSON config

Going back to trying to find the correct info, one thing that made it more difficult for me was all of the v1 information around the web. But, I did start to see the differences, so when I’d find a medium article, I could recognize that it was v1.

Again, I am just tryingh to “tap” my expereince as a newbie before I get too “experienced” at it and forget the struggles I went through. And, I am only doing this to help Caddy grow. :slight_smile:

1 Like

Aye, we appreciate that feedback a lot. We’re just trying to map out those pain points with precision so we can see what’s actionable, hopefully it doesn’t seem like we’re trying to put you on the spot and grill you over it.

3 Likes

I also find that information which is found on the web is mostly for Caddy V1, and here in Europe we also have the Problem that Caddy is also a Car Type from Volkswagen which doesnt make searching information on the internet a lot easier (they also have a Caddy 2 :slight_smile: ) .

Maybe some full examples would be fine, or a complete config with all default values if there are some.

but nevertheless i think Caddy is a superb piece of Software, keep up the good work !

1 Like

Tbh, I never did like the name “Caddy” :stuck_out_tongue:

I’m shocked! Absolutely astonished! Had no idea that was the case! :joy:

1 Like

Suggestion (for the name):

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