# Configure nginx with certbot for www and non-www

**URL:** <https://community.letsencrypt.org/t/configure-nginx-with-certbot-for-www-and-non-www/52659>\
**Category:** Server\
**Created:** [February 9, 2018, 4:12pm UTC](https://community.letsencrypt.org/t/configure-nginx-with-certbot-for-www-and-non-www/52659 "2018-02-09T16:12:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mattisern](https://sea3.discourse-cdn.com/letsencrypt/user_avatar/community.letsencrypt.org/mattisern/32/20214_2.png) [@mattisern](https://community.letsencrypt.org/u/mattisern)\
**Post date:** [February 9, 2018, 4:12pm UTC](https://community.letsencrypt.org/t/configure-nginx-with-certbot-for-www-and-non-www/52659/1 "2018-02-09T16:12:04Z")

</div>

Hi. I can’t get my nginx server to work with Let’s Encrypt (Certbot) for both www and non-www. This is my current sites-enabled:

server {  
root …  
index …  
server\_name [www.domain.com](http://www.domain.com)  
listen 443 ssl;  
ssl\_certificate …  
ssl\_certificate\_key …  
include …  
ssl\_dhparam …  
}

server {  
if ($host = [www.domain.com](http://www.domain.com) {  
return 301 https://$host$request\_uri;  
}

```
listen 80;
server_name domain.com www.domain.com;
return 301 https://www.domain.com$request_uri;

```

}

```
server {
listen 443;
server_name www.domain.com
return 301 https://www.domain.com$request_uri;

```

}

Now, what happens is that it works with www, but not without. Where am I fucking this up?

---

<div class="post-metadata">

**Author:** ![sahsanu](https://sea3.discourse-cdn.com/letsencrypt/user_avatar/community.letsencrypt.org/sahsanu/32/89984_2.png) [@sahsanu](https://community.letsencrypt.org/u/sahsanu)\
**Post date:** [February 9, 2018, 4:29pm UTC](https://community.letsencrypt.org/t/configure-nginx-with-certbot-for-www-and-non-www/52659/2 "2018-02-09T16:29:46Z")

</div>

Hi @mattisern,

What you want to do?. Redirect http domain & www.domain & https domain to https www.domain?

Cheers,  
sahsanu

---

<div class="post-metadata">

**Author:** ![sahsanu](https://sea3.discourse-cdn.com/letsencrypt/user_avatar/community.letsencrypt.org/sahsanu/32/89984_2.png) [@sahsanu](https://community.letsencrypt.org/u/sahsanu)\
**Post date:** [February 9, 2018, 5:05pm UTC](https://community.letsencrypt.org/t/configure-nginx-with-certbot-for-www-and-non-www/52659/3 "2018-02-09T17:05:44Z")

</div>

Well, just in case, this is the skeleton to redirect http domain & www.domain & https domain to https www.domain.

```
server {
    listen 80;
    server_name domain.tld www.domain.tld;
    return 301 https://www.domain.tld$request_uri;
}

server {
    listen 443 ssl;
    server_name domain.tld;
    ssl_certificate /etc/letsencrypt/live/domain.tld/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/domain.tld/privkey.pem;
    #other ssl options
    return 301 https://www.domain.tld$request_uri;
}

server {
    listen 443 ssl;
    server_name www.domain.tld;
    #your root, index, etc. stuff
    ssl_certificate /etc/letsencrypt/live/domain.tld/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/domain.tld/privkey.pem;
    #other ssl options
}

```

Note: in the example, `domain.tld` & `www.domain.tld` are both covered by the same certificate which is located in `/etc/letsencrypt/live/domain.tld/`, keep that in mind because if you have one certificate covering only `domain.tld` and another one covering `www.domain.tld` then the paths should be replaced in the example with the right ones.

Hope this helps.

Cheers,  
sahsanu

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/letsencrypt/original/3X/c/a/ca6c06ea1ea201324bba7048c6841ce60236468d.png) [@system](https://community.letsencrypt.org/u/system)\
**Post date:** [March 11, 2018, 5:06pm UTC](https://community.letsencrypt.org/t/configure-nginx-with-certbot-for-www-and-non-www/52659/4 "2018-03-11T17:06:32Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
