bideek
By:
bideek

Nginx with Wordpress and a Nodejs app (Mean) in the same VPS (Ubuntu)

August 13, 2016 688 views
Nginx Node.js Ubuntu

Hi,

I'm running a nodejs app in the domain http://test.bideek.com and I want to run a Wordpress with Nginx in http://test.bideek.com/blog

I started with a "one click install" using the Mean application and installing the nodejs app in the port 9000
Then, I'm following this link to install Nginx and Wordpress:
https://www.digitalocean.com/community/tutorials/how-to-install-wordpress-with-nginx-on-ubuntu-14-04

I guess, in the Nginx file config, I should create one location for /blog (linked to Nginx+Wordpress in port 80) and another one for "/" (linked to the node app in port 9000), but doesn't work.
http://test.bideek.com load properly the nodejs app
But, http://test.bideek.com/blog is not loading wordpress.

Am I doing something wrong ?

server {

listen 80 default_server;

listen [::]:80 default_server ipv6only=on;



root /var/www//html;

index index.php index.html index.htm;



# Make site accessible from http://localhost/

server_name bideek.com;

location /blog {

                try_files $uri $uri/ /index.php?q=$uri&$args;

}



error_page 404 /404.html;

error_page 500 502 503 504 /50x.html;
location = /50x.html {
                root /usr/share/nginx/html;
}

location ~ \.php$ {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
}


location /{

        proxy_pass http://localhost:{9000};

        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;

}

}

1 Answer

It can be a bit tricky to get your proxy_pass to not include the subdirectory path under it. This thread provides some useful examples of running a node.js all along side WordPress in much the way you want to. After some trial and error this post provided a workable solution like the following:

 server {
listen 80;
server_name example.com;
access_log /var/log/nginx/nginx-access.log;
error_log /var/log/nginx/nginx-error.log;
root /home/user/example;

# pass the request to the node.js server with the correct headers and much more can be added, see nginx$
location / {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_set_header X-NginX-Proxy true;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
      proxy_pass http://localhost:9000;
      proxy_redirect off;
     location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|js|ttf|woff|eot|svg)$ {
        alias   /home/user/example/public;
    }
}


location @blog {
  rewrite ^/blog(.*) /blog/index.php?q=$1;
}
location /blog {
    index index.php;
    try_files $uri $uri/ @blog;
    alias /home/user/example/blog;

     location ~ \.php$ {
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $request_filename;
        fastcgi_pass 127.0.0.1:9000;
        }

        #if (!-e $request_filename)
        #{
        #   rewrite ^(.+)$ /index.php?q=$1 last;
        #}
} }
Have another answer? Share your knowledge.