Nginx minimal configuration

Hi,
I’m trying to configure nginx, but in the documentation it is only proposed a full configuration of nginx. I already have a lot of apps and configurations on my server, so I’d like to use only the minimal configuration, some lines to add to my already config file.
I have tried many thing but I can never go further than the homepage…

Here is my default conf https://pastebin.com/Fk70xtmJ

I tried to add :
location (/grav)/ {
try_files $uri $uri/ (/grav)/index.php?_url=$uri&$query_string;
}
From https://github.com/YunoHost-Apps/grav_ynh/blob/master/conf/nginx.conf I tried :
location /grav/ {
alias /path/to/my/grav/;
index index.php;

	if (!-e $request_filename)
	{
		rewrite ^(.+)$ /grav/index.php?q=$1 last;
	}
	if ($scheme = http) { 
		rewrite ^ https://$server_name$request_uri? permanent;

}

From https://gist.github.com/DarrylDias/2a04dba9688e131eaced I tried :
location /mygrav {
index index.php;
if (!-e $request_filename){ rewrite ^(.*)$ /mygrav/$2 last; }
try_files $uri $uri/ /index.php?$args;
}

But nothing works…

This I believe is the minimal config to get grav working on NGINX:

server {
    listen      80;
    server_name www.domain.tld domain.tld;
    root /var/www/grav_root;
    index index.html index.php;
    access_log  /var/log/nginx/domain.access.log;
    error_log   /var/log/nginx/domain.error.log;
location / {
      try_files $uri $uri/ /index.php?_url=$uri&$query_string;
    }
location ~* /(.git|cache|bin|logs|backups)/.*$ {
        return 403;
    }
location ~* /(system|vendor)/.*\.(txt|xml|md|html|yaml|php|pl|py|cgi|twig|sh|ba$
        return 403;
    }
location ~* /user/.*\.(txt|md|yaml|php|pl|py|cgi|twig|sh|bat)$ {
        return 403;
    }
location ~ /(LICENSE|composer.lock|composer.json|nginx.conf|web.config|htaccess$ 
        return 403;
    }

location ~ \.php$ { fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/var/run/php-fpm-www-data.sock; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_intercept_errors off; fastcgi_buffer_size 16k; fastcgi_buffers 4 16k; } }
---

More clean version… haha

server {
    listen 80;
    server_name www.domain.tld domain.tld;
    root /server_root/grav_root/;
    index index.html index.php;
    access_log /var/log/nginx/domain.access.log;
    error_log /var/log/nginx/domain.error.log;
location / {
      try_files $uri $uri/ /index.php?_url=$uri&$query_string;
    }
location ~* /(.git|cache|bin|logs|backups)/.*$ {
        return 403;
    }
location ~* /(system|vendor)/.*\.(txt|xml|md|html|yaml|php|pl|py|cgi|twig|sh|bat)$ {
        return 403;
    }
location ~* /user/.*\.(txt|md|yaml|php|pl|py|cgi|twig|sh|bat)$ {
        return 403;
    }
location ~ /(LICENSE|composer.lock|composer.json|nginx.conf|web.config|htaccess.t xt|\.htaccess) { 
        return 403;
    }

location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm-www-data.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
fastcgi_intercept_errors off; 
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
    }  
    }
---

Did this work for you? I know Yunohost has some additional settings in nginx, but creating this file in sites-available should be enough.

It works !
But I had to put it under blog.domain.tld as I already have a server directive for domain.tld, I’d like to have it in domain.tld/grav/ (for SEO)
So I tried to put pieces of your conf in domain.tld.d/my_webapp__2.conf, I feel like try_files is the key…

Would guess this does the trick for you:

Put this under your original .conf for your domain and add Grav package under that server root.

location / {
      try_files $uri $uri/ /index.php?_url=$uri&$query_string;
    }
location ~* /(.git|cache|bin|logs|backups)/.*$ {
        return 403;
    }
location ~* /(system|vendor)/.*\.(txt|xml|md|html|yaml|php|pl|py|cgi|twig|sh|bat)$ {
        return 403;
    }
location ~* /user/.*\.(txt|md|yaml|php|pl|py|cgi|twig|sh|bat)$ {
        return 403;
    }
location ~ /(LICENSE|composer.lock|composer.json|nginx.conf|web.config|htaccess.t xt|\.htaccess) { 
        return 403;
    }

location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$; 
fastcgi_pass unix:/var/run/php-fpm-www-data.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
fastcgi_intercept_errors off; 
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
    }

Messing with try_files will also most likely require changes to .htaccess. But yea you are right you can add:

location / {
      try_files $uri $uri/ */grav*/index.php?_url=$uri&$query_string;
    }
---

I can’t because location / { is already declared, so I tried

location / {
      (...)
     location /grav {
         try_files $uri $uri/ /grav/index.php?_url=$uri&$query_string;
   }
location ~* /(.git|cache|bin|logs|backups)/.*$ {
        return 403;
    }
location ~* /(system|vendor)/.*\.(txt|xml|md|html|yaml|php|pl|py|cgi|twig|sh|bat)$ {
        return 403;
    }
location ~* /user/.*\.(txt|md|yaml|php|pl|py|cgi|twig|sh|bat)$ {
        return 403;
    }
location ~ /(LICENSE|composer.lock|composer.json|nginx.conf|web.config|htaccess.t xt|\.htaccess) { 
        return 403;
    }

location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;  
fastcgi_pass unix:/var/run/php-fpm-www-data.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
fastcgi_intercept_errors off; 
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
    }
(...)

But I get a 404 as soon as I try to leave the home page…

Try putting that code outside and under the location that is already there.

nginx -t 
nginx: [emerg] duplicate location "/" in /etc/nginx/conf.d/domain.tld.d/my_webapp__2.conf:39
nginx: configuration file /etc/nginx/nginx.conf test failed
---

You put a location directive within a location.

location / {
      (...)
     location /grav {
         try_files $uri $uri/ /grav/index.php?_url=$uri&$query_string;
   }

It needs to be on its own.

I have some news, I copied the exact same config I have for domain.tld for blog.domain.tld.

It works for blog.domain.tld but not for domain.tld… I can’t understand…

Wait, the difference is I’m forced to access domain.tld with ssl… (I have a file not found error at /grav whereas it works with blog.domain.tld without ssl)

It works with https://github.com/YunoHost-Apps/grav_ynh/blob/master/conf/nginx.conf
I can’t understand why =s but never mind.
Last time I tried it I must have forgotten to change fastcgi_pass
Thank you very much for you help, I understand nginx a bit better now.
See you !