Question

How Serve Nodejs Application from sub directory ?

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


}
Show comments

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Hi there @gurupal,

What is the exact error that you get in your browser’s web console? It could be that the path to your static files in your Node.js application is not set correctly.

You could try chaning the rule for your statuc files to:

        location /public {
           alias /var/www/html/newcars/public/;
        }

Also make sure that Nginx has the correct permissions to the /var/www/html/newcars/public directory.

Regards, Bobby