Nginx Cheat Sheet

Table of Contents

Simulating Apache’s UserDir, with nginx.

# Apache userdir simulation.
location ~ ^/~([^/]+)(/.*)?$ {
    alias /home/$1/public_html$2;
    autoindex on;
    ssi on;

Rewrite rules for redirects

They’re regex-based. e.g.:

server {
    # Server-wide redirect

    rewrite ^/software/poll/?.* permanent;
    # Location-specific redirect
    location /bmc/blog/ {
        rewrite /bmc/blog/(.*)$$1;


For instance, for PHP, Python or Ruby.

location ~ \.php {
    include /etc/nginx/fastcgi_params;
    keepalive_timeout 0;
    fastcgi_param SCRIPT_FILENAME /var/www/html/$fastcgi_script_name;
    fastcgi_index index.php;