[Django] #31354: When I am using a reverse proxy on a non-standard port request.get_host() does not include the port

273 views
Skip to first unread message

Django

unread,
Mar 9, 2020, 11:55:51 PM3/9/20
to django-...@googlegroups.com
#31354: When I am using a reverse proxy on a non-standard port request.get_host()
does not include the port
-------------------------------------+-------------------------------------
Reporter: dgcgh | Owner: nobody
Type: Bug | Status: new
Component: | Version: 3.0
Uncategorized | Keywords: request get_host
Severity: Normal | build_absolute_uri
Triage Stage: | Has patch: 1
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 0 |
-------------------------------------+-------------------------------------
request.get_port() should include the port of the authority if it is not
80 for http or 443 for https. Currently this does not happen when
HTTP_X_FORWARDED_HOST is present in request.META.

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.

Django

unread,
Mar 10, 2020, 12:07:18 AM3/10/20
to django-...@googlegroups.com
#31354: When I am using a reverse proxy on a non-standard port request.get_host()
does not include the port
-------------------------------------+-------------------------------------
Reporter: dgcgh | Owner: nobody
Type: Bug | Status: new
Component: Uncategorized | Version: 3.0
Severity: Normal | Resolution:
Keywords: request get_host | Triage Stage:
build_absolute_uri | Unreviewed
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by dgcgh):

PR: https://github.com/django/django/pull/12550

--
Ticket URL: <https://code.djangoproject.com/ticket/31354#comment:1>

Django

unread,
Mar 10, 2020, 12:09:58 AM3/10/20
to django-...@googlegroups.com
#31354: When I am using a reverse proxy on a non-standard port request.get_host()
does not include the port
-------------------------------------+-------------------------------------
Reporter: dgcgh | Owner: dgcgh
Type: Bug | Status: assigned

Component: Uncategorized | Version: 3.0
Severity: Normal | Resolution:
Keywords: request get_host | Triage Stage:
build_absolute_uri | Unreviewed
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by dgcgh):

* owner: nobody => dgcgh
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/31354#comment:2>

Django

unread,
Mar 10, 2020, 2:18:30 AM3/10/20
to django-...@googlegroups.com
#31354: When I am using a reverse proxy on a non-standard port request.get_host()
does not include the port
-------------------------------------+-------------------------------------
Reporter: dgcgh | Owner: dgcgh
Type: Bug | Status: assigned
Component: Uncategorized | Version: 3.0
Severity: Normal | Resolution:
Keywords: request get_host | Triage Stage:
build_absolute_uri | Unreviewed
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Mar 11, 2020, 4:19:30 AM3/11/20
to django-...@googlegroups.com
#31354: When I am using a reverse proxy on a non-standard port request.get_host()
does not include the port
-------------------------------------+-------------------------------------
Reporter: dgcgh | Owner: dgcgh
Type: Bug | Status: assigned
Component: Uncategorized | Version: 3.0
Severity: Normal | Resolution:
Keywords: request get_host | Triage Stage:
build_absolute_uri | Unreviewed
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Mar 11, 2020, 4:25:33 AM3/11/20
to django-...@googlegroups.com
#31354: HttpRequest.get_host() doesn't include the port from
META['HTTP_X_FORWARDED_PORT'].

-------------------------------------+-------------------------------------
Reporter: dgcgh | Owner: dgcgh
Type: Bug | Status: assigned
Component: HTTP handling | Version: master
Severity: Normal | Resolution:
Keywords: request get_host | Triage Stage: Accepted
build_absolute_uri |
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* component: Uncategorized => HTTP handling
* version: 3.0 => master
* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/31354#comment:5>

Django

unread,
Mar 11, 2020, 4:29:12 AM3/11/20
to django-...@googlegroups.com
#31354: HttpRequest.get_host() doesn't include the port from
META['HTTP_X_FORWARDED_PORT'].
-------------------------------------+-------------------------------------
Reporter: dgcgh | Owner: dgcgh
Type: Bug | Status: assigned
Component: HTTP handling | Version: master
Severity: Normal | Resolution:
Keywords: request get_host | Triage Stage: Accepted
build_absolute_uri |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* needs_better_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/31354#comment:6>

Django

unread,
Mar 13, 2020, 8:02:56 AM3/13/20
to django-...@googlegroups.com
#31354: HttpRequest.get_host() doesn't include the port from
META['HTTP_X_FORWARDED_PORT'].
-------------------------------------+-------------------------------------
Reporter: dgcgh | Owner: dgcgh
Type: Bug | Status: assigned
Component: HTTP handling | Version: master
Severity: Normal | Resolution:
Keywords: request get_host | Triage Stage: Accepted
build_absolute_uri |
Has patch: 1 | Needs documentation: 1
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* needs_docs: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/31354#comment:7>

Django

