nginx重定向问题正确配置但返回404

问题描述 投票:0回答:1

所以我开始在我的第一个项目中使用 Nginx,但我在重定向我的配置文件时遇到问题,它看起来不错,但每当我尝试“curl IP_address/redirect_me”时,404 都会显示任何帮助 我确实检查了 error.log 和 nginx -t 但还没有解决方案,并且“curl IP_address work”完美。 (并不是说我还在学习网络开发)

这是我的默认配置文件

ubuntu@381927-web-01:~$ sudo cat  /etc/nginx/sites-available/default
server {
    listen 80 default_server;
    listen [::]:80 default_server;
    
    location / {
        return 301 https://www.youtube.com/watch?v=QH2-TGUlwu4;
    }
    # SSL configuration
    #
    # listen 443 ssl default_server;
    # listen [::]:443 ssl default_server;
    #
    # Note: You should disable gzip for SSL traffic.
    # See: https://bugs.debian.org/773332
    #
    # Read up on ssl_ciphers to ensure a secure configuration.
    # See: https://bugs.debian.org/765782
    #
    # Self signed certs generated by the ssl-cert package
    # Don't use them in a production server!
    #
    # include snippets/snakeoil.conf;

    root /var/www/html;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;

    server_name _;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }

    # pass PHP scripts to FastCGI server
    #
    #location ~ \.php$ {
    #   include snippets/fastcgi-php.conf;
    #
    #   # With php-fpm (or other unix sockets):
    #   fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
    #   # With php-cgi (or other tcp sockets):
    #   fastcgi_pass 127.0.0.1:9000;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #   deny all;
    #}
}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#   listen 80;
#   listen [::]:80;
#
#   server_name example.com;
#
#   root /var/www/example.com;
#   index index.html;
#
#   location / {
#       try_files $uri $uri/ =404;
#   }
#}
ubuntu@381927-web-01:~$ 


这是错误:

ubuntu@381927-web-01:~$ curl http://35.153.18.176/redirect_me
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.18.0 (Ubuntu)</center>
</body>
</html>
ubuntu@3819
web-services nginx server devops nginx-config
1个回答
0
投票

您的 Nginx 配置没有定义处理路径的规则

/redirect_me
。您当前的配置有一个
location /
块,它尝试从文档根目录提供文件,如果找不到,则返回 404 错误。

要实现访问

/redirect_me
时的重定向,您需要在 Nginx 配置中为其添加特定的
location
块,如下所示:

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;
    index index.html index.htm index.nginx-debian.html;
    server_name _;

    # Redirect /redirect_me to YouTube
    location /redirect_me {
        return 301 https://www.youtube.com/watch?v=QH2-TGUlwu4;
    }

    location / {
        # Existing configuration for serving files
        try_files $uri $uri/ =404;
    }

    # The rest of your existing server block configuration...
}

进行这些更改后,请记住重新加载 Nginx 来应用它们:

sudo nginx -t  # Test the configuration for syntax errors
sudo systemctl reload nginx  # Reload the configuration

然后,尝试

curl http://your_server_ip/redirect_me
或在浏览器中访问该URL,它应该会将您重定向到指定的YouTube视频。

如果没有,请分享您更新的默认配置,我可以进一步查看。

© www.soinside.com 2019 - 2024. All rights reserved.