BaseURL not respected

Hi, I am running hugo in a Kubernetes container.
My dev.yaml contains:

baseURL: "//localhost:9301/"

However if I do {{ .Site.BaseURL }} in my template, this still returns: //localhost:8080/.

my hugo arguments: hugo serve --verbose --port=8080 --bind= --gc --source /site --config /site/config/dev.yaml --disableLiveReload

Any ideas?

The CLI flags override the baseURL that is set in the project config.


 hugo serve --verbose --port=8080 --bind= --gc --source /site --config /site/config/dev.yaml --disableLiveReload

You would get localhost:8080 and not localhost:9301

Hmm. So how can I get hugo to listen on my container port and, while still retaining the correct baseURL? Provide it via CLI too?

You are setting the --port that the local server will use.

The baseURL value is meant for project deployment, basically the baseURL is used to correctly output the permalinks of the various assets and pages.

I do not quite understand your setup.

It seems that you are trying to run the local server provided by Hugo, as a deployment server within a Kubernetes container. This is not my area of expertise.

Others may have more insights, hopefully they’ll share.

To clarify, I am running hugo on a container. The port hugo is using (and thus the container) is not equal to the port that is actually used to access the application. So the BaseURL should be different.

From your OP above you are using hugo serve and with that command you are actually invoking the local Hugo Server.

The command hugo outputs all files and pages of a Hugo project under the /public/ directory.

The baseURL value is used to write the output under /public/ when the command hugo is executed and not when hugo server is invoked.

The local Hugo Server by default runs on localhost:1313 and with your command above it runs on localhost:8080.

hugo and hugo server are two different commands.

I don’t know a lot about Kubernetes, but I think your setup and problem is something like this:

  • you’re serving with hugo inside the container on port 8080
  • your container config maps inbound port 9301 -> port 8080
  • and so you’re trying to load a page by visiting http://localhost:9301 from the host machine, but all the links are broken because they’ve been rewritten to http://localhost:8080 by the port setting?

Is that right?

If that’s the case, it sounds like the CLI switches break the behaviour that you’re looking for. Is it possible to work around the issue by instead running hugo serve with --port 9301 and mapping 9301 -> 9301 in your container config?

Hi @capnfabs , that’s correct.
However i cannot use the same port since I’m running multiple instances of hugo containers, so they need separate ports. (so same container but different hugo config)

The following does not seem to have any effect: hugo serve --verbose --port=8080 --bind= --gc --source /site --config /site/config/dev.yaml --disableLiveReload --baseURL "http://localhost:9301/"

Ah, I see.

I think I would try building with hugo build and then serving with a different program which is able to serve on the correct port. Given that you’re disabling the live reload functionality, I get the impression you might not need hugo serve, which is intended only for local development I think.

Otherwise, I took a quick look for the code that seems to be messing with the baseUrl. Have you seen the --appendPort option? It looks like setting --appendPort=false might do the trick. But, I think this solution is nastier and probably more prone to breakage in the future, because you seem to be pushing up against the edges of what hugo serve was intended for.

I wonder what a better UX here would be though; I think keeping hugo serve fiddling with the port is really useful for the general use case, but you’re right that it’s extremely confusing in this (somewhat rarer) combination.