Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

This is the configuration of a vhost.

I have to redirect ALL the HTTP request to HTTPS and all the website.it to www.website.it

Can I optimize something?

server {

        listen          80;

        server_name     website.it www.website.it;


        if ($host = 'website.it' ) {
                rewrite  ^(.*)$ https://www.website.it$1 permanent;
        }

        rewrite ^(.*)$ https://$host$1 permanent;

}

server {

        listen          443;

        server_name     website.it www.website.it;


        ssl on;

        ssl_certificate      /etc/ssl/nginx/www.website.it/www.website.it.crt;

        ssl_certificate_key  /etc/ssl/nginx/www.website.it/www.website.it.key;


        root            /home/nginx/vhosts/website.it/web;

        access_log      /home/nginx/vhosts/website.it/log/access.log;

        error_log       /home/nginx/vhosts/website.it/log/error.log;



        if ($host = 'website.it' ) {
                rewrite  ^(.*)$ https://www.website.it$1 permanent;
        }


        location / {

                index   index.php;

                try_files $uri $uri/ /index.php;

        }


        location ~ \.php$ {

                fastcgi_pass   127.0.0.1:9003;

                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

                include        /etc/nginx/fastcgi_params;

        }

}

Thank you!

share|improve this question

migrated from stackoverflow.com May 26 '11 at 0:35

This question came from our site for professional and enthusiast programmers.

2 Answers 2

I think you can use this:

server {
    listen 80;
    server_name website.it www.website.it;
    rewrite ^ https://www.website.it$request_uri? permanent;
}

I pretty much do exactly this on https://self-evident.org/, but I forget where I got the recipe from.

[updated... twice]

For the SSL part, I would break it into two stanzas:

server {
    listen 443;
    server_name website.it;

    ssl on;
    ssl_certificate      /etc/ssl/nginx/www.website.it/www.website.it.crt;
    ssl_certificate_key  /etc/ssl/nginx/www.website.it/www.website.it.key;

    rewrite ^ https://www.website.it$request_uri? permanent;
}

server {
    listen 443;
    server_name www.website.it;
    [etc.]
}

I suspect that nginx is well-optimized for choosing a "server" stanza based on the server_name. The interpreted "if" clause is almost certainly slower.

share|improve this answer
    
ok but I also have to use SSL... in this example I only see the standard http port. –  Anonymous May 25 '11 at 22:36
    
Yeah, I should have read the question more carefully :-). I have updated my answer –  Anonymous May 25 '11 at 23:08
    
@Nemo I tried but this URL https:// website.it responses with an error "Errore 107 (net::ERR_SSL_PROTOCOL_ERROR): Errore protocollo SSL."......... so i think i can't do: rewrite ^ website.it$request_uri? permanent; i have to enable SSL there..... no? –  Anonymous May 25 '11 at 23:45
    
Whoops! Yeah, any time you "listen 443" you also need to include the SSL configuration, of course. I have updated my answer... again –  Nemo May 26 '11 at 1:07

you can delete this line, as the one below is the more general statement that includes this case:

# delete this:
if ($host = 'website.it' ) {
        rewrite  ^(.*)$ https://www.website.it$1 permanent;
}
share|improve this answer
    
where? from the first server { ... } or the second ? –  Anonymous May 25 '11 at 22:38

Your Answer

 
discard

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