Skip to content

Forward all HTTP to HTTPS

Paul Spencer edited this page Oct 13, 2021 · 2 revisions

Use the following to redirect all HTTP traffic for all VIRTUAL_HOST to HTTPS:

RUN mkdir /etc/nginx/vhost.d
RUN { \
      echo "if (\$proxy_x_forwarded_proto = 'http') {"; \
      echo "  return 301 https://\$host\$request_uri;"; \
      echo '}'; \
    } > /etc/nginx/vhost.d/default_location

Note: You may have heard that if is evil due to performance concerns, but if you read that page by the authors of Nginx, you will find that using a return like the above is a perfectly acceptable usage.