Ubuntu + Apache (mod_wsgi) + Django - HTTPSConnectionPool NewConnectionError - [Errno 111] Connection refused

Environment:

Ubuntu 24.04, Apache 2.4 (+ libapache2-mod-wsgi-py3 5.0.0-1build2), Python 3.12 (+ requests 2.32.3), Django Admin 5.1.6

Error:

I have connected a Django project through Apache (mod-wsgi) and it is running fine (at 'https mysub.mydom.com/myapp') except the requests.post in the python codes failing with:

HTTPSConnectionPool(host='mysub.mydom.com', port=443): Max retries exceeded with url: /myapp/api/v1/endpoint (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7191eb589e50>: Failed to establish a new connection: [Errno 111] Connection refused'))

'https mysub.mydom.com/myapp/api/v1/endpoint' is working (200) from the browser. "Apache Full" is allowed from ufw.

mysub.mydom.com.conf (for Apache sites-enabled alongwith mysub.mydom.com-le-ssl.conf generated by certbot):

Define wsgi_daemon "mydjango_proc"

<VirtualHost *:80>
    ServerName mysub.mydom.com
    ServerAdmin webmaster@mydom.com
    DocumentRoot /home/myuser/my_django/my_project
    
    <IfModule mpm_itk_module>
            AssignUserId myuser www-data
    </IfModule>
    
    ErrorLog /home/myuser/my_django/log/my_project.error.log
    CustomLog /home/myuser/my_django/log/my_project.access.log common

    Alias /static /home/myuser/my_django/my_project/static
    <Directory /home/myuser/my_django/my_project/static>
            Require all granted
    </Directory>

    <Directory /home/myuser/my_django/my_project/my_project>
        <Files wsgi.py>
              Require all granted
        </Files>
    </Directory>
    
    <Directory /home/myuser/my_django/my_project>
          Require all granted
    </Directory>
    
    <IfDefine !wsgi_init>
            WSGIApplicationGroup %{GLOBAL}
            WSGIProcessGroup %{GLOBAL}
            WSGIDaemonProcess ${wsgi_daemon} python-home=/home/myuser/my_django/sklp_env python-path=/home/myuser/my_django/my_project user=myuser socket-user=myuser
            WSGIProcessGroup ${wsgi_daemon}
            WSGIScriptAlias / /home/myuser/my_django/my_project/my_project/wsgi.py
            Define wsgi_init 1
    </IfDefine>
</VirtualHost>

The Django location + structure:

home
|-- myuser
    |-- my_django (group: www-data)
        |-- sklp_env
        `-- my_project (group: www-data)
           |-- myapp
           |-- my_project
           |-- static
           |-- db.sqlite3 (group: www-data / permission: 664)
           `-- manage.py

Some settings of the project (/home/myuser/my_django/my_project/my_project/settings.py):

...
ALLOWED_HOSTS = ['xxx.xxx.xxx.xxx','mysub.mydom.com']

SECURE_HSTS_SECONDS = 60
SECURE_HSTS_INCLUDE_SUBDOMAINS = True
SECURE_HSTS_PRELOAD = True
SECURE_SSL_REDIRECT = True
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True
...

I am just wondering what the problem would be when the urls (without codes containing requests) are working fine in the browser with https, but those with requests faces a refusal.

Thank you for reading this far. And a lot more for giving it a thought.

Вернуться на верх