unread,
May 4, 2020, 12:04:38 AM5/4/20
to django-...@googlegroups.com
#31354: HttpRequest.get_host() doesn't include the port from
META['HTTP_X_FORWARDED_PORT'].
-------------------------------------+-------------------------------------
Reporter: dgcgh | Owner: dgcgh
Type: Bug | Status: assigned
Component: HTTP handling | Version: master
Severity: Normal | Resolution:
Keywords: request get_host | Triage Stage: Accepted
build_absolute_uri |
Has patch: 1 | Needs documentation: 1
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by felixxm):

[https://github.com/django/django/pull/12844 New PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/31354#comment:8>

Django

unread,
Aug 14, 2021, 9:14:49 AM8/14/21
to django-...@googlegroups.com
#31354: HttpRequest.get_host() doesn't include the port from
META['HTTP_X_FORWARDED_PORT'].
-------------------------------------+-------------------------------------
Reporter: dgcgh | Owner: (none)
Type: Bug | Status: new
Component: HTTP handling | Version: dev

Severity: Normal | Resolution:
Keywords: request get_host | Triage Stage: Accepted
build_absolute_uri |
Has patch: 1 | Needs documentation: 1
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by David Smith):

* owner: dgcgh => (none)
* status: assigned => new


--
Ticket URL: <https://code.djangoproject.com/ticket/31354#comment:9>

Django

unread,
Aug 26, 2021, 7:31:16 AM8/26/21
to django-...@googlegroups.com
#31354: HttpRequest.get_host() doesn't include the port from
META['HTTP_X_FORWARDED_PORT'].
-------------------------------------+-------------------------------------
Reporter: dgcgh | Owner:
| LightChain09
Type: Bug | Status: assigned

Component: HTTP handling | Version: dev
Severity: Normal | Resolution:
Keywords: request get_host | Triage Stage: Accepted
build_absolute_uri |
Has patch: 1 | Needs documentation: 1
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by LightChain09):

* owner: (none) => LightChain09


* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/31354#comment:10>

Django

unread,
Aug 26, 2021, 8:32:06 AM8/26/21
to django-...@googlegroups.com
#31354: HttpRequest.get_host() doesn't include the port from
META['HTTP_X_FORWARDED_PORT'].
-------------------------------------+-------------------------------------
Reporter: dgcgh | Owner: (none)
Type: Bug | Status: new

Component: HTTP handling | Version: dev
Severity: Normal | Resolution:
Keywords: request get_host | Triage Stage: Accepted
build_absolute_uri |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by LightChain09):

* owner: LightChain09 => (none)
* needs_docs: 1 => 0


* status: assigned => new


--
Ticket URL: <https://code.djangoproject.com/ticket/31354#comment:11>

Django

unread,
Aug 26, 2021, 8:33:05 AM8/26/21
to django-...@googlegroups.com
#31354: HttpRequest.get_host() doesn't include the port from
META['HTTP_X_FORWARDED_PORT'].
-------------------------------------+-------------------------------------
Reporter: dgcgh | Owner: (none)
Type: Bug | Status: new

Component: HTTP handling | Version: dev
Severity: Normal | Resolution:
Keywords: request get_host | Triage Stage: Accepted
build_absolute_uri |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Sep 11, 2021, 5:00:00 AM9/11/21
to django-...@googlegroups.com
#31354: HttpRequest.get_host() doesn't include the port from
META['HTTP_X_FORWARDED_PORT'].
-------------------------------------+-------------------------------------
Reporter: dgcgh | Owner: SREEHARI
| K.V
Type: Bug | Status: assigned

Component: HTTP handling | Version: dev
Severity: Normal | Resolution:
Keywords: request get_host | Triage Stage: Accepted
build_absolute_uri |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by SREEHARI K.V):

* owner: (none) => SREEHARI K.V


* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/31354#comment:13>

Django

unread,
Oct 4, 2021, 3:40:53 AM10/4/21
to django-...@googlegroups.com
#31354: HttpRequest.get_host() doesn't include the port from
META['HTTP_X_FORWARDED_PORT'].
-------------------------------------+-------------------------------------
Reporter: dgcgh | Owner: SREEHARI
| K.V
Type: Bug | Status: assigned
Component: HTTP handling | Version: dev
Severity: Normal | Resolution:
Keywords: request get_host | Triage Stage: Accepted
build_absolute_uri |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* easy: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/31354#comment:14>

Django

unread,
Oct 4, 2021, 12:13:21 PM10/4/21
to django-...@googlegroups.com
#31354: HttpRequest.get_host() doesn't include the port from
META['HTTP_X_FORWARDED_PORT'].
-------------------------------------+-------------------------------------
Reporter: dgcgh | Owner: SREEHARI
| K.V
Type: Bug | Status: assigned
Component: HTTP handling | Version: dev
Severity: Normal | Resolution:
Keywords: request get_host | Triage Stage: Accepted
build_absolute_uri |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Oct 4, 2021, 2:20:05 PM10/4/21
to django-...@googlegroups.com
#31354: HttpRequest.get_host() doesn't include the port from
META['HTTP_X_FORWARDED_PORT'].
-------------------------------------+-------------------------------------
Reporter: dgcgh | Owner: SREEHARI
| K.V
Type: Bug | Status: assigned
Component: HTTP handling | Version: dev
Severity: Normal | Resolution:
Keywords: request get_host | Triage Stage: Accepted
build_absolute_uri |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Jun 30, 2022, 4:25:54 AM6/30/22
to django-...@googlegroups.com
#31354: HttpRequest.get_host() doesn't include the port from
META['HTTP_X_FORWARDED_PORT'].
-------------------------------------+-------------------------------------
Reporter: dgcgh | Owner: (none)
Type: Bug | Status: new

