Question

Use Nginx to run WordPress and Node.js

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?


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

@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

i can’t access phpmyadmin with this method. i am opening phpmyadmin by typing www.myurl.com/phpmyadmin and www.myurl.com/blog/phpmyadmin. Both not working, Please help me with this

Check this Gist… it works for me ;)

Practically you can copy and paste, just need to update the routes.

https://gist.github.com/yacafx/a730903e8ec69063c18394e4752b6657

Hope this works for you!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel