0

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!
2
  • This nginx configuration seems to be doing rewrites in the opposite direction, adding /samples to the URL, but you said that you want to remove it. What is going on? Mar 14, 2021 at 21:44
  • I have updated the post. If I "reverse" the location I get another error it seems its using /usr/share/nginx/html/ as root.
    – u123
    Mar 15, 2021 at 9:09

1 Answer 1

0

The following will remove prefix /samples from all redirected URLs:

location ~ /samples(.*) {
    return 301 $1;
}

If you want to use another domain, use:

return 301 https://new.example.com$1;

In the location block.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .