##
# Friendica Nginx configuration
# by Olaf Conradi, modified by Philipp Holzer
#
worker_processes 4;

events {
  worker_connections 1024;
}

error_log /var/log/nginx/error.log warn;
pid       /var/run/nginx.pid;

http {
  charset utf-8;

  include       /etc/nginx/mime.types;
  default_type  application/octet-stream;

  log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';

  access_log    /var/log/nginx/access.log main;

  # If behind reverse proxy, forwards the correct IP
  set_real_ip_from 10.0.0.0/8;
  set_real_ip_from 172.16.0.0/12;
  set_real_ip_from 192.168.0.0/16;
  set_real_ip_from fc00::/7;
  real_ip_header X-Real-IP;

  upstream php-handler {
    server app:9000;
  }

  server {
    listen 80;
    server_name friendica.local;

    index index.php;

     root /var/www/html;
       #Uncomment the following line to include a standard configuration file
       #Note that the most specific rule wins and your standard configuration
       #will therefore *add* to this file, but not override it.
     #include standard.conf
     # allow uploads up to 20MB in size
     client_max_body_size 20m;
     client_body_buffer_size 128k;

     # rewrite to front controller as default rule
     location / {
       if (!-e $request_filename) {
         rewrite ^(.*)$ /index.php?pagename=$1;
       }
     }
    # make sure webfinger and other well known services aren't blocked
    # by denying dot files and rewrite request to the front controller
    location ^~ /.well-known/ {
      allow all;
      if (!-e $request_filename) {
        rewrite ^(.*)$ /index.php?pagename=$1;
      }
    }

    # statically serve these file types when possible
    # otherwise fall back to front controller
    # allow browser to cache them
    # added .htm for advanced source code editor library
    #location ~* \.(jpg|jpeg|gif|png|ico|css|js|htm|html|ttf|woff|svg)$ {
    #  expires 30d;
    #  try_files $uri /index.php?pagename=$uri&$args;
    #}

    include mime.types;

    # block these file types
    location ~* \.(tpl|md|tgz|log|out)$ {
      deny all;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    # or a unix socket
    location ~* \.php$ {
      # Zero-day exploit defense.
      # http://forum.nginx.org/read.php?2,88845,page=3
      # Won't work properly (404 error) if the file is not stored on this
      # server, which is entirely possible with php-fpm/php-fcgi.
      # Comment the 'try_files' line out if you set up php-fpm/php-fcgi on
      # another machine.  And then cross your fingers that you won't get hacked.
      try_files $uri =404;

      # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
      fastcgi_split_path_info ^(.+\.php)(/.+)$;

      fastcgi_pass php-handler;

      include fastcgi_params;
      fastcgi_index index.php;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    # deny access to all dot files
    location ~ /\. {
      deny all;
    }
  }
}