Setting Up Friendica on Nginx

You’ve made the switch to nginx (“engine-x”)? You want to run Friendica on your nginx-powered server?

Here’s a short guide on how you can setup your Friendica to run on Nginx. So jump on in…

Note: This tutorial assumes that you already have your Nginx server and PHP running, if not, feel free to search online for instructions on how to ready your server. Without further ado, let’s begin.


  1. Open your /etc/nginx/sites-available/domain.tld configuration file
  2. Copy this to your file (or adjust it if you already have one):
    server {
      listen 80;
    
      server_name domain.tld;
      root /path/to/your/website/domain.tld/public_html;
    
      access_log off;                             # If you are using 'Analytics' type software for tracking, keep this 'off'
      log_not_found off;                          # Turn on if you want to track "not found" errors
      error_log /path/to/your/website/domain.tld/logs/error.log crit;          # valid values: debug, info, notice, warn, error, crit
      #rewrite_log on;                            # Uncomment if you want to debug your rewrites (then change 'crit' above to 'notice')
    
      # block stuff early
      # Do not log favicon.ico and robots.txt stuff
      location ~* /(favicon\.ico|robots\.txt) {
        allow all;
        access_log off;
        log_not_found off;
      }
    
      # Return error 444 for these files
      location ~* ^.+\.(bzr|git|log)$ {
        access_log off;
        log_not_found off;
        return 444;
      }
    
      # Deny public access to ~ (bak) files
      location ~* ~$ {
        access_log off;
        log_not_found off;
        return 444;
      }
    
      #############
      # Friendica #
      #############
      location / {
        try_files $uri $uri/ @friendicacleanurl;
      }
    
      location @friendicacleanurl {
        rewrite ^/(.*) /index.php?q=$uri last;
        break;
      }
    
      #######################
      # Security: Friendica #
      #######################
      # block public access to .htaccess and .htconfig.php
      location ~* /\.ht {
        access_log off;
        log_not_found off;
        return 444;
      }
    
      # block public access to .tpl files located in /view/ folder
      location ~* /view/(.*)\.tpl$ {
        access_log off;
        log_not_found off;
        return 444;
      }
    
      # block public access to /util/ folder
      location ^~ /util/ {
        access_log off;
        log_not_found off;
        return 444;
      }
    
      #################################
      # Deliver static files directly #
      #################################
      # images (Friendica)
      location ~* /(addon|images|library|spec|util|view)/(.*)\.(bmp|cur|gif|ico|j2k|jp2|jpe|jpeg|jpf|jpg|jpm|jpx|mj2|mng|png|svg|svgz|thm|tif|tiff|webp)$ {
        add_header Pragma "public";
        add_header Cache-Control "public";
        access_log off;
        log_not_found off;
        expires 28d;
      }
    
      ############################
      # redirect 50x error pages #
      ############################
      error_page 500 502 503 504 /50x.html;
      location = /50x.html {
        root /usr/share/nginx/html;
        internal;
      }
    
      ##############
      # enable PHP #
      ##############
      location ~ \.php$ {
        try_files $uri =404;
    
        fastcgi_split_path_info ^(.+\.php)(.*)$;
    
        fastcgi_pass 127.0.0.1:9000;                       # Comment if you want to use sock instead of tcp
        #fastcgi_pass unix:/var/run/php-fpm.sock;          # Uncomment to use sock instead of tcp
    
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    
        include /etc/nginx/fastcgi_params;
      }
    }
  3. Type:
    sudo service nginx restart

You’re done!

Take note however that this is a basic configuration only. There are other nginx settings that you can setup which isn’t covered by this tutorial simply because those settings are not Friendica-specific settings. But for your use, here are some useful configurations you may want to experiment on. Note that, all of the values are set to default.

##################
# Request limits #
##################
#open_file_cache max=1000 inactive=1h;              #=off; http/server/location
#open_file_cache_errors on;                         #=off; http/server/location
#open_file_cache_min_uses 3;                        #=1; http/server/location
#open_file_cache_valid 1m;                          #=60; http/server/location

###############
# Size limits #
###############
client_body_in_file_only off;                       # always keep this 'off'
client_body_buffer_size 16k;                        #=8k/16k; http/server/location
client_header_buffer_size 1k;                       #=1k; http/server
client_max_body_size 10m;                           #=1m; http/server/location
                                                      # must match php.ini post_max_size and upload_max_filesize
large_client_header_buffers 4 8k;                   #=4 4k/8k; http/server

############
# Timeouts #
############
client_body_timeout 60;                             #=60; http/server/location
client_header_timeout 60;                           #=60; http/server
keepalive_disable msie6;                            #=msie6 safari; http/server/location
keepalive_timeout 300 300;                          #=75 75 (300 300 is for SSL); http/server/location
keepalive_requests 100;                             #=100; http/server/location
send_timeout 60;                                    #=60; http/server/location
tcp_nodelay on;                                     # http/server/location

If you found a good working setup, don’t forget to share it with the rest of us!


Share and Enjoy

  • Google Plus
  • Facebook
  • Twitter
  • StumbleUpon
  • Tumblr
  • http://pedroassuncao.com/ Pedro Assuncao


    /etc/nginx/fastcgi_params” failed (2: No such file or directory