I have a server file like this
server {
listen 80;
server_name subdomain.example.com;
return 301 https://$server_name$request_uri;
location /.well-known/acme-challenge {
root /var/www/letsencrypt;
}
}
Now when I try sudo letsencrypt renew
. It throws up and error saying can't find .well-known/acme-challenge
. But as soon as I commented the return 301
line restarted the server and It worked.
Now I want to retest it putting the location first and not commenting the return 301 statement but it says certificate not due for renewal
.So the question is does order in which the file is read, does it matter?
and it won't automatically renew because of this reason for me, those who do renewal how do you handle this situation?
In this case, it isn't so much the ordering (a good explanation of how location and regex is evaluated can be found here: https://www.digitalocean.com/community/tutorials/understanding-nginx-server-and-location-block-selection-algorithms).
For things like location blocks, the short version is best match wins, rather than the first match.
In your case, however, order counts because you use
return
. Per https://nginx.org/en/docs/http/ngx_http_rewrite_module.html#return:The key here is that a
return
immediately stops processing/evaluation, so what is happening is that nginx isn't looking at anything belowreturn
.So you just need to move that
return
clause below your location block.As for testing, I would try adding
--test-cert
to your commandline (see https://certbot.eff.org/docs/using.html#certbot-command-line-options).That should avoid the 'problem' you are having when trying to use their production server, which reports you have a valid cert and do not need a new one right now.
You should include your
return
directive in alocation
block, then the normallocation
block matching rules are used:answering for the idea of line orders in nginx config files
Yes, it does and totally depends on different directives specified within the different context supported by Nginx. In layman's term, nginx keeps a stack of things to do and applies certain algorithms respectively with
best match
in mind.Nginx uses
selection algorithm
to make decisions in theserver
context; primarily based on two directives viz.listen
andserver_name
.Multiple location contexts can be defined, each location is used to handle a certain type of client request, and each location is selected by virtue of matching the location definition against the client request through a
selection algorithm
.The
upstream
context usesround-robin
by default to determine which specific server to hand the request to.