I’ve read in Nginx docummentation that instead of using IFs, new server blocks should be made.
I have tried making another block listening to 443 ssl, just to set there redvi.eu (non www) and then redirect it accordingly. It fails because of duplication.
How is the correct way to redirect https non-www here?
server {
listen 80 default_server;
server_name www.redvi.eu redvi.eu;
return 301 https://www.redvi.eu$request_uri;
}
server {
server_name www.redvi.eu;
root /home/deploy/redvi/current/public;
passenger_enabled on;
passenger_app_env production;
location /cable {
passenger_app_group_name redvi_websocket;
passenger_force_max_concurrent_requests_per_process 0;
}
# Allow uploads up to 100MB in size
client_max_body_size 100m;
location ~ ^/(assets|packs) {
expires max;
gzip_static on;
}
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/www.redvi.eu/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/www.redvi.eu/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
2
Answers
Even if you don’t want to use "IF", there’s not much you can do.
I’ve been using it this way for many years and it works well.
I think I have to comment the difference between the accepted answer and my suggestion. Of course you can use two server blocks with the additional string comparsion operation during the
NGX_HTTP_SERVER_REWRITE
request processing phase. Moreover, you can even use a single server block like the following one:Nevertheless my advice to use three server blocks was given intentionally because of the performance considerations. That’s because internally nginx will build a hash table for each subdomain level to select the proper server block via hash table(s) lookup with a complexity not bigger than
O(x)
wherex
is a maximum number of subdomain components for a longest hosted server name (e.g.3
for thewww.example.com
). This is the same algorithm that is used to evaluate amap
block with thehostnames
keyword specified, and explanations about its performance was given by nginx lead developer Igor Sysoev here. Configuring nginx, especially on a high loaded system, you should not try to shorten your config in cost of performance. Nginx configuration is (mostly) declarative, it isn’t a script or program code, nginx isn’t an optimizing compiler to process your configuration most optimal way. The DRY (don’t repeat yourself) principles aren’t applicable here even if you got such a temptation.In some examples you can even see something like:
This one is the worst in the performance terms because of an expensive PCRE library call being invoked (actually even two calls when a
rewrite
directive used in the above example instead of thereturn 301 https://www.$http_host$request_uri;
).