Home > Mobile >  nginx if host true with location and root
nginx if host true with location and root

Time:02-23

I use docker with nginx and this is my app config file:

server {
  listen 80;
  server_name app.me.site;
  return 308 https://$host$uri;
  location .well-known {
    root /var/www/.well-known;
    try_files /$uri /$uri/ /index.html;
  }
  # other configs
}

The path is /var/www/app.

I also created /var/www/.well-known for Let's Encrypt and it is accessible but it's only accessible for https.

I need to have an if cluse: if URL is app.me.site/.well-known, do not use https.

I tried to reach this but did not find any clue.

CodePudding user response:

Your config is not workable because the return directive is executed at the NGX_HTTP_SERVER_REWRITE_PHASE while proper location selection will be done later at the NGX_HTTP_FIND_CONFIG_PHASE (request processing phases are described in the development guide). To fix it you should move that return directive to the location block (I also suggest to use the $request_uri variable instead the normalized $uri one):

location / {
    # everything not started with the '/.well-known/' prefix will be processed here
    return 308 https://$host$request_uri;
}
location /.well-known/ {
    # do not append '/.well-known' suffix here!
    # (see the difference between 'root' and 'alias' directives)
    root /var/www;
    try_files $uri =404;
}
  • Related