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

Nginx map subdomain to subfolder

In this tutorial I’ll show you how to direct your incoming web traffic dynamically to folders on your server. This is really useful for a lot of setups I use this one a lot for development environments because you can give developers rights to create new folders and it automatically has its own sub domain. Example: DNS endpoint Folder on disk /var/www/ test.

Use a deafult cert for nginx

If you use a service like cloudflare or sucuri you don’t need a valid cert on the backend servers to do full ssl to backend. If you want full ssl to the server you need 2 things self signed cert nginx proxy to send traffic to port 80 Using this method you need your application to send out redirects to ssl version of pages. Self signed crt You can gererate one with: selfsigned.

Map client header to upstream in nginx

Sometimes you want to route traffic to different endpoints based on a http header. Avoiding if statements in the nginx config is the most important part here. The example below is based on the user-agent header that is send from the client. Test nginx conf to view traffic flow # create upstreams # android and other non ios device upstream android { server; } # ios based devices upstream ios { server 127.

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.