Component: HTTP handling | Version: dev
Severity: Normal | Resolution:
Keywords: request get_host | Triage Stage: Accepted
build_absolute_uri |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* owner: SREEHARI K.V => (none)


* status: assigned => new


--
Ticket URL: <https://code.djangoproject.com/ticket/31354#comment:17>

Django

unread,
Mar 5, 2023, 1:54:02 PM3/5/23
to django-...@googlegroups.com
#31354: HttpRequest.get_host() doesn't include the port from
META['HTTP_X_FORWARDED_PORT'].
-------------------------------------+-------------------------------------
Reporter: dgcgh | Owner: (none)
Type: Bug | Status: new

Component: HTTP handling | Version: dev
Severity: Normal | Resolution:
Keywords: request get_host | Triage Stage: Accepted
build_absolute_uri |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Mar 18, 2024, 1:47:56 AM3/18/24
to django-...@googlegroups.com
#31354: HttpRequest.get_host() doesn't include the port from
META['HTTP_X_FORWARDED_PORT'].
-------------------------------------+-------------------------------------
Reporter: dgcgh | Owner: (none)
Type: Bug | Status: new
Component: HTTP handling | Version: dev
Severity: Normal | Resolution:
Keywords: request get_host | Triage Stage: Accepted
build_absolute_uri |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Ülgen Sarıkavak):

* cc: Ülgen Sarıkavak (added)

--
Ticket URL: <https://code.djangoproject.com/ticket/31354#comment:19>

Django

unread,
Nov 14, 2024, 4:50:30 PM11/14/24
to django-...@googlegroups.com
#31354: HttpRequest.get_host() doesn't include the port from
META['HTTP_X_FORWARDED_PORT'].
-------------------------------------+-------------------------------------
Reporter: dgcgh | Owner: (none)
Type: Bug | Status: new
Component: HTTP handling | Version: dev
Severity: Normal | Resolution:
Keywords: request get_host | Triage Stage: Accepted
build_absolute_uri |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Calvin Vu):

* cc: Calvin Vu (added)

--
Ticket URL: <https://code.djangoproject.com/ticket/31354#comment:20>

Django

unread,
Nov 18, 2024, 6:01:51 PM11/18/24
to django-...@googlegroups.com
#31354: HttpRequest.get_host() doesn't include the port from
META['HTTP_X_FORWARDED_PORT'].
-------------------------------------+-------------------------------------
Reporter: dgcgh | Owner: Calvin Vu
Type: Bug | Status: assigned
Component: HTTP handling | Version: dev
Severity: Normal | Resolution:
Keywords: request get_host | Triage Stage: Accepted
build_absolute_uri |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Calvin Vu):

* owner: (none) => Calvin Vu
* status: new => assigned

--
Ticket URL: <https://code.djangoproject.com/ticket/31354#comment:21>

Django

unread,
May 29, 2025, 5:33:38 AMMay 29
to django-...@googlegroups.com
#31354: HttpRequest.get_host() doesn't include the port from
META['HTTP_X_FORWARDED_PORT'].
-------------------------------------+-------------------------------------
Reporter: dgcgh | Owner: Calvin Vu
Type: Bug | Status: assigned
Component: HTTP handling | Version: dev
Severity: Normal | Resolution:
Keywords: request get_host | Triage Stage: Accepted
build_absolute_uri |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sage Abdullah):

* needs_better_patch: 1 => 0

Comment:

[https://github.com/django/django/pull/18835 New PR] is available, and
looks like it hasn't been reviewed so I'm removing the "patch needs
improvement" flag.
--
Ticket URL: <https://code.djangoproject.com/ticket/31354#comment:22>

Django

unread,
Jul 12, 2025, 5:04:13 PMJul 12
to django-...@googlegroups.com
#31354: HttpRequest.get_host() doesn't include the port from
META['HTTP_X_FORWARDED_PORT'].
-------------------------------------+-------------------------------------
Reporter: dgcgh | Owner: Calvin Vu
Type: Bug | Status: assigned
Component: HTTP handling | Version: dev
Severity: Normal | Resolution:
Keywords: request get_host | Triage Stage: Accepted
build_absolute_uri |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by blighj):

* needs_better_patch: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/31354#comment:23>
Reply all
Reply to author
Forward
0 new messages