How to get client IP and Port when client send request to my web service via reverse_proxy?

1. Output of caddy version:

v2.6.1 h1:EDqo59TyYWhXQnfde93Mmv4FJfYe00dO60zMiEt+pzo=

2. How I run Caddy:

I run caddy in Oracle VPS Ubuntu with workflow:

a. System environment:

Ubuntu 22.04.1 LTS

b. Command:

sudo systemctl daemon-reload
sudo systemctl restart caddy

d. My complete Caddy config:

My Caddyfile: {
        reverse_proxy {
                header_up Remote-Addr {remote_ip}    # I tried changing these 2 lines 
                header_up Remote-Port {remote_port}  # but the result is still not as expected

3. The problem I’m having:

I run a Django web service (using ASGI) on port 8080 using a reverse_proxy Caddy so it can be accessed from the internet by users.
And I want to know the Remote IP and Remote Port of the incoming user, I was able to get the Client IP from HTTP_X_FORWARDED_FOR but Port is not.

4. Error messages and/or full log output:

Resut of netstat -an -p tcp command

tcp        0      0          ESTABLISHED 336828/caddy        
tcp        0      0         ESTABLISHED 341505/python       
tcp6       0      0 :::80                   :::*                    LISTEN      336828/caddy        
tcp6       0      0 :::443                  :::*                    LISTEN      336828/caddy        
tcp6       0      0      ESTABLISHED 336828/caddy

The results I get from Django command:

HTTP GET / 302 [0.01,]

However, I want something like corresponding to Remote IP : Remote Port

5. What I already tried:

I tried searching the related Django ASGI and Caddy docs to determine if the problem was with Django or Caddy. However, I think it was caused by my program receiving IP:Port which changed after Caddy reverse_proxy from Outsite Address to Internal Address on local network.
Is that correct? and how should I configure Caddy or Django so that I can get the Client Port and display it in Django.

6. Links to relevant resources:

Same problem as this but Real IP not working with Caddy >=2.5, is there any other fix or do I have to reinstall Caddy < 2.5 to use this plugin?

Caddy passes through the original client IP via the X-Forwarded-For header. See the docs:

Why do you need the port? The connecting port will always be random, determined by your machine’s networking stack. It’s almost always irrelevant.

This topic was automatically closed after 30 days. New replies are no longer allowed.