Drupal 8 as a Static Site: Web Server Configuration

Submitted by nigel on Saturday 3rd November 2018

For testing your statically site, and perhaps for production too if you are self-hosting, you will need a configuration for either nginx or Apache. These are the configurations that have worked for me.  

nginx
# Default server configuration
#
server {
        listen 80 default_server;   # Replace with 443 if using SSL
        listen [::]:80 default_server;  # Replace with 443 if using SSL
 
        # SSL configuration - uncomment this section if you are using https
        #ssl_certificate         {{ path_to_your_certificate }};
        #ssl_certificate_key     {{ path_to_your_certificate_key }};
        #ssl_protocols           TLSv1 TLSv1.1 TLSv1.2;
        #ssl_ciphers             HIGH:!aNULL:!MD5;
 
        root {{ path_to_your_docroot }};
 
        index index.html;
 
        server_name {{ your_server_name }};
 
        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }
 
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        location ~ /\.ht {
               deny all;
        }
}
Apache
<VirtualHost *:80>
# change this to 443 is you are using ssl
 
  ServerName {{ your_server_name }}
  DocumentRoot "{{ path_to_your_docroot }}"
  DirectoryIndex index.html
 
  # Uncomment out the block below for https
  #SSLEngine on
  #SSLCertificateFile "{{ path_to_your_certificate }}"
  #SSLCertificateKeyFile "{{ path_to_your_certificate_key }}"
 
  <Directory "{{ path_to_your_docroot }}">
      AllowOverride All
  </Directory>
 
  # Logs
  Errorlog ${APACHE_LOG_DIR}/{{ your site }}.error.log
  CustomLog ${APACHE_LOG_DIR}/{{ your site }}.access.log combined
 
</VirtualHost>