I currently host content on the following paths:
http://oldsite/samples/content/
http://oldsite/samples/ql
http://oldsite/samples/api/content/plans/
http://oldsite/samples/api/streams/map.xml
...
I have a new site coming up which will eventually serve that content under:
http://newsite/content/
http://newsite/ql
http://newsite/api/content/plans/
http://newsite/api/streams/map.xml
...
Meaning the samples subpath will be removed.
So basically the inverted version of:
Nginx redirect one path to another
During this transition my plan is to rewrite those path using nginx. Currently I have this configured:
/etc/nginx.conf.d/sandbox.conf
server {
#listen 80;
#listen [::]:80;
listen 8082 default_server;
listen [::]:8082 default_server;
location / {
proxy_pass http://localhost:8080/;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Port $server_port;
location /content/ {
rewrite 301 /samples/content/;
}
location /ql {
rewrite 301 /samples/ql;
}
location /api/content/plans/ {
rewrite 301 /samples/api/content/plans/;
}
location /api/streams/map.xml {
rewrite 301 /samples/api/streams/map.xml;
}
}
}
But is there a more clean way to do this - using some regex/wildcard pattern so I don't have to write out each path?
Also if I instead do:
/etc/nginx.conf.d/sandbox.conf
server {
#listen 80;
#listen [::]:80;
listen 8082 default_server;
listen [::]:8082 default_server;
location / {
proxy_pass http://localhost:8080/;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Port $server_port;
location /samples/content/ {
rewrite 301 /content/;
}
location /samples/ql {
rewrite 301 /ql;
}
location /samples/api/content/plans/ {
rewrite 301 /api/content/plans/;
}
location /samples/api/streams/map.xml {
rewrite 301 /api/streams/map.xml;
}
}
}
I get:
$ curl localhost:8082/content
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.18.0 (Ubuntu)</center>
</body>
</html>
and access log:
, request: "GET /content HTTP/1.1", host: "localhost:8082"
2021/03/15 10:05:18 [error] 81448#81448: *4 open() "/usr/share/nginx/html/content" failed (2: No such file or directory), client: 127.0.0.1, server: , request: "GET /content HTTP/1.1", host: "localhost:8082"
Where bypassing nginx I get:
curl localhost:8080/samples/content
Hello!
The following will remove prefix
/samples
from all redirected URLs:If you want to use another domain, use:
In the
location
block.