YOURLS and Nginx, an updated config

By | January 27, 2012

YOURLS is a simple php package that lets you set up your own URL shortener, avoiding privacy issues and problems with a shortener going out of ┬ábusiness. I use it on pcurl.us, but with my recent server move I’m switching all of my sites to Nginx, which requires converting apache rewrite rules to Nginx versions.

The official suggestion on the YOURLS site involves heavy use of ifs, which is discouraged in nginx, and is also missing support for info pages and case-sensitive short urls. I wrote a new version, that was better, but it was still more complex than I liked. In setting up another instance for one of the people I host though, I realized that YOURLS has actually made my job a lot easier than I thought. The updated config is below:

server {
  listen 178.33.177.161:80;
  server_name pcurl.us;
  access_log off;
  error_log /var/log/nginx/pcurl.us.error.log;
  root /var/www/pcurl.us/htdocs;
  location / {
    try_files $uri $uri/ /yourls-loader.php;
    location ~ \.php$ {
      fastcgi_pass   127.0.0.1:9000;
      fastcgi_index  index.php;
      fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
      include        fastcgi_params;
    }
  }
}

13 thoughts on “YOURLS and Nginx, an updated config

  1. PacketCollision Post author

    I don’t know what the problem is. Maybe your host isn’t set properly in includes/config.php? Also, make sure you’re using the latest version of yourls (1.5) and post your entire config file (and any includes) and I’ll try to help.

  2. bubs

    I have the same problem as above, this is the website config:

    server {
            listen 217.79.190.118:80;
    		
            listen 217.79.190.118:443 ssl;
            ssl_certificate /var/www/clients/client1/web5/ssl/tppu.me.crt;
            ssl_certificate_key /var/www/clients/client1/web5/ssl/tppu.me.key;
            
            server_name tppu.me www.tppu.me;
    
            root   /var/www/tppu.me/web;
    		
    		
    		
            index index.html index.htm index.php index.cgi index.pl index.xhtml;
    		
    		
            location ~ \.shtml$ {
                ssi on;
            }
    
    		
            error_page 400 /error/400.html;
            error_page 401 /error/401.html;
            error_page 403 /error/403.html;
            error_page 404 /error/404.html;
            error_page 405 /error/405.html;
            error_page 500 /error/500.html;
            error_page 502 /error/502.html;
            error_page 503 /error/503.html;
            recursive_error_pages on;
            location = /error/400.html {
                internal;
            }
            location = /error/401.html {
                internal;
            }
            location = /error/403.html {
                internal;
            }
            location = /error/404.html {
                internal;
            }
            location = /error/405.html {
                internal;
            }
            location = /error/500.html {
                internal;
            }
            location = /error/502.html {
                internal;
            }
            location = /error/503.html {
                internal;
            }
    		
            error_log /var/log/ispconfig/httpd/tppu.me/error.log;
            access_log /var/log/ispconfig/httpd/tppu.me/access.log combined;
    
            ## Disable .htaccess and other hidden files
            location ~ /\. {
                deny all;
                access_log off;
                log_not_found off;
            }
    		
            location = /favicon.ico {
                log_not_found off;
                access_log off;
            }
    
            location = /robots.txt {
                allow all;
                log_not_found off;
                access_log off;
            }
    		
            location /stats {
                index index.html index.php;
                auth_basic "Members Only";
                auth_basic_user_file /var/www/clients/client1/web5/.htpasswd_stats;
            }
    
            location ^~ /awstats-icon {
                alias /usr/share/awstats/icon;
            }
    
            location ~ \.php$ {
                try_files $uri =404;
                include /etc/nginx/fastcgi_params;
                fastcgi_pass 127.0.0.1:9014;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_param PATH_INFO $fastcgi_script_name;
                fastcgi_intercept_errors on;
            }
    		
            location /cgi-bin/ {
                try_files $uri =404;
                include /etc/nginx/fastcgi_params;
                root /var/www/clients/client1/web5;
                gzip off;
                fastcgi_pass  unix:/var/run/fcgiwrap.socket;
                fastcgi_index index.cgi;
                fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                fastcgi_intercept_errors on;
            }
    
    
    
      location /u/ {
        try_files $uri $uri/ /u/yourls-loader.php;
      }
    	
    }
  3. digi

    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;
    }

  4. Andrei Chira

    I’m using Yourls in a folder /s/ but I get 404 errors when accessing the short urls. If I use the rewrite I get 403 error for /s/admin.
    Here’s my setup:

    location /s/ {

    try_files $uri $uri/ /yourls-loader.php;

    location ~ \.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;
    }

    }

    Can you help me?

  5. Pingback: YOURLS with nginx on Mac OSX 10.8.4 | GreenLegos's Blog

  6. Pingback: Your(ls) Own Short URL service on Raspberry Pi | KrishnaChaitanya Ch

  7. Pingback: How I Created My Own Link Shortener For Under $4 - SLee And Topher

Comments are closed.