Multiple flavours of nginx http to https redirects

Redirects in nginx are really powerful, here are some cool and common examples. all domains http > https and remove www Redirect all http traffic which has no other better matching server_name defined from www.domain.tld and domain.tld to https://domain.tld # redirect http://www to https non www # global and maybe not the best in all setups server { listen 80; server_name ~^(www\.)(?<domain>.+)$; return 301 https://$domain$request_uri; } http > https Redirect all of the incoming http requests to https for selected domains

Apache force www subdomain

A redirect in an apache vhost to force the website to www.$domain.tld <VirtualHost *:80> ... RewriteEngine On RewriteCond %{HTTP_HOST} !^www\. [NC] RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=302,L] # test and reload apachectl -t && apachectl -k graceful

Keep nginx from pushing backend port on try and redirects

Ever found yourself wondering why your nginx setup is returning a port from the backend vhost? QUICK FIX: port_in_redirect off; I found some tweaks to prevent this from happening, first a example: # This is the result of a: try $uri $uri/ in a backend vhost stein@stein ~ $ curl --head HTTP/1.1 301 Moved Permanently Server: nginx Content-Type: text/html Content-Length: 178 Connection: keep-alive Location: Accept-Ranges: bytes X-Varnish: 1044342160 Age: 0 Via: 1.