In this tutorial, you will learn how to configure Django and Wordpress together on the same Nginx server and under the same domain name. We will install WordPress as a subdirectory such as /blog and Django will be the main website at the top-level domain.
Django and WordPress on Nginx
In order to serve WordPress and Django on the same Nginx server, you must use the alias directive to map the location of WordPress to a subdirectory. In the Nginx configuration below, I have WordPress served from the /diamond-blog subdirectory.
# the upstream component nginx needs to connect to upstream django-tda-prod { server unix:///home/daymon/thediamondapp/prod/thediamondapp/tda_prod.sock; } # upstream for wordpress php requests upstream wordpress-prod-php-handler { server unix:/var/run/php/php7.4-fpm.sock; } # configuration of the server server { server_name thediamondapp.com www.thediamondapp.com; gzip_static on; client_max_body_size 5M; charset utf-8; root /home/daymon/thediamondapp/prod/public/; # Only allow connections by domain name if ( $host !~* ^(thediamondapp.com|www.thediamondapp.com)$ ) { return 444; } # Django static files location /static/ { alias /home/daymon/thediamondapp/prod/public/static/; expires 7d; } # Sitemap location ~ ^/(?P<file>sitemap-?.*\.xml)$ { try_files /$file =404; } # Favicon location = /favicon.ico { access_log off; log_not_found off; } # Send all non-media requests to the Django server. location / { uwsgi_pass django-tda-prod; include uwsgi_params; } # Serve wordpress from subdirectory location /diamond-blog { alias /home/daymon/thediamondapp/blog/prod; index index.php; try_files $uri $uri/ /diamond-blog/index.php?$args; location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_param SCRIPT_FILENAME $request_filename; fastcgi_pass wordpress-prod-php-handler; } } }
Within the same location block, you’ll want to embed another location block for handling PHP files from WordPress. Since Django doesn’t use PHP, it makes sense to put this location block here.
In this location block, please note how we use the $request_filename variable rather than $document_root$fastcgi_script_name according to Nginx Pitfalls and Common Mistakes.
For a full walkthrough of configuring Django and WordPress together, check out the video below, and please let me know if you have any question in the comments section.
