How to Serve Nodejs Application from sub directory with PHP Application on root?
Tags: node, nginx, php
Published: 2020-10-23 21:18:33

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;
            }

    }

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;
        }


}