Why Nginx adds "?next=" for my URL on Django Web App?

0

Issue

My Django project has 5 apps: central, mails, insure, redbooks and dshop.

urls.py

urlpatterns = [
    path('login/', auth_views.LoginView.as_view(template_name='central/login.html'), name='login'),
    path('logout/', auth_views.LogoutView.as_view(next_page='login'), name='logout'),
    path('home/', c_view.home, name='home'),
    path('mails/', include('mails.urls')),
    path('insure/', include('insure.urls')),
    path('redbooks/', include('redbooks.urls')),
    path('dshop/', include('dshop.urls')),
    path('adminpage/', admin.site.urls),
    ]

My Django project has been deployed on Digital Ocean.

How To Set Up Django with Postgres, Nginx, and Gunicorn on Ubuntu 20.04

Run with

$ python manage.py runserver 0.0.0.0:8000, WORKS FINE

also

$ gunicorn --bind 0.0.0.0:8000 lskweb.wsgi, WORKS FINE

But when Nginx starts with basic config:

sudo nano /etc/nginx/sites-available/lskweb

    server {
        listen 80;
        server_name 68.183.111.111;
    
        location = /favicon.ico { access_log off; log_not_found off; }
        location /static/ {
            root /home/pawin/lskweb-ocean;
        }
    
        location / {
            include proxy_params;
            proxy_pass http://unix:/home/pawin/lskweb-ocean.sock;
        }
    
    }

Nginx adds "?next=" in front of my Django URL.

Ex.:

  • http://68.183.111.111/?next=/home/ instead of http://68.183.111.111/home/

  • http://68.183.111.111/?next=/mails/ instead of http://68.183.111.111/mails/

But when I click on the browser and go back and click the link again sometime Nginx give the correct URL in one click and sometimes after multiple clicks.


Now, my landing page is http://68.183.111.111/login/ (type in browser address box) and it is OK.

/static/ no problem its serv files OK.

This is my video , when i click any links

https://www.youtube.com/watch?v=rXnP76klkBo

Thank you for your helps 🙂

Solution

This is caused by Django and not nginx.

you probably have @login_required() decorator on your view

When you have a page that requires a login and the user is not logged in, it will redirect you to the login page specified in your LOGIN_URL = 'login' (This is for mine) after the user logs in it’ll then redirect to the page the user was trying to go before.

like this:
http://127.0.0.1:8000/login/?next=/user-dashboard/

it will redirect to the user dashboard after the user logs in.

Answered By – brian yeap

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More