Posts for Tag: nginX

Avoiding SSL redirect loop

If you are configuring an nginx website to use SSL and - by any chance - you need to setup more than one “server” entry (for instance to make www redirect to the plain URL), keep in mind that you need to declare the ssl certificate mumbo-jumbo in both entries, otherwise browsers will enter a redirect loop.

Something like the following will do:

server {
        listen 443 ssl;
        server_name www.zenblast.com;

        include zenblast-ssl.conf;

        return 301 $scheme://zenblast.com$request_uri;
}

server {
        server_name zenblast.com;
        listen              443 ssl;

        include zenblast-ssl.conf;

        ...
}

zenblast-ssl.conf is where we define stuff like ssl_certificate, ssl_certificate_key, ssl on, ssl_ciphers, ssl_prefer_server_ciphers on, and ssl_protocols. Notice that the file is included in both entries.

Hope this helps save someone 30 minutes of Googling :)

Nginx rewrite/redirect for blog URL

When i moved my blog to a [middleman](http://middlemanapp.com) system, i found that i needed to provide redirects from `/2012/01/25/some-post` to `/blog/2012/01/25/some-post`. It's a relatively simple task, though i lost some time because i missed a detail that no one usually explains.

There are articles on the web explaining how to do url rewriting in Nginx. Most of them will tell you something like this:

location /2009/ { rewrite ^(/2009/.*)$ /blog$1 break; }

In a gist, that's what you need. What they forget to mention is it will not work unless you provide the *root* in that *location* block.

Another thing you might want to do, to track eventual problems with your regular expression skills (hehe), is activate rewrite logging. It's also a good idea to inform clients that the resource moved permanently (301).

The full solution ended up being:

server { listen 80; server_name pedroassuncao.com *.pedroassuncao.com;

access_log /path/to/your/logs/nginx.access.log; error_log /path/to/your/logs/nginx.error.log;

rewrite_log on;

location /2009/ { root /path/to/your/html/; rewrite ^(/2009/.*)$ /blog$1 break; return 301; }

location / { root /path/to/your/html/; expires 24h; }

error_page 404 /404; }

A final note: When you activate `rewrite_log on;` nginx will dump all rewrite information to your *error* log as a notice. Since notice is the default it will show up. You can always make sure, by adding *notice* after the log file:

error_log /path/to/your/logs/nginx.error.log notice;

Happy nginxing :)