This causes issues for e.g. request.build_absolute_uri() if the django app
is running behind a reverse proxy on a non-standard port.
Example would be: nginx is listening on port 8443 and forwarding to a
django server listening on port 8001:
{{{
server {
listen 8443 ssl;
server_name localhost.example.com
location / {
proxy_pass http://localhost:8001;
# include /etc/nginx/uwsgi_params;
proxy_cache_bypass $http_upgrade;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port $server_port;
}
}
}}}
In this case URIs built with request.build_absolute_uri() in a django app
will exclude the port, i.e.:
{{{request.build_django_uri("/") == "https://localhost.example.com/"}}}
where it should return "https://localhost.example.com:8443/"
--
Ticket URL: <https://code.djangoproject.com/ticket/31354>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
Comment (by dgcgh):
PR: https://github.com/django/django/pull/12550
--
Ticket URL: <https://code.djangoproject.com/ticket/31354#comment:1>
* owner: nobody => dgcgh
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/31354#comment:2>
Comment (by dgcgh):
I think the problem here might be that django assumes X-Forwarded-Host
will include the port number, as opposed to putting the port number in X
-Forwarded-Port.
I can get the correct behavior by setting the nginx header as:
{{{
proxy_set_header X-Forwarded-Host $host:$server_port;
}}}
and disabling `USE_X_FORWARDED_PORT` .
I take it that `X-Forwarded-Port` is not popular, so maybe this bug is not
worth fixing, but I will update my PR with a more explicit solution.
--
Ticket URL: <https://code.djangoproject.com/ticket/31354#comment:3>
Comment (by Florian Apolloner):
LGTM, ses my comment on the PR for a small change.
--
Ticket URL: <https://code.djangoproject.com/ticket/31354#comment:4>
* component: Uncategorized => HTTP handling
* version: 3.0 => master
* stage: Unreviewed => Accepted
--
Ticket URL: <https://code.djangoproject.com/ticket/31354#comment:5>
* needs_better_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/31354#comment:6>
* needs_docs: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/31354#comment:7>
Comment (by felixxm):
[https://github.com/django/django/pull/12844 New PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/31354#comment:8>
* owner: dgcgh => (none)
* status: assigned => new
--
Ticket URL: <https://code.djangoproject.com/ticket/31354#comment:9>
* owner: (none) => LightChain09
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/31354#comment:10>
* owner: LightChain09 => (none)
* needs_docs: 1 => 0
* status: assigned => new
--
Ticket URL: <https://code.djangoproject.com/ticket/31354#comment:11>
Comment (by LightChain09):
As far as I can see, there already is documentation about the patch, so I
changed the "Needs documentation" tag.
--
Ticket URL: <https://code.djangoproject.com/ticket/31354#comment:12>
* owner: (none) => SREEHARI K.V
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/31354#comment:13>
* easy: 1 => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/31354#comment:14>
Comment (by SREEHARI K.V):
I'd like to work on this bug.
I notice that https://github.com/django/django/pull/12844 has been closed
due to inactivity.
Can I pick up where Apollo13 left off or start from scratch?
--
Ticket URL: <https://code.djangoproject.com/ticket/31354#comment:15>
Comment (by Jacob Walls):
Ordinarily yes, you can start from a previous attempt and credit the prior
author as a [https://docs.github.com/en/github/committing-changes-to-your-
project/creating-and-editing-commits/creating-a-commit-with-multiple-
authors co-author] in the commit message.
--
Ticket URL: <https://code.djangoproject.com/ticket/31354#comment:16>
* owner: SREEHARI K.V => (none)
* status: assigned => new
--
Ticket URL: <https://code.djangoproject.com/ticket/31354#comment:17>
Comment (by Mateusz Kurowski):
Hello, i seen multiple pull requests that have their origin in this issue
- when it comes to host and port with proxy protocol - does it need some
big rewrite in the django source?
--
Ticket URL: <https://code.djangoproject.com/ticket/31354#comment:18>