NGINX – Redirect All HTTPS (SSL)

One method I like to do is catch-all port 80 (http) requests and redirect them to port 443 (https).

This works when you know you want all configured sites to definitely only use https.

server {
    listen 80 default_server;

    server_name _;

    return 301 https://$host$request_uri;

This listens on port 80. It’s also the port 80 default server. It’s assumed that it’s the only server block listening on port 80.

The server_name is _, which matches any hostname used.

Finally, we return a 301 redirect to the https version of whatever URI was requested. We know all requests reaching this server block is http, because it only listens for port 80 requests.

Our sites/apps can then be set to listen on port 443 for SSL connections only.

server {
    listen 443 ssl;

Redirect Specific Sites

We can redirect only specific sites also. This is better if you have multiple apps/sites and not all of them should be forced to use SSL certificates.

server {
    listen 80;

    return 301$request_uri;

Here we listen on port 80 for http requests, but we don’t make it the default server. Instead, it is only listening for requests made to the hostname (where the Host HTTP header is set to

It returns a 301 redirect to the https version of the URI given, but hard-codes the hostname (

Was this helpful?

0 / 0