I'm just beginning to setup Flarum for the very first time (the database isn't installed yet) but I have some experience with Nginx and I can share my configuration files here.
This configuration works on my test platform (Lubuntu 18.04) and on my workstation (FreeBSD), except the MySQL part for the moment, obviousy (which will connect using a local socket). I have used the .nginx.conf file provided by Flarum with little modification, but I will see whether it can be optimized.
Please modify the files according to your installation (directories, etc) and server name (which needs to be resolvable). Also, the root directory must be given to the user running the Ngnix daemon (www-data in this case).
I suggest to split the configuration into 2 parts:
- /etc/nginx/nginx.conf
- /etc/nginx/nginx.d/sites.conf
The first file contains directives common to all sites ("servers" in Ngnix parlance, similar to Apache "virtual hosts"), and the second contains the individual directives of the sites.
Hope that this helps.
# /etc/nginx/nginx.conf
user www-data www-data;
worker_processes 1;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
charset utf-8;
index index.html;
include 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"';
error_log /var/log/nginx-error.log;
access_log /var/log/nginx-access.log main;
gzip on;
sendfile on;
merge_slashes on;
keepalive_timeout 75 20;
client_max_body_size 50M;
upstream php7 {
server unix:/var/run/php/php7.2-fpm.sock;
}
include nginx.d/sites.conf;
}
Then
#/etc/nginx/nginx.d/sites.conf
server {
listen 80;
root /var/www/flarum/public;
server_name flarum.yourdomain.com;
error_log /var/log/nginx/flarum-error.log;
access_log /var/log/nginx/flarum-access.log main;
location / {
index index.php;
try_files $uri $uri/ /index.php?$query_string;
}
location ~* \.(?:manifest|appcache|html?|xml|json)$ {
add_header Cache-Control "max-age=0";
}
location ~* \.(?:rss|atom)$ {
add_header Cache-Control "max-age=3600";
}
location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|mp4|ogg|ogv|webm|htc)$ {
add_header Cache-Control "max-age=2592000";
access_log off;
}
location ~* \.(?:css|js)$ {
add_header Cache-Control "max-age=31536000";
access_log off;
}
location ~* \.(?:ttf|ttc|otf|eot|woff|woff2)$ {
add_header Cache-Control "max-age=2592000";
access_log off;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass php7;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
gzip on;
gzip_comp_level 5;
gzip_min_length 256;
gzip_proxied any;
gzip_vary on;
gzip_types
application/atom+xml
application/javascript
application/json
application/ld+json
application/manifest+json
application/rss+xml
application/vnd.geo+json
application/vnd.ms-fontobject
application/x-font-ttf
application/x-web-app-manifest+json
application/xhtml+xml
application/xml
font/opentype
image/bmp
image/svg+xml
image/x-icon
text/cache-manifest
text/css
text/plain
text/vcard
text/vnd.rim.location.xloc
text/vtt
text/x-component
text/x-cross-domain-policy;
}