Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx: Redirect all but one

Tags:

nginx

I have the following http config:

server {
  listen                80;
  server_name           example.com;

  # Necessary for Let's Encrypt Domain Name ownership validation
  location /.well-known/acme-challenge/ {
    root /home/vagrant/.well-known/acme-challenge/;
  }

  return 301 https://$host$request_uri;
}

I would like http://example.com/.well-known/acme-challenge/filename to serve /home/vagrant/.well-known/acme-challenge/filename while every other http request should be redirected to https.

I thought Nginx would process rules in their order, if matches, uses it, otherwise continues. But apparently not. How can I achieve what I want ?

like image 880
Augustin Riedinger Avatar asked Apr 08 '16 16:04

Augustin Riedinger


Video Answer


1 Answers

You should move that redirect to be in the "/" block:

location / {
   return 301 https://$host$request_uri;
}

They do process in order, but yours is outside of any location block, so likely is taking precedence.

like image 89
Joshua DeWald Avatar answered Sep 27 '22 21:09

Joshua DeWald