Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Actually you don't even need a rewrite.</p> <pre><code>server { #listen 80 is default server_name www.example.com; return 301 $scheme://example.com$request_uri; } server { #listen 80 is default server_name example.com; ## here goes the rest of your conf... } </code></pre> <p>As my answer is getting more and more up votes but the above as well. You should never use a <code>rewrite</code> in this context. Why? Because nginx has to process and start a search. If you use <code>return</code> (which should be available in any nginx version) it directly stops execution. This is preferred in any context.</p> <p>Redirect both, non-SSL and SSL to their non-www counterpart:</p> <pre><code>server { listen 80; listen 443 ssl; server_name www.example.com; ssl_certificate path/to/cert; ssl_certificate_key path/to/key; return 301 $scheme://example.com$request_uri; } server { listen 80; listen 443 ssl; server_name example.com; ssl_certificate path/to/cert; ssl_certificate_key path/to/key; # rest goes here... } </code></pre> <p>The <code>$scheme</code> variable will only contain <code>http</code> if your server is only listening on port 80 (default) and the listen option does not contain the <code>ssl</code> keyword. Not using the variable will not gain you any performance.</p> <p>Note that you need even more server blocks if you use HSTS, because the HSTS headers should not be sent over non-encrypted connections. Hence, you need unencrypted server blocks with redirects and encrypted server blocks with redirects and HSTS headers.</p> <p>Redirect everything to SSL (personal config on UNIX with IPv4, IPv6, SPDY, ...):</p> <pre><code># # Redirect all www to non-www # server { server_name www.example.com; ssl_certificate ssl/example.com/crt; ssl_certificate_key ssl/example.com/key; listen *:80; listen *:443 ssl spdy; listen [::]:80 ipv6only=on; listen [::]:443 ssl spdy ipv6only=on; return 301 https://example.com$request_uri; } # # Redirect all non-encrypted to encrypted # server { server_name example.com; listen *:80; listen [::]:80; return 301 https://example.com$request_uri; } # # There we go! # server { server_name example.com; ssl_certificate ssl/example.com/crt; ssl_certificate_key ssl/example.com/key; listen *:443 ssl spdy; listen [::]:443 ssl spdy; # rest goes here... } </code></pre> <p>I guess you can imagine other compounds with this pattern now by yourself.</p> <p>More of my configs? Go <a href="https://github.com/MovLib/www/tree/master/conf/nginx">here</a> and <a href="https://github.com/Fleshgrinder/nginx-configuration">here</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload