Use Nginx to run WordPress and Node.js

February 7, 2017 804 views
Nginx WordPress Node.js PHP Ubuntu 16.04

I have installed the LEMP stack with this guide and have installed wordpress with this guide. They were both great guides but I need help making a small change.

I want for the root route to reverse proxy into a Node.js server. I would then like the /blog route to serve the wordpress site. As it stands right now the root route servers the wordpress site perfectly.

/etc/nginx/sites-enabled/default

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;

    index index.php index.html index.htm index.nginx-debian.html;

    server_name my_ip;

    location = /favicon.ico { log_not_found off; access_log off; }
    location = /robots.txt { log_not_found off; access_log off; allow all; }
    location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
        expires max;
        log_not_found off;
    }

    location / {
        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;
    }

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

How would I modify this Nginx config so that /blog is wordpress and all other routes are handled by a Node.js server?

2 Answers
jtittle February 7, 2017
Accepted Answer

@jamesbedont

Start by creating your /blog directory and move your data over. Then you'd need to change:

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

to

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

For the / location block, we need to setup proxy_pass to point to the location of your Node app.

    location / {
        proxy_pass http://APP_PRIVATE_IP_ADDRESS:8080;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

Where http://APP_PRIVATE_IP_ADDRESS:8080 points to your Node App. So for example, if your app is running on:

127.0.0.1:2365

You'd use:

http://127.0.0.1:2365

You'd then want to reload NGINX:

systemctl reload nginx

or

service nginx restart
  • This works! However it only works fully if WordPress permalinks are set to simple?p=123. For example permalinks set to %postname% would produce links like /blog/hello-world which produce a 404 error.

    interestingly navigating to the admin panel blog/wp-admin/ works fine.

    Here is my current Nginx config after I added your comments. How would I change it to enable post-name permalinks to work?

    server {
        listen 80 default_server;
        listen [::]:80 default_server;
    
        root /var/www/html;
    
        index index.php index.html index.htm index.nginx-debian.html;
    
        server_name 138.197.31.86;
    
        #location = /favicon.ico { log_not_found off; access_log off; }
        #location = /robots.txt { log_not_found off; access_log off; allow all; }
        #location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
        #    expires max;
        #    log_not_found off;
        #}
    
        location / {
            proxy_pass http://localhost:8081;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
        }
    
        location /blog {
            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;
        }
    
        location ~ /\.ht {
            deny all;
        }
    }
    

Hi James,

Check out this post and linked answer. Hope it helps.
StackOverflow solution

Have another answer? Share your knowledge.