Server config for YOURLS on nginx

I recently converted my DigitalOcean droplets to nginx instead of apache as their webserver, mainly because apache uses tons of RAM and my database was running out of overhead.

Unfortunately, YOURLS doesn’t have very good any official documentation on how to configure the appropriate virtualhost/rewrite rules. Here’s the config that finally worked for me.

UPDATE: This works for regular http:// urls, but I’m having issues with https:// urls. Will update this when fixed.
UPDATE 2014-03-26: Fixed! Thanks to some code I found lying around on http://wandborg.se/2012/09/06/welcome-to-nginx.html

UPDATE 2014-04-01: After having some issues with other sites I was running on nginx, I decided to simplify my life, upgrade 1 tier of RAM (to 1GB) and go back to Apache. I still really like nginx, but other priorities rank somewhat higher than constantly fighting rewrite rules on my webserver.  However, YOURLS was still working great when I switched back.

server {
	listen	80;
	server_name cris.lc;
	access_log /var/www/cris.lc/access.log;
	error_log /var/www/cris.lc/error.log;

	root /var/www/cris.lc/public_html;
	index index.html index.htm index.php;

    location / {
        try_files $uri $uri/ @yourls;
    }

    location @yourls {
        rewrite ^(.*)$ /yourls-loader.php;
    } 

    location ~ .php$ {
		fastcgi_split_path_info ^(.+.php)(/.+)$;
		fastcgi_pass   unix:/var/run/php5-fpm.sock;
		fastcgi_index  index.php;
		fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
		include        fastcgi_params;
	}

	location ~ /.ht {
		deny all;
	}
}

Related posts

4 thoughts on “Server config for YOURLS on nginx

  1. jamov

    Thanks for the info. However, I can’t get “yourls” to install. When trying to access my yourls install at “http://yourls.example.com/admin” or “http://yourls.example.com/admin/install.php”, all I get is “File not found.” from yourls. Any thoughts?

    1. cristoslc

      Could you upload your config file to something like http://pastebin.com and provide a link? Also, what server platform are you using? (i.e., Linux version, PHP handler, etc.)

  2. cristoslc

    Looks like there’s been some further discussion of this in the YOURLS project on Github, including one recommended (but not yet official) config file: https://github.com/YOURLS/YOURLS/issues/1715

    server {
    listen 80;
    listen [::]:80;

    root /srv/http/website/public;
    index index.php index.html index.htm;

    server_name website;

    # Logging
    access_log /srv/http/website/log/access.log;
    error_log /srv/http/website/log/error.log warn;

    # Custom Stuff
    if ( $uri !~ ^/(robots\.txt|favicon\.ico|admin) ){
    set $rule_0 1$rule_0;
    }
    if (!-f $request_filename){
    set $rule_0 2$rule_0;
    }
    if (!-d $request_filename){
    set $rule_0 3$rule_0;
    }
    if ($rule_0 = “321”){
    rewrite ^/.*$ /yourls-loader.php last;
    }

    location ~ config.php {
    deny all;
    }

    # Error pages?
    location / {
    try_files $uri $uri/ =404;
    }

    # Remove WWW
    if ($http_host ~* “^www.(.*)$”){
    set $rulea_0 1$rulea_0;
    set $bref_1 $1;
    }
    if ($rulea_0 = “1”){
    rewrite ^/(.*)$ http://$bref_1/$1 permanent;
    }

    # Static File Caching?
    location ~* .(jpg|jpeg|png|gif|ico|css|js)$ {
    expires 1d;
    }

    # Pass PHP files to FPM.
    location ~ \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
    }

    # Deny hidden files.
    location ~ /\. {
    deny all;
    }
    }

Leave a Reply