I have a docker image that serves an Angular app via an nginx webserver. The image is to be deployed into a kubernetes cluster, exposed behind an nginx-ingress resource, so all my DNS config is handled by ingress.
A requirement appeared where I'm supposed to keep some legacy configuration, mainly a URI of the form /some/path/to/stuff/{a large code}/something/{another code}
. I have the regex settled and everthing works on that part.
It was requested to perform a redirect from www.example.com/some/path/to/stuff/{a large code}/something/{another code}
to www.example.com/path/{a large code}/something/{another code}
?
The constraint being is that the only modification I can do is modifying the nginx.conf file that's injected in the Dockerfile
.
I tried location regex with proxy_pass
, rewrite
and return 301
.
From my tests it seems impossible to do while being DNS-agnostic.
Tl;dr can redirects be performed on webservers while being DNS agnostic?
EDIT: This is the Dockerfile
:
FROM node:12.16.1-alpine AS builder
WORKDIR /usr/src/app
COPY package.json package-lock.json ./
RUN npm install
COPY . .
RUN npm run build-prod
FROM nginx:1.15.8-alpine
RUN rm -rf /usr/share/nginx/html/*
COPY ./nginx.conf /etc/nginx/nginx.conf
COPY --from=builder /usr/src/app/dist/dish-ui/ /usr/share/nginx/html
This is the base nginx.conf
file:
worker_processes 1;
events {
worker_connections 1024;
}
http {
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
index index.html index.htm;
include /etc/nginx/mime.types;
gzip on;
gzip_min_length 1000;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;
location / {
try_files $uri $uri/ /index.html;
}
}
}
What I tried: 1. added rewrite rules in location blocks:
location ~* ".*([a-z0-9]{64})/something/([a-z0-9]{64})quot; {
rewrite ".*([a-z0-9]{64})/something/([a-z0-9]{64})quot; /new/$1/$2/path;
}
The above configuration caused 404 error because the new path couldn't be found in the root folder.
rewrite ".*([a-z0-9]{64})/something/([a-z0-9]{64})quot; /new/$1/$2/path;
This would only check once, subsequent requests wouldn't get rewritten. It also didn't redirect properly.
location ~* ".*([a-z0-9]{64})/something/([a-z0-9]{64})quot; {
proxy_pass http://127.0.0.1/new/$1/$2/path;
}
This didn't work either. My index.html would alway be loaded, it seemed that the rule was ignored completely, I also placed it BEFORE location /.
I settled on hardcoding the correct URL and use a 301 return
.