How to configure Nginx for Ember.js + WordPress?

The scenario is that I’d like to use WordPress as a backend API provider for our Ember.js frontend app.

The Ember.js frontend needs to be served from the root, and the WordPress instance ideally would be reachable by going to a subdirectory. So for example on localhost it would be http://localhost and http://localhost/wordpress

Read More

On the disk the two are deployed in /srv/http/ember and /srv/http/wordpress respectively.

I was trying to assemble the configuration going by the example on the Nginx site:
https://www.nginx.com/resources/wiki/start/topics/recipes/wordpress/

The config:

http {

    upstream php {
      server   unix:/run/php-fpm/php-fpm.sock;
    }

    server {
        listen 80;
        server_name localhost;
        root /srv/http/ember;
        index index.html;
        try_files $uri $uri/ /index.html?/$request_uri;

        location /wordpress {
            root /srv/http/wordpress;
            try_files $uri $uri/ /index.php?$args;
        }

        location ~ .php$ {
            #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
            include fastcgi.conf;
            fastcgi_intercept_errors on;
            fastcgi_pass php;
            fastcgi_split_path_info ^(/wordpress)(/.*)$;
        }

    }

}

However this is obviously not the correct solution.
Upon trying to access the address http://localhost/wordpress/index.php I get the following in the logs:

2016/05/01 17:50:14 [error] 4332#4332: *3 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET /wordpress/index.php HTTP/1.1", upstream: "fastcgi://unix:/run/php-fpm/php-fpm.sock:", host: "localhost"

The recipe isn’t clear about where to put the root directive for the location of wordpress. I also tried with adding index index.php, which doesn’t help either.

(Serving the Ember app works fine.)

Related posts

1 comment

  1. From your question it seems that the location ~ .php$ block is used by WordPress alone. However, it needs a root of /srv/http in order to find the script files for URIs beginning with /wordpress under the local path /srv/http/wordpress.

    As there are two locations which both use the same WordPress root, it is possibly cleaner to make /srv/http the default (that is, inherited from the server block) and move root /srv/http/ember; into a separate location / block.

    server {
        listen 80;
        server_name localhost;
        root /srv/http;
    
        location / {
            root /srv/http/ember;
            index index.html;
            try_files $uri $uri/ /index.html?/$request_uri;
        }
    
        location /wordpress {
            index index.php;
            try_files $uri $uri/ /wordpress/index.php?$args;
        }
    
        location ~ .php$ {
            include fastcgi.conf;
            fastcgi_intercept_errors on;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_pass php;
        }
    }
    

    Notice that the default URI in location /wordpress is /wordpress/index.php and not /index.php as you originally had.

    I have explicitly set SCRIPT_FILENAME as it may or may not appear in your fastcgi.conf file.

    fastcgi_split_path_info has been removed as it is unnecessary in your specific case, and I think it would actually break WordPress the way you had it.

Comments are closed.