Nginx

From EQdkp Plus
Jump to navigation Jump to search

Here are some hints for creating a nginx configuration.

Deny access to folders

There are some default folders that should not be accessible.

  • Change c3338889c07ac363fc08bc715b22c59c to the one that fits to your installation (fresh installs of EQdkp Plus 2.3 don't have hash subfolder anymore)
  • These are not all files. Some plugins require more protected folders
  • Check all folders that have a .htaccess files with "Deny all" content. These folders have to be protected.
    • data/<HASH>/tmp
    • data/<HASH>/repository
    • data/<HASH>/live_update
    • data/<HASH>/cache
    • data/<HASH>/eqdkp/backup
    • data/<HASH>/eqdkp/timekeeper
    • data/<HASH>/eqdkp/config
    • data/<HASH>/eqdkp/rss
	location ~ /data/c3338889c07ac363fc08bc715b22c59c/eqdkp/(backup|timekeeper|config|rss) {
		deny all;
	}
	location ~ /data/c3338889c07ac363fc08bc715b22c59c/(tmp|repository|live_update|cache){
		deny all;
	}
	#Fresh installs of 2.3 dont have hash folder anymore
	location ~ /data/eqdkp/(backup|timekeeper|config|rss) {
		deny all;
	}
	#Fresh installs of 2.3 dont have hash folder anymore
	location ~ /data/(tmp|repository|live_update|cache){
		deny all;
	}

PHP-Files

  • Because we are using the structure index.php/someurl/, the configuration for php files must look like this:
	location ~ \.php(/|$) { 
		fastcgi_split_path_info ^(.+?\.php)(/.*)$;
		# With php5-cgi alone, uncomment the following line:
		# fastcgi_pass 127.0.0.1:9000;
		# With php5-fpm (adjust it to your installed PHP Version)
		fastcgi_pass unix:/var/run/php5-fpm.sock;
		fastcgi_index index.php;
		include fastcgi_params;
	}

Adjust the Sock-Path to the PHP version you have installed. You can use "ls -la /var/run/" or "ls -la /run/php/" to list the available PHP sock files.

FastCGI-Params

  • You have to edit the file /etc/nginx/fastcgi_params and add the following lines, if they are not present:
fastcgi_param   PATH_INFO               $fastcgi_path_info;
fastcgi_param   PATH_TRANSLATED         $document_root$fastcgi_path_info;

SEO URLs

  • For removing the index.php from URLs, you can add the following:
	location / {
		index index.php;
		try_files $uri $uri/ /index.php/$uri?$args;
	}
  • You have to adjust the installation path. E.g. if you have installed EQdkp Plus in subdirectory /eqdkp/, it must look like this:
	location /eqdkp/ {
		index index.php;
		try_files $uri $uri/ /eqdkp/index.php/$uri?$args;
	}

Full example

server {
	listen   80; ## listen for ipv4; this line is default and implied
	#listen   [::]:80 default_server ipv6only=on; ## listen for ipv6

	root /var/www/nginx;
	index index.html index.htm index.php;

	# Make site accessible from http://localhost/
	server_name localhost;
	
	# START EQDKP CONFIGURATION
	# =========================
	
	# Deny access to some folders. Change the subfolder-name c3338889c07ac363fc08bc715b22c59c to the one of your installation. 
	# Please keep in mind, that there will be more if you install plugins etc.
	
	location ~ /data/c3338889c07ac363fc08bc715b22c59c/eqdkp/(backup|timekeeper|config|rss) {
		deny all;
	}
	location ~ /data/c3338889c07ac363fc08bc715b22c59c/(tmp|repository|live_update|cache){
		deny all;
	}
	#For Fresh installs of EQdkp Plus 2.3
	location ~ /data/eqdkp/(backup|timekeeper|config|rss) {
		deny all;
	}
	#For Fresh installs of EQdkp Plus 2.3
	location ~ /data/(tmp|repository|live_update|cache){
		deny all;
	}
	
	# Configuration of php Files
	# Maybe you have to adjust fastcgi_pass unix:/var/run/php5-fpm.sock;
	location ~ \.php(/|$) { 
		fastcgi_split_path_info ^(.+?\.php)(/.*)$;
		# With php5-cgi alone, uncomment the following line:
		#fastcgi_pass 127.0.0.1:9000;
		# With php5-fpm:
		fastcgi_pass unix:/var/run/php5-fpm.sock;
		fastcgi_index index.php;
		include fastcgi_params;
	}
	
	# SEO URLs. Here you have to change your install directory
	location / {
		index index.php;
		try_files $uri $uri/ /index.php/$uri?$args;
	}
	
	# END EQDKP CONFIGURATION
	# =========================

	# Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
	#location /RequestDenied {
	#	proxy_pass http://127.0.0.1:8080;    
	#}

	#error_page 404 /404.html;

	# redirect server error pages to the static page /50x.html
	#
	#error_page 500 502 503 504 /50x.html;
	#location = /50x.html {
	#	root /usr/share/nginx/www;
	#}

	# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
	#
	#location ~ \.php$ {
	#	fastcgi_split_path_info ^(.+\.php)(/.+)$;
	#	# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
	#
	#	# With php5-cgi alone:
	#	fastcgi_pass 127.0.0.1:9000;
	#	# With php5-fpm (adjust it to your installed PHP version):
	#	fastcgi_pass unix:/var/run/php5-fpm.sock;
	#	fastcgi_index index.php;
	#	include fastcgi_params;
	#}

	# deny access to .htaccess files, if Apache's document root
	# concurs with nginx's one
	#
	location ~ /\.ht {
		deny all;
	}
}

Full Example for Ubuntu 20.04.1 LTS, PHP8 and nginx 1.18.0:

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or Wordpress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
server {
	listen 80 default_server;
	listen [::]:80 default_server;

	# SSL configuration
	#
	# listen 443 ssl default_server;
	# listen [::]:443 ssl default_server;
	#
	# Note: You should disable gzip for SSL traffic.
	# See: https://bugs.debian.org/773332
	#
	# Read up on ssl_ciphers to ensure a secure configuration.
	# See: https://bugs.debian.org/765782
	#
	# Self signed certs generated by the ssl-cert package
	# Don't use them in a production server!
	#
	# include snippets/snakeoil.conf;

	root /var/www/html;

	# Add index.php to the list if you are using PHP
	index index.html index.htm index.nginx-debian.html;

	server_name _;

	location ~ /data/c3338889c07ac363fc08bc715b22c59c/eqdkp/(backup|timekeeper|config|rss) {
		deny all;
	}
	location ~ /data/c3338889c07ac363fc08bc715b22c59c/(tmp|repository|live_update|cache){
		deny all;
	}
	#For Fresh installs of EQdkp Plus 2.3
	location ~ /data/eqdkp/(backup|timekeeper|config|rss) {
		deny all;
	}
	#For Fresh installs of EQdkp Plus 2.3
	location ~ /data/(tmp|repository|live_update|cache){
		deny all;
	}

	location ~ \.php(/|$) { 
		#fastcgi_split_path_info ^(.+?\.php)(/.*)$;
        	include snippets/fastcgi-php.conf;
        	fastcgi_pass unix:/run/php/php8.0-fpm.sock;
		#fastcgi_index index.php;
		#include fastcgi_params;
    	}

	location / {
		index index.php;
		try_files $uri $uri/ /index.php/$uri?$args;
	}
	# pass PHP scripts to FastCGI server
	#
	#location ~ \.php$ {
	#	include snippets/fastcgi-php.conf;
	#
	#	# With php-fpm (or other unix sockets):
	#	fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
	#	# With php-cgi (or other tcp sockets):
	#	fastcgi_pass 127.0.0.1:9000;
	#}

	# deny access to .htaccess files, if Apache's document root
	# concurs with nginx's one
	#
	#location ~ /\.ht {
	#	deny all;
	#}
}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#	listen 80;
#	listen [::]:80;
#
#	server_name example.com;
#
#	root /var/www/example.com;
#	index index.html;
#
#	location / {
#		try_files $uri $uri/ =404;
#	}
#}

Debugging

You can use the command "nginx -t" to test the syntax of the configuration file. Also, "systemctl status nginx.service" gives you helpful information about the current status of the nginx process.