How do I add a Wordpress Install to "/blog" on my server when "/" serves a different app via NGINX?

June 16, 2017 167 views
WordPress Django Nginx Ubuntu 16.04

I have a Django app served at "domain.com/"
I want to serve a Wordpress install from "domain.com/blog" so that I can enable SSL for both.

All I ever get is a 404 not found on the "/blog" entry. My NGINX conf file is shown below.

server {
listen 80;
listen [::]:80;
servername domain.org www.domain.org;
return 301 https://$server
name$request_uri;
}

server {

# SSL configuration
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name www.domain.org domain.org;
include snippets/ssl-domain.org.conf;
include snippets/ssl-params.conf;

access_log /home/user/logs/nginx/nginx-access.log;
error_log  /home/user/logs/nginx/nginx-error.log;

client_max_body_size 4G;

root /home/user/app;

keepalive_timeout 5;

location = /favicon.ico { access_log off; log_not_found off; }

location /static {
    alias /home/user/app/static;
}


location /uploads {
    alias /home/user/uploads;
}

error_page 500 502 503 504 /500.html;
location = /500.html {
    alias /home/user/app/app/app/templates;
}

location / {
    include proxy_params;
    proxy_pass http://unix:/home/user/app/mosaic.sock;
}

location ~ /.well-known {
    allow all;
}

location /blog {
    try_files $uri $uri/ /index.php$is_args$args;
}

}

3 Answers
davemerwin June 19, 2017
Accepted Answer

To round this out, I'm posting the solution for prosperity. Even though @hansen didn't post the final solution, they were crucial to figuring this out.

server {
    listen 80;
    listen [::]:80;
    server_name domain.org www.domain.org;
    return 301 https://$server_name$request_uri;
}

server {

    # SSL configuration

    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name www.domain.org domain.org;
    include snippets/ssl-domain.org.conf;
    include snippets/ssl-params.conf;

    access_log /home/user/logs/nginx/nginx-access.log;
    error_log  /home/user/logs/nginx/nginx-error.log;

    client_max_body_size 4G;

    root /home/user;

    keepalive_timeout 5;

    location = /favicon.ico { access_log off; log_not_found off; }

    location /static {
        alias /home/user/app/static;
    }


    location /uploads {
        alias /home/user/uploads;
    }

    error_page 500 502 503 504 /500.html;
    location = /500.html {
        alias /home/user/app/app/app/templates;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/user/app/app.sock;
    }

    location ~ /.well-known {
        allow all;
    }

    location ^~ /blog {

        index index.php index.html index.htm index.nginx-debian.html;
        try_files $uri $uri/ /blog/index.php$is_args$args;

        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }
    }
}

Hi @dave0ca67299bda3400d468baf

Try changing your bottom location block to this (I'm guessing php7.0-fpm is installed):

location ^~ /blog {
  try_files $uri $uri/ /blog/index.php$is_args$args;
}
location ~ \.php$ {
  include snippets/fastcgi-php.conf;
  fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
  • That's a good update and led me down a path to fix a few other things, but it's still just displaying a 404.

    • @dave0ca67299bda3400d468baf
      Can you generate the 404 error and then run this command to show the error log:

      tail -20 /home/user/logs/nginx/nginx-error.log
      
      • Ok. Seems to be working a little better. Now it's downloading the file. For instance, the info.php test will download the file not render the PHP.

        • @davemerwin
          Can you post your entire server block. Not sure why it's not working.

          • server {
                listen 80;
                listen [::]:80;
                server_name domain.org www.domain.org;
                return 301 https://$server_name$request_uri;
            }
            
            server {
            
                # SSL configuration
            
                listen 443 ssl http2;
                listen [::]:443 ssl http2;
                server_name www.domain.org domain.org;
                include snippets/ssl-domain.org.conf;
                include snippets/ssl-params.conf;
            
                access_log /home/user/logs/nginx/nginx-access.log;
                error_log  /home/user/logs/nginx/nginx-error.log;
            
                client_max_body_size 4G;
            
                root /home/user/app;
            
                keepalive_timeout 5;
            
                location = /favicon.ico { access_log off; log_not_found off; }
            
                location /static {
                    alias /home/user/app/static;
                }
            
            
                location /uploads {
                    alias /home/user/uploads;
                }
            
                error_page 500 502 503 504 /500.html;
                location = /500.html {
                    alias /home/user/app/app/app/templates;
                }
            
                location / {
                    include proxy_params;
                    proxy_pass http://unix:/home/user/app/app.sock;
                }
            
                location ~ /.well-known {
                    allow all;
                }
            
                location ^~ /blog {
                    alias /home/user/blog;
                    index index.php index.html index.htm index.nginx-debian.html;
                    try_files $uri $uri/ /index.php$is_args$args;
                }
            
                location ~ \.php$ {
                    include snippets/fastcgi-php.conf;
                    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
                }
            }
            

@davemerwin
Okay, let's try this instead - I don't have an active test box to play with right now:

    location /blog/ {
        alias /home/user/blog;
        index index.php index.html index.htm index.nginx-debian.html;
        try_files $uri $uri/ /index.php$is_args$args;

        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }
    }
  • That is getting me closer. Now I'm getting "No input file specified." in the browser and this is the error:

    2017/06/19 14:23:12 [error] 11837#11837: *635 FastCGI sent in stderr: "Unable to open primary script: /home/user/blog/blog/info.php (No such file or directory)" while reading response header from upstream, client: XX.XXX.XX.XXX, server: www.domain.org, request: "GET /blog/info.php HTTP/2.0", upstream: "fastcgi://unix:/run/php/php7.0-fpm.sock:", host: "domain.org"
    

    So,clearly I have a redundant "blog" somewhere!

Have another answer? Share your knowledge.