Question

Serving nodejs static files in the same domain as my PHP

Hi there! I have website which running on PHP, but also I have NodeJS app wich running on the same domain. NodeJs address is /nodejs, all other requests are under PHP.
PHP project located in folder /home/php-project PHP static files folder /home/php-project/public NodeJS is located in - /home/nodejs-project. NodeJS static files folder /home/nodejs-project/public Is it possible to serve different static folders?

Here is my nodeJs code app.use(express.static(__dirname + '/public'));

Here is the nginx config


server {
    listen         80;
    server_name    www.example.com example.com;
    return         301 https://www.example.com$request_uri;
}

server {
    listen 443 ssl;
    server_name example.com;

   #### Comodo SSL
    ssl_certificate path/to;
    ssl_certificate_key path/to;

    return 301 https://www.example.com$request_uri;
}

server {

    listen 443 ssl;
    server_name   www.example.com;

    #### Comodo SSL
    ssl_certificate path/to;
    ssl_certificate_key path/to;

    root /home/php-project/public;
    index index.php;


    location / {
        autoindex off;
        try_files $uri $uri/ /index.php?$query_string;
    }

    location /node-js {
        proxy_pass http://localhost:5555;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_buffering off;
        root /home/node-js;   
    }


} #### End server setup

Update #1 Currently all static files coming from php static folder


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 @gillesgambier14,

You can try to serve the NodeJS static files using the following server block in your Nginx Configuration

location nodejs-project/public {
    proxy_pass  http://localhost:5555;;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    # Following is necessary for Websocket support
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
}

Based on the location, your Nginx will be used either as a Proxy or not however it should be able to serve the static NodeJS and PHP files without a problem.

Regards, KDSys