and how to make YOURLS work with Nginx.

at and how to make YOURLS work with Nginx.

Sometimes I wonder… why do I do such things?

Well, honestly I am a megalomaniac, and all the most famous websites have an URL shortening service for their own service. Since (one day) FoOlRulez will be the biggest website evar, I thought… why not adding the short URL service early? Doing it was far harder than thinking of it.

You know, we aren’t using a LAMP (Linux-Apache-MySQL-PHP) server, but an Nginx powered server (LNMP sounds weird tho). This means most of the software needs some fix to work correctly, though it will work much faster. This means YOURLS, the software I intended to use, needed some fix as well.

For this fix, I had to find a solution, and I have found it here: 使用 Yourls 强化你的短网址系统 – Showfom’s Blog.
I have no idea what that URL says… but hey, I found the fix to make YOURLS work!

if (!-f $request_filename){
set $rule_0 1$rule_0;
if (!-d $request_filename){
set $rule_0 2$rule_0;
if ($rule_0 = "21"){
rewrite ^/([0-9a-z]+)/?$ /yourls-go.php?id=$1 last;

Gibberish? No this is the code you need to make it work in your Nginx installation! Paste it where you need it. If you don’t know how comment here and I’ll explain a bit further.

Anyway, this made everything work… a bit of styling of the homepage et voilà!

It works! I can’t believe it myself!

Let’s go a bit more indeep with YOURLS. This is a very complete script to make your own URL shortener, by:

I think they’re the same who made a few of the plugins I use in this WordPress blog (yeah they are). Anyway, this script is powerful enough to not leave you dry mouthed: you get an API to use the URL shortening service on your server from anywhere, the admin menu lets you control the tidbits you need (ok, most of the controls are via code, but it’s easily done with the explanations), the code has explanation for most of the lines – I in fact edited it to fit my needs better.

Best part of all, it comes with a WordPress plugin, that hooks to the shortening service and makes a short url for every post: even this one post has a shortening url. Then you get automatic submission of the post to Twitter after publishing. The button to submit to twitter on the top is my creation by using the functions (wordpress hooks) that YOURL for WordPress provides.

35 Responses to “ and how to make YOURLS work with Nginx.”

  1. Nyarth says:

    The URL you linked above says “Use your short-Yourls enhanced web-based system – Showfom’s Blog.” /google translated =P

    So this is basically like Tinyurl/ huh?

  2. muge says:

    Oh hey, this is pretty cool.
    Once the database “registers” a custom keyword, does it expire after a while?
    Or is it permanently stored?

  3. David Benfell says:


    It didn’t work for me.

    The relevant configuration:

    server {
    listen; ## listen for ipv4
    listen [2001:470:8:1ae::2]:80; ## listen for ipv6
    root /home/www/;
    access_log /home/www/;
    location / {
    index index.php;
    if (!-f $request_filename){
    set $rule_0 1$rule_0;
    if (!-d $request_filename){
    set $rule_0 2$rule_0;
    if ($rule_0 = “21″){
    rewrite ^/([0-9a-z]+)/?$ /yourls-go.php?id=$1 last;
    location ~ \.php$ {
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param SERVER_NAME $http_host;
    fastcgi_ignore_client_abort on;

    location ~ /\.ht {
    deny all;

    My guess is that it isn’t the rewrite rules you found but the fastcgi implementation. Any ideas?


  4. Jedidiah Hurt says:

    Based on the contents of the .htaccess file generated by Yourls, this seems to be the most direct translation to Nginx config (seems to be working fine for me, though I didn’t bother to translate the ‘path’ prefix, as I don’t need it):

    if (!-e $request_filename) {
    rewrite ^ /yourls-loader.php last;

  5. Pedro Galvan says:

    Hi guys. Used the info here to set up yourls in nginx. Pretty much everything is working except stats. So, from the admin interface, if I click on the icon for stats of a specific link, it tries to go to that url appending a ‘+’ at the end. But my nginx doesn’t recognize that url and sends a 403.

    Has anybody else had this problem?

  6. Inta says:

    Hi, Thanks for the article I’m trynig to get a similar setup working. I’m just concerned about the proxy_cache_key here. $do_not_cache would be 1 for all users who have those cookies. So, if I understand this correctly, a logged in user can see a cached copy of another logged in user. Is that correct?Thanks.

  7. digi says:

    If you want dashes to work in your shortened URL, you’ll need to add a dash after the ‘z’ as shown below.

    location / {
    rewrite ^/([0-9a-z-]+)/?$ /s/yourls-go.php?id=$1 last;

  8. ozh says:

    @woxxy Awesome! I’m adding this to the readme. Stay tuned, next version has link stats & more :)