In previous part of nginx vhost configuration, we've configured nginx to serve Drupal site directly (i.e. "http://example.com"). In this part we're going to configure nginx to serve Drupal as a sub-directory. For e.g. "http://localhost/drupal" or "http://example.com/subsite".
We're going to use similar configuration as part 1 with minor tweak.
NOTE :
- Change "SERVER_NAME" to actual domain name you want to use. For e.g. example.com
- Change "PHP_FPM_VERSION" to PHP FPM version installed on server. I've php 7.2 installed with fpm on my machine, so I'll be using php7.2-fpm as PHP-FPM version.
For e.g. fastcgi_pass unix:/run/php/php7.2-fpm.sock - Change "SSL_CERTIFICATE_NAME" to actual SSL Certificate file name and its location. For e.g. example.com.pem and example.com.key
# VHOST Config.
server {
server_name SERVER_NAME;
root /var/www/html;
listen 80;
# Add headers.
add_header Strict-Transport-Security 'max-age=31536000;';
add_header X-XSS-Protection "1; mode=block" always;
# Handle error pages;
location = /favicon.ico {
log_not_found off;
access_log off;
}
# Handle robots.txt separately
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
# Prevent access to files directly.
location ~ .*\.config$ {
return 403;
}
location ~ \..*/.*\.php$ {
return 403;
}
location ~ \..*/.*\.yml$ {
return 403;
}
location ~ /composer.*$ {
return 403;
}
# Prevent any hidden files (.htaccess, .gitignore)
location ~ (^|/)\. {
return 403;
}
location ~ ^/sites/.*/private/ {
return 403;
}
location / {
try_files $uri $uri/ =404;
}
# Re-write URLs to start with index.php
location @subsite {
try_files $uri /SUB_DIRECTORY/index.php?$query_string;
}
location /SUB_DIRECTORY/ {
try_files $uri $uri/ @subsite;
}
location ~ '\.php$|^/update.php' {
# Allow sub-pages after update.php
fastcgi_split_path_info ^(.+?\.php)(|/.*)$;
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include fastcgi_params;
include snippets/fastcgi-php.conf;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_intercept_errors on;
fastcgi_pass unix:/run/php/PHP_FPM_VERSION.sock;
}
# Set expiration for media, CSS and JS files.
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
proxy_cache_bypass 1;
}
}
Above vhost configuration works in Ubuntu, but for this to get working in CentOS, we've to update vhost configuration for "update.php" as below :
server {
server_name SERVER_NAME;
root /var/www/html;
listen 80;
# Add headers.
add_header Strict-Transport-Security 'max-age=31536000;';
add_header X-XSS-Protection "1; mode=block" always;
# Handle error pages;
location = /favicon.ico {
log_not_found off;
access_log off;
}
# Handle robots.txt separately
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
# Prevent access to files directly.
location ~ .*\.config$ {
return 403;
}
location ~ \..*/.*\.php$ {
return 403;
}
location ~ \..*/.*\.yml$ {
return 403;
}
location ~ /composer.*$ {
return 403;
}
# Prevent any hidden files (.htaccess, .gitignore)
location ~ (^|/)\. {
return 403;
}
location ~ ^/sites/.*/private/ {
return 403;
}
location / {
try_files $uri $uri/ =404;
}
# Re-write URLs to start with index.php
location @subsite {
try_files $uri /SUB_DIRECTORY/index.php?$query_string;
}
location /SUB_DIRECTORY/ {
try_files $uri $uri/ @subsite;
}
location ~ '\.php$|^/update.php' {
# Allow sub-pages after update.php
fastcgi_split_path_info ^(.+?\.php)(|/.*)$;
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include fastcgi_params;
include snippets/fastcgi-php.conf;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_intercept_errors on;
fastcgi_pass unix:/run/php/PHP_FPM_VERSION.sock;
}
# Set expiration for media, CSS and JS files.
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
proxy_cache_bypass 1;
}
}
# For CentOS
location ~ '\.php$|^/update.php' {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
# Allow sub-pages after update.php
fastcgi_split_path_info ^(.+?\.php)(|/.*)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~ '\.php$|^/update.php' {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
# Allow sub-pages after update.php
fastcgi_split_path_info ^(.+?\.php)(|/.*)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}