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.

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值