Customise your NGINX configuration

With our Service Storefront as a Service offering, you can also customise the NGINX configuration for your specific needs. You can find the NGINX configuration here:

config/nginx.erb

This file contains a template, that also supports the usage of environment variables. By default the following values are available:

Name Vaule (Example)
MAKAIRA_API_INSTANCE stage
MAKAIRA_API_URL https://shop.makaira.io
MAKAIRA_ASSET_URL https://storefront-preview.s3.eu-central-1.amazonaws.com
NODE_ENV production
PRODUCTS_PER_PAGE 50
RUNS_ON_HEROKU true
SHOP_DOMAIN https://shop.makaira.cloud
SHOP_ID 1

In order to use an environment variable inside the template, you'll need to use the following syntax:

listen <%= ENV["PORT"] %>;

Additionally you can also use all common features of NGINX. You can find the documentation of NGINX directly on their website: NGINX Documentation

 

Basic Auth

Your new Storefront is protected by a login. You can find all authorised users in the config/.htpasswd file. If you want to add a new user to your list, you can do so by running:

htpasswd -B config/.htpasswd USER_NAME

If you have htpasswd not installed, please follow one of these guides: Install htpasswd on Ubuntu/DebianUse htpasswd on Windows or Use htpasswd on macOS

 

Configuration Examples

Pre Go Live Routing

The following configuration is useful if you want to forward the whole traffic to your shop system. You can do that before you go live with your new storefront and have already switched your DNS record to the new storefront.

http {
  ...

  location / {
    # Redirect all traffic to HTTPS
    if ($http_x_forwarded_proto != "https") {
      return 301 https://$host$request_uri;
    }

    proxy_pass            http://example.com; # Please put the url of your shop system here
    proxy_read_timeout    120s;
    proxy_connect_timeout 15s;
    proxy_send_timeout    90s;
    proxy_set_header      Host "SHOP.makria.io"; # Please put the address of this storefront here
    proxy_set_header      X-Real-IP $http_x_real_ip;
    proxy_set_header      X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header      X-Forwarded-Proto $http_x_forwarded_proto;
    proxy_set_header      HTTPS $http_https;
  }
}
Failover

Use this configuration, if you want to forward requests to shop system, that cannot be served by storefront and are throwing an error.

http {
  ...
  # Enable this part if you want to use error page failover
  proxy_busy_buffers_size   512k;
  proxy_buffers   			4 512k;
  proxy_buffer_size   		256k;
  proxy_intercept_errors 	on;
  recursive_error_pages 	on;

  location / {
    # Redirect all traffic to HTTPS
    if ($http_x_forwarded_proto != "https") {
      return 301 https://$host$request_uri;
    }

    # Enable password protection
    auth_basic           "Access Restricted";
    auth_basic_user_file /app/config/.htpasswd;

    # Enable error_page if you want to use error page failover
    error_page 404 500 501 502 503 504 505 506 507 508 510 511 = @failover_target;

    proxy_set_header      Host $http_host;
    proxy_set_header      X-Real-IP $http_x_real_ip;
    proxy_set_header      X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header      X-Forwarded-Proto $http_x_forwarded_proto;
    proxy_set_header      HTTPS $http_https;
    proxy_set_header      Accept-Encoding "gzip,deflate";
    proxy_set_header      Connection "";
    proxy_set_header      Proxy "";
    proxy_set_header      X-Country-Code $http_x_country_code;

    proxy_redirect 			off;
    proxy_pass 				http://app_server;
  }

  # Failover target
  location @failover_target {
    proxy_pass            http://example.com; # Please put the url of your shop system here
    proxy_read_timeout    120s;
    proxy_connect_timeout 15s;
    proxy_send_timeout    90s;
    proxy_set_header      Host "SHOP.makria.io"; # Please put the address of this storefront here
    proxy_set_header      X-Real-IP $http_x_real_ip;
    proxy_set_header      X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header      X-Forwarded-Proto $http_x_forwarded_proto;
    proxy_set_header      HTTPS $http_https;
  }
}
Forward specific routes to your shop system

If you want to forward the whole traffic for specific routes to your shop system directly, you can use the following configuration. For the configuration of the route you can use regular expressions. For more information about configuring locations in NGINX, please go and visit this blog post by DigitalOcean: Understanding Nginx Server and Location Block Selection Algorithms

http {
  ...
  location / {
    ...
  }

  # Custom location with direct failover
  location ~* ^(/api/) {
    try_files /dev/null @failover_target;
  }

  # Failover target
  location @failover_target {
    proxy_pass            http://example.com; # Please put the url of your shop system here
    proxy_read_timeout    120s;
    proxy_connect_timeout 15s;
    proxy_send_timeout    90s;
    proxy_set_header      Host "SHOP.makria.io"; # Please put the address of this storefront here
    proxy_set_header      X-Real-IP $http_x_real_ip;
    proxy_set_header      X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header      X-Forwarded-Proto $http_x_forwarded_proto;
    proxy_set_header      HTTPS $http_https;
  }
}