Laravel • Varnish • Performance
by Dimitri König Reading time: ~2 minutes

Proper redirects handling

Handle as many redirects as possible outside of your application

Wether you have a -> redirect, or a -> redirect, you should do this outside of your application.

I see many bots that access all different variants, with an aggressive rate, which could further affect your server load and result in high and long peaks of traffic/load. Hundrets of requests for can either hit your application or be already handled by our server app.

Fortunately many websites already implement this properly, but not all. We all can do better.

If you want to redirect all http -> https traffic, you can implement this piece of nginx config:

1server {
2 listen 80 default_server;
3 server_name _;
4 return 301 https://$host$request_uri;

If you only have one website or a few on your server, you can go with this simple config:

1server {
2 listen 80;
3 listen 443 ssl;
4 server_name;
5 return 301$request_uri;

If you have a catch all server config where the server is assigned somewhere else like a gateway/reverse proxy, and you don't want to create many different server blocks, you could do it this way:

1server {
2 ...
3 if ($host ~ 'alternative-domain' ) {
4 rewrite ^/(.*)$$1 permanent;
5 }
7 if ($host ~ 'another-alternative-domain' ) {
8 rewrite ^/(.*)$$1 permanent;
9 }
10 ...

Just make sure that you try to catch as many redirects as possible, which are not dynamic, and you should be fine. Just another small step in reducing traffic and load on your application.

Feel free to comment on Twitter: