【问题标题】:How to redirect 404 requests to homepage in Django single page app using Nginx?如何使用 Nginx 将 404 请求重定向到 Django 单页应用程序的主页?
【发布时间】:2017-07-17 02:17:48
【问题描述】:

我有一个 django 单页应用程序。目前,当您访问网站上不存在的 url 时,会显示 404 错误。但是,在这种情况下,我想重定向到网站的主页。我不确定我是否应该如何使用 Nginx 执行此操作,或者是否有办法在 Django 中执行此操作?下面附上我的 Nginx 文件。我尝试使用以下设置,但没有成功。

error_page 404 = @foobar;

location @foobar {
  return 301 /webapps/mysite/app/templates/index.html;
}


upstream mysite_wsgi_server {
  # fail_timeout=0 means we always retry an upstream even if it failed
  # to return a good HTTP response (in case the Unicorn master nukes a
  # single worker for timing out).

  server unix:/webapps/mysite/run/gunicorn.sock fail_timeout=0;
}

server {
    listen      80;
    server_name kanjisama.com;
    rewrite     ^ https://$server_name$request_uri? permanent;
}

server {
    listen              443;
    server_name         kanjisama.com;
    ssl on;
    ssl_certificate     /etc/letsencrypt/live/kanjisama.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/kanjisama.com/privkey.pem;
    ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;

    client_max_body_size 4G;

    access_log /webapps/mysite/logs/nginx_access.log;
    error_log /webapps/mysite/logs/nginx_error.log;

    location /static/ {
        alias   /webapps/mysite/app/static/;
    }

    location /media/ {
        alias   /webapps/mysite/media/;
    }

    location / {
        if (-f /webapps/mysite/maintenance_on.html) {
            return 503;
        }

        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header Host $host;
        proxy_redirect off;

        # Try to serve static files from nginx, no point in making an
        # *application* server like Unicorn/Rainbows! serve static files.
        if (!-f $request_filename) {
            proxy_pass http://mysite_wsgi_server;
            break;
        }

    # Error pages
    error_page 500 502 504 /500.html;
    location = /500.html {
        root /webapps/mysite/app/mysite/templates/;
    }

    error_page 503 /maintenance_on.html;
    location = /maintenance_on.html {
        root /webapps/mysite/;
    }

    error_page 404 = @foobar;

    location @foobar {
      return 301 /webapps/mysite/app/templates/index.html;
    }
}

【问题讨论】:

    标签: python django nginx


    【解决方案1】:

    首先,创建一个视图来处理所有 404 请求。

    # views.py
    
    from django.shortcuts import redirect
    
    def view_404(request, exception=None):
        # make a redirect to homepage
        # you can use the name of url or just the plain link
        return redirect('/') # or redirect('name-of-index-url')
    

    其次,将以下内容放入项目的urls.py

    handler404 = 'myapp.views.view_404' 
    # replace `myapp` with your app's name where the above view is located
    

    【讨论】:

    • 来自Django 2.2,如果您实现自定义视图,请确保它接受请求和异常参数并返回 HttpResponseNotFound。
    【解决方案2】:

    404 页面错误处理

    ->在 settings.py 中设置 DEBUG=False

    -> handler404='appname.views.view_404' 在根 urls.py 文件的底部添加这一行。

    ->将此函数添加到handler404中提到的应用程序的视图文件中

    def view_404(request,exception=None)
        return redirect('redirect-url-name')
    

    【讨论】:

    • this existing answer 已经提供了这个解决方案。在回答老问题时,请确保您的回答对问答提供了独特而有价值的贡献。
    • 我告诉你在 settings.py 根文件中添加这一行 在 settings.py 中设置 DEBUG=False
    猜你喜欢
    • 2021-11-03
    • 1970-01-01
    • 2017-04-17
    • 1970-01-01
    • 1970-01-01
    • 2014-03-20
    • 2017-08-07
    • 2014-11-18
    • 1970-01-01
    相关资源
    最近更新 更多