DEV Community

Gurupal Singh
Gurupal Singh

Posted on

How to Serve Nodejs Application from sub directory with PHP Application on root?

I have php running in root directory and wants to run a microservice built with nodejs in subdirectory. I did some configurations but the css/js/images are showing 404 not found. Below the nginx configuration.

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

    server_name    www.test.com;
return         301 https://$server_name$request_uri;

    root /var/www/html/app/webroot;

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


    location / {
    try_files $uri $uri/ /index.php?$args;
    }
location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_param SCRIPT_FILENAME $request_filename;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        }

}
Enter fullscreen mode Exit fullscreen mode

server {

listen  443 ssl http2;
    listen  [::]:443 ssl http2;
ssl_certificate /etc/nginx/ssl/test_chain.crt;
    ssl_certificate_key /etc/nginx/ssl/www.test.com.key;

root /var/www/html/app/webroot;

    # Add index.php to the list if you are using PHP
    index index.php index.html;

    include snippets/phpmyadmin.conf;

    server_name www.test.com;



# . files
location ~ /\.(?!well-known) {
        deny all;
}

    location / {

try_files $uri $uri/ /index.php?$args;
            }

location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_param SCRIPT_FILENAME $request_filename;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
    fastcgi_buffers               8 16k;
    fastcgi_buffer_size           32k;
        }

    #NODEJS SERVER BLOCK
location /public {
       root /var/www/html/newcars/public/;
    }

    location /newcars {
     rewrite ^/newcars/(.*)$ /$1 break;
 proxy_pass http://127.0.0.1:3000;

}
 #END of NODEJS SERVER BLOCK

}

location ^~ /estore {
        root /var/www/html;
        index index.php index.html index.htm;
        try_files $uri $uri/ @opencart;
location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_intercept_errors on;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_buffers               8 16k;
    fastcgi_buffer_size           32k;
        fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
}

    location ~* (\.(tpl|ini))$
        { deny all; }
}

location @opencart {
    rewrite ^/(.+)$ /estore/index.php?_route_=$1 last;
}

location = /estore/sitemap.xml {
    rewrite ^(.*)$ /estore/index.php?route=extension/feed/google_sitemap break;
}

location = /estore/googlebase.xml {
rewrite ^(.*)$ /estore/index.php?route=extension/feed/google_base break;
}

location /estore/system {
rewrite ^/estore/system/download/(.*) /estore/index.php?route=error/not_found break;
}

location /estore/video {
rewrite ^/estore/video/courses/(.*) /estore/index.php?route=video/courses break;
}

location /estore/courses {
rewrite ^/estore/courses-lists(.*) /estore/index.php?route=course/courseslist break;
}

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

    # robots.txt
    location = /robots.txt {
            log_not_found off;
            access_log    off;
    }

    # assets, media
    location ~* \.(?:css(\.map)?|js(\.map)?|jpe?g|png|gif|ico|cur|heic|webp|tiff?|mp3|m4a|aac|ogg|midi?|wav|mp4|mov|webm|mpe?g|avi|ogv|flv|wmv)$ {
            expires    7d;
            access_log off;
    }

    # svg, fonts
    location ~* \.(?:svgz?|ttf|ttc|otf|eot|woff2?)$ {
            add_header Access-Control-Allow-Origin "*";
            expires    7d;
            access_log off;
    }
Enter fullscreen mode Exit fullscreen mode

}

Top comments (4)

Collapse
 
lito profile image
Lito •

Node can run on any directory, but you must execute it as service (like php-fpm). You can manage your node services with pm2 or run node directly from source folder.

Collapse
 
gurupal profile image
Gurupal Singh •

I am running node app via pm2. But getting 404 for css/images/js. Already given the public path but it is not working

Collapse
 
lito profile image
Lito • • Edited

Are the assets being redirected to your node app? Is pm2 logs showing the assets requests?

Collapse
 
lito profile image
Lito •

Also, you need to change some things in your node app stackoverflow.com/a/40175109