NGinX config for Django and WordPress in subdirectory

Helmut asked:

I need to set up a Django site at the root of a domain, but then have a WordPress installation in a subdirectly (e.g. /blog/). How would one configure NGinX to do this? “Pretty” URLs have to work for WordPress as well.

For Django I am using Gunicorn, which is already configured. From NGinX I would call “proxy_pass” to direct to that. PHP is run via FPM.

Considering the restrictions above, how would I configure NGinX? Any help would be appreciated!

Thanks.

UPDATE: I’ve tried several things, and currently I have it working partly. I’ve removed any Django config. Then I’ve got WordPress in a /blog/ subdirectory. In there, I have test.php, which just calls phpinfo(), and then also test.html, which shows pure html. When calling test.php, the page loads. However, when I call test.html, or index.php (WordPress index page), it gives me a 404.

My nGinx config:

server {
        listen   80;

        root /path/to/www/root;

        server_name domain.com;

        access_log /path/to/logs/access.log;
        error_log /path/to/logs/error.log;

        location / {
                index index.php;
        }

        location /blog {
                alias /path/to/www/blog;
                try_files $uri =404;
                index index.php index.html;
        }

        location ~ /blog/.+\.php$ {
                include /etc/nginx/fastcgi_params;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME /path/to/www/blog$fastcgi_script_name;
        }

        location ~ /\.ht {
                deny all;
        }
}

My answer:


You set up a location /blog/ and use all the usual nginx directives to pass off to PHP.


View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.