Skip to content

Instantly share code, notes, and snippets.

@ZE3kr
Last active November 17, 2022 03:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZE3kr/dfe80d881b3665a2631c280bd59d8cc5 to your computer and use it in GitHub Desktop.
Save ZE3kr/dfe80d881b3665a2631c280bd59d8cc5 to your computer and use it in GitHub Desktop.
Nginx Short Link
http {
# Other configs
map $uri $url_short {
default /404;
/ /;
include url.map;
}
server {
# Other configs
if ($url_short = /) {
# What to do with main page?
return 301 https://www.example.com;
}
if ($url_short = /404) {
# How to handle 404?
return 404 '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>404 Not Found</title>
</head><body style="margin-top: 32px;">
<center><h1>404 Not Found</h1>
<p>Sorry! The page you are looking for could not be located</p>
<p>Visit the <a href="/">homepage</a></p>
</center></body></html>
';
}
location / {
return 301 $url_short;
}
}
}
# Put all URLs in this file
/1 https://example.com;
/2 https://example.net;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment