大网数据广告活动
服务器运维问答

nginx redirect or return single location

· 52CE 我爱测

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…

问题原文

原帖链接 · jwir3 · CC BY-SA 4.0

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.

已采纳解答

原帖链接 · Michael Hampton · CC BY-SA 4.0

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;
    }

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2021-08-05)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。