Sign up ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I have multiple hostfiles. I also have a basic: "default" hostfile (with phpmyadmin locations etc)

In this file I would like to force a redirect to https for all other hostfiles. I'm not sure how to do this in the example below.

server {
    listen      80;
    server_name; <-- this should be a wildcard
    rewrite     ^   https://$server_name$request_uri? permanent;
share|improve this question

1 Answer 1

up vote 1 down vote accepted




 server {
        listen       80;
        if ($host !~* ^www\.) {
                rewrite ^(.*)$ http://www.$host$1 permanent;
        client_max_body_size   10M;
        client_body_buffer_size   128k;

        root       /home/test/test/public;
        passenger_enabled on;
        rails_env production;

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
                root   html;

and you can use it for multiply sites.

more info this and this

share|improve this answer
Fantastic, I didn't know that the if statement would work on Nginx :-) – Bob van Luijt Jun 11 '13 at 14:53

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.