brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: nginx return 301 beats rewrite for plain redirects

Nginx
~/tips/tip-nginx-return-301-over-rewrite

return 301 https://example.com$request_uri;

Sends a permanent redirect that keeps the path and query string, without a regex.

I use `return` whenever the whole job is sending visitors somewhere else, since no regex is involved.

server { listen 80; server_name example.com www.example.com;
    return 301 https://example.com$request_uri;
}

return stops processing at once and sends the response, which is faster and easier to read than rewrite ^ ... permanent. $request_uri carries the original path and query string along. Browsers cache a 301 very hard, so start with 302 while testing and switch when it is right. A second server block for the https site then handles the real content. curl -I http://example.com/x?y=1 shows the Location header you will get.

Further reading: ngx_http_rewrite_module: return

Going deeper: Run degoog on Unraid as your own search page, compared with SearXNG and 4get

More Nginx tips sudo certbot --nginx -d example.com -d www.example.com /data/nginx/custom/server_redirect.conf map $http_upgrade $connection_upgrade { proxy_intercept_errors on; proxy_redirect http://localhost:3000/ /app/; All Nginx tips (50) All topics

Link