简体   繁体   中英

Rewrite URL with version number in Nginx

I have a Digital Ocean + Nginx server, which hosts several websites. Now, I want to manage several versions for one website. So I need to add a version number to its urls, and want to apply the following rules:

1) Let's assume the current version number is 1 . Any url without a version number like www.myweb.com/action/... (where action could be anything except a version number) should be rewritten automatically to www.myweb.com/1/action/... .

2) Urls having different version numbers will be treated by different servers (ie, codebase + database) listening to different ports (eg, 3000 and 8080 ).

The follows is my current nginx config file, does anyone know how to modify it to apply the 1st rule (we may implement the 2nd rule later) ?

server {
    listen 80;
    server_name myweb.io www.myweb.io;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;

    server_name myweb.io www.myweb.io;

    ssl_certificate /etc/letsencrypt/live/myweb.io/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/myweb.io/privkey.pem;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_dhparam /etc/ssl/certs/dhparam.pem;
    ssl_ciphers 'ECDHE-RSA-......-SHA';
    ssl_session_timeout 1d;
    ssl_stapling on;
    ssl_stapling_verify on;
    add_header Strict-Transport-Security max-age=15768000;

    location = / {
        return 301 /home;
    }

    location ~ /.well-known {
        allow all;
    }

    location / {
        proxy_set_header    Host                $host;
        proxy_set_header    X-Forwarded-For     $proxy_add_x_forwarded_for;
        proxy_set_header    X-Forwarded-Proto   $scheme;
        proxy_set_header    Accept-Encoding     "";
        proxy_set_header    Proxy               "";
        proxy_pass          https://127.0.0.1:3000;

        proxy_http_version 1.1;
        proxy_set_header   Upgrade $http_upgrade;
        proxy_set_header   Connection "upgrade";
    }
}

So you need a simple rewrite for urls where first path is not a number. So i would add below for Rule 1

location ~* ^/[^0-9]+/? {
  rewrite .* /1$request_uri redirect;
}

This will make sure any path not starting with a number get redirected to correct path with application version. Below is the test results of same

$ curl -I localhost/1/tarun
HTTP/1.1 200 OK
Server: openresty/1.11.2.2
Date: Tue, 31 Oct 2017 18:46:21 GMT
Content-Type: text/plain
Connection: keep-alive

$ curl -I localhost/tarun
HTTP/1.1 302 Moved Temporarily
Server: openresty/1.11.2.2
Date: Tue, 31 Oct 2017 18:46:27 GMT
Content-Type: text/html
Content-Length: 167
Location: http://localhost/1/tarun
Connection: keep-alive

When you want to implement Rule #2 , you should use a map

map $request_uri $app_port {
    ~ "^/1/" "3000";
    ~ "^/2/" "3001";
    ~ "^/3/" "3002";
    default "3000";
}

server  {
  location / {
    ....
    proxy_pass http://127.0.0.1:$app_port$request_uri$is_args$args;

  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM