Nginx on nixos: Redirect www to non-www?

I have an entry like this:

    virtualHosts."" = {
      enableACME = true;
      forceSSL = true;
      locations."/" = {
        proxyPass = "";
        extraConfig = ''
          proxy_pass_request_headers on;
          proxy_set_header Cookie $http_cookie;
          proxy_set_header Host $host;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header X-Forwarded-Proto $scheme;
          proxy_set_header X-Real-IP $remote_addr;

And I’ve setup an A record for the subdomain. How can I create a redirect from to in nginx? I’ve tried to read, but I don’t understand enough of it.

You can create virtual host for and use the globalRedirect option. Do not forget to also set up the TLS certificate.


Thanks! I added this:

    virtualHosts."" = {
      enableACME = true;
      forceSSL = true;
      globalRedirect = "";

And that worked :slight_smile:

1 Like

For bonus points, you can also replace enableACME = true with useACMEHost = ""; and add

security.acme.certs."".extraDomainNames = [

To reduce the number of certificates that need to be generated.