Score:0

Trying to get Nginx redirect working in a pod

gd flag

So, I'm trying to do a very basic Nginx redirect from https://strnordicfin.eu/magnesium-fakta to https://strnordicse.eu/ntr/some/magnesium/ (just training domains, no worries).

My nginx-config looks like this,

worker_processes  1;
events { worker_connections  1024;
}
http {
server {
listen 80;
location https://strnordicfin.eu/magnesium-fakta {
rewrite   https://strnordicfin.eu/magnesium-fakta https://strnordicse.eu/ntr/some/magnesium/$request_uri  permanent;
}
}
}
  • but all this achieves is an error to pod and nginx 404 page,
[error] 19#19: *1 open() "/etc/nginx/html/magnesium-fakta" failed (2: No such file or directory), client: 10.244.10.130, server: , request: "GET /magnesium-fakta HTTP/1.1", host: "strnordicfin.eu"

I see that nginx is trying to open from pods /etc/nginx/html/magnesium-fakta, that is an empty directory, it should be getting it's info from https://strnordicse.eu/ntr/some/magnesium/.

Searching from net gives a lot of answers, but in those the server is mainly onsite not in a pod.

Edit: made some adjustments, nginx-config is now -

http {
server {
listen 80;
root /;
location https://strnordicfin.eu/magnesium-fakta/ {
return 302 https://strnordicse.eu/ntr/some/magnesium/magnesium-fakta/$request_uri;
}
}

the error message changed to -

[error] 20#20: *2 "/magnesium-fakta/index.html" is not found (2: No such file or directory), client: 10.244.8.172, server:

but still no redirect.

ws flag
"open() "/etc/nginx/html/magnesium-fakta" failed" - OMG your document root is set to /etc/nginx/html - that's really bad. If you provisioned this docker instance then go fix it now. If you download random images off the innternet and run them then try a different one.
Score:0
in flag

I see two problems here:

  1. Your document root root <path> is not set correctly. You should set it to a folder where your web files are present. For example, if you copy your files in /var/www/html while building your docker image, you should set it to root /var/www/html
  2. Your location block contains a URL whereas it should contain the matching pattern, like what you want to achieve when a matching pattern is found.

So based on your question, your final configuration should be something like this:

http {
  server {
  listen 80
  root /var/www/html;
  
  location ~* /magnesium-fakta {
    return 302 https://strnordicse.eu/ntr/some/magnesium/magnesium-fakta/$request_uri;
    }
  } 
}
Antti Karvinen avatar
gd flag
Thank you. faizan. That location was somewhat difficult, because of that pattern, back to school for me.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.