/nginx-redirect-generator

A simple tool to generate nginx server configuration for redirects by a given URL list.

Primary LanguageJavaScriptMIT LicenseMIT

nginx-redirect-generator

A simple tool to generate nginx server configuration for redirects by a given URL list.

Use case

I had to create an nginx server just to redirect from some old domains to new ones. The list of URLs was big and I couldn't generate a general rule because the path on the target url was different from the source. So I created this to help me out :)

It transforms this:

http://sample-domain.com/test/123123 http://another-domain.com/awe123123
http://sample-domain.com/test/555 http://another-domain.com/aweaw
http://sample-domain.com/test/3 http://another-domain.com/tawe3

http://different-domain.com/test/123123 http://another-domain.com/awe123123
http://different-domain.com/test/4555 http://another-domain.com/aweaw
http://different-domain.com/test123/3 http://another-domain.com/tawe3

Into this:

events {}

http {
  server {
    listen 8080;
    server_name sample-domain.com;
    rewrite ^/test/123123$ http://another-domain.com/awe123123 permanent;
    rewrite ^/test/555$ http://another-domain.com/aweaw permanent;
    rewrite ^/test/(.*)$ http://another-domain.com/path/ permanent;
  }
  server {
    listen 8080;
    server_name different-domain.com;
    rewrite ^/test/123123$ http://another-domain.com/awe123123 permanent;
    rewrite ^/test/4555$ http://another-domain.com/aweaw permanent;
    rewrite ^/test123/3$ http://another-domain.com/tawe3 permanent;
  }
}

How to use

  • Install NodeJS (https://nodejs.org)
  • Clone the repository and open its folder
  • Run npm start <path-to-url-list-file>
  • Done!

Docker image

  • Build docker image with docker build -t nginx-redirect-generator .
  • Create a URL fil list based on the sample-file.txt
  • Run the docker image with docker run --rm nginx-redirect-generator <url-file.txt>

TODO

  • Configure if I want code 301 or 302 on my redirects