I’m having trouble getting Apache to serve static files for my Django project hosted on a separate server. My main server, http://metadbdev.riken.jp/BEHF
, is where the Apache configuration resides. I’m trying to enable the Django server on 192.168.80.86
and serve its static files through the main server.
Apache Configuration (httpd.conf
):
## BEHF
ProxyPass /BEHF/static/ !
Alias /BEHF/static/ /home/julio/repos/event_finder/django_frontend/staticfiles/
<Directory /home/julio/repos/event_finder/django_frontend/staticfiles/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
<Location /BEHF>
ProxyPass http://192.168.80.86:8000
ProxyPassReverse http://192.168.80.86:8000
</Location>
Django settings.py
static settings:
BASE_DIR = Path(__file__).resolve().parent.parent
# STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
ALLOWED_HOSTS = ['192.168.80.86','127.0.0.1','http://metadbdev.riken.jp/']
STATIC_URL = '/BEHF/static/'
# STATIC_URL = 'http://192.168.80.86/BEHF/static/'
STATIC_ROOT = '/home/julio/repos/event_finder/django_frontend/staticfiles/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static'),]
Directory Listing:
.
├── db.sqlite3
├── django_frontend
│ ├── __init__.py
│ ├── __pycache__
│ ├── asgi.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── manage.py
├── nohup.out
├── search_engine
│ ├── __init__.py
│ ├── __pycache__
│ ├── admin.py
│ ├── apps.py
│ ├── migrations
│ ├── models.py
│ ├── tests.py
│ ├── urls.py
│ └── views.py
├── static
│ ├── images
│ └── styles
├── staticfiles
│ ├── admin
│ ├── bef.png
│ ├── images
│ ├── lol.txt
│ └── styles
└── templates
├── base.html
├── base_no_search.html
├── document.html
├── search.html
└── search_results.html
SELinux Status: Disabled
Logs:
[14/Aug/2023 07:43:13] "GET / HTTP/1.1" 200 2299
[14/Aug/2023 07:43:13] "GET /static/styles/main.css HTTP/1.1" 404 179
[14/Aug/2023 07:43:13] "GET /static/images/bef.png HTTP/1.1" 404 179
[14/Aug/2023 07:43:13] "GET /static/styles/main.css HTTP/1.1" 404 179
Additional Information:
- I’ve confirmed that the
mod_alias
module is loaded in Apache. - The file permissions for the
staticfiles
directory and its contents are set to be accessible by the Apache user. - I’ve tried accessing the static files directly using the alias, e.g.,
http://metadbdev.riken.jp/BEHF/static/images/bef.png
, but it results in a 404. - There are no conflicting
Alias
directives in other configuration files. - I’ve run Django’s
collectstatic
command to ensure all static files are in thestaticfiles
directory. - Temporarily commenting out the
ProxyPass
directive for/BEHF
doesn’t resolve the issue.
Any insights or suggestions would be greatly appreciated!
2
Answers
Remove the location
/BEHF/
proxypass and set it under yourstatic
proxypass setting as followsThat is to make the rules under the same parser
It looks like you’ve set up your Apache configuration and Django settings for serving static files, but there might be a few points to double-check and troubleshoot:
Order of Configuration Files: Make sure that your Apache configuration is being read. Sometimes, there could be multiple configuration files or virtual hosts, and if there’s an issue with the order of loading, your specific settings might not be applied.
STATIC_ROOT in Django Settings: The
STATIC_ROOT
should be the absolute filesystem path to the directory wherecollectstatic
has gathered all your static files. The path you have provided (/home/julio/repos/event_finder/django_frontend/staticfiles/
) seems correct, assuming that it’s the actual path where the files are collected.Permissions and Ownership: Double-check that the permissions and ownership of the static files and directories are correctly set so that the Apache user has read access. You mentioned that the permissions are set correctly, but it’s always good to verify.
Collect Static Files: Run the
collectstatic
management command again to make sure all your static files are in theSTATIC_ROOT
directory. Sometimes, if new files are added, they might not be collected properly.URL Format: When accessing static files through Apache, make sure you are using the correct URL format. In your case, it should be something like:
http://metadbdev.riken.jp/BEHF/static/images/bef.png
.Django Debug Mode: While not directly related to serving static files, if you are experiencing issues, temporarily turning on Django’s debug mode (
DEBUG = True
) could help in identifying any potential problems.Browser Cache: Clear your browser cache or try accessing the static files in an incognito window to ensure you’re not seeing cached results.
Firewall or Network Issues: Ensure that there are no firewall rules or network issues that might be preventing the communication between the Apache server and the Django server.
If you have verified all these points and the issue still persists, you might want to provide any relevant error messages from Apache’s error logs, as they can often give clues about what’s going wrong.