I recently started using Nginx (2 days ago, honestly). As always, it was for a WordPress site that I needed to build, with its own URL shortner using YOURLS.

I’ve installed YOURLS on Apache a million times(not counting) without any issues. It’s easy to install on Apache. You really don’t have to do anything apart from configuring the config.php file. But with Nginx, YOURLS wasn’t working properly.

WordPress was installed in the root of my domain (let’s call it domain.com), while YOURLS was supposed to be in domain.com/go/ folder.

Visiting /go/ directly led to a 404 error page, while domain.com/go/admin/ was working properly. However, the short URLs weren’t working at all. Anyway, let’s start from the beginning.

Note: You need SSH access to your server in order to follow this tutorial.

First you need to make sure that you’ve Git installed on your server. If it’s not installed, you can run:

sudo apt-get install git

Once Git is installed, you need to run clone YOURLS file to your server. Since we wanted to run YOURLS under /go/ directly in our root, we need to run the following code in our root folder:

git clone --branch master https://github.com/YOURLS/YOURLS.git go

That will clone the Git repo to your server. If you want to remove the git tracking then you can delete the .git folder by running the following command from your /go/ directory:

It’s not required but worth doing it. Now you need to edit your /admin/config.php file. Run the following command from your /go/ directory:

cd admin
mv config-sample.php config.php

Then you need to configure your config.php file as described here with your database info: http://yourls.org/#Config

So yea, that’s it for the installation. That’s where it gets little tricky. Everything is done but it doesn’t work properly.

Now you need to edit your /etc/nginx/sites-available/default file. Run the following command:

sudo nano /etc/nginx/sites-available/default

Now you need to add the following to your default file:

location /go/ {
    location = /go/ {
        try_files $uri $uri /go/admin/;
    }
    try_files $uri $uri/ /go/yourls-loader.php?$args;
}

You can add it before the closing } tag of server {...}. And that should make it work. Don’t forget to restart Nginx before trying witht the following:

sudo systemctl restart nginx

That should do it. Now you can visit domain.com/go/ and it should work properly, so will your short URLs. Hope it helps.