Score:1

nginx redirect or return single location

cn flag

I am trying to get videos.mydomain.com/kickboxing to redirect to a YouTube link, as well as videos.mydomain.com/classes to redirect to a different YouTube link. (These are both unlisted playlists, and I want anyone who has access to our website to have this redirect happen automatically for them. This is step 1 of several steps of configuring this and I can't seem to get it to work).

Here's my configuration:

server {
  server_name videos.mydomain.com;
  root /var/www/www.mydomain.com;

  listen 80;
  access_log /var/log/nginx/videos.mydomain.com-access.log;
  error_log /var/log/nginx/videos.mydomain.com-error.log debug;

  location /classes {
    return 301 https://youtube.com/playlist?list=classes-playlist;
  }

  location /kickboxing {
    return 301 https://youtube.com/playlist?list=kickboxing-playlist;
  }

  return 404;
}

I've set this up in my sites-available, as well as enabled it (and of course, set up DNS). Unfortunately, when I navigate to the urls http://videos.mydomain.com/kickboxing or http://videos.mydomain.com/classes, all I get is the 404 url. If I change the return 404 line to one of the return 301 lines, then it will redirect appropriately, but only for that particular url. It seems to not be correctly identifying the location sections.

Score:2
cz flag

This happens because you put return 404; in the server block.

Rewrites at the server level are processed before rewrites at the location level.

To fix the problem, put it in a location. (As a general rule you should always process requests in a location.)

    location / {
        return 404;
    }
jwir3 avatar
cn flag
Bingo! That was the problem. Thank you so much. I also appreciate the detailed explanation of _why_ this happened. It helps me learn.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.