Serving nodejs static files in the same domain as my wordpress

January 19, 2017 629 views
Nginx Node.js WordPress Ubuntu 16.04

server {
listen 80;
listen [::]:80;
servername www.domain.com.br;
return 301 http://www.domain.com.br$request
uri;

root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
location / {
   #try_files $uri $uri/ =404;
   try_files $uri $uri/ /index.php$is_args$args;
}

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}

 location ~ /\.ht {
    deny all;
}

location = /favicon.ico { log_not_found off; access_log off; }
location = /robots.txt { log_not_found off; access_log off; allow all; }
location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
   expires max;
   log_not_found off;
}

location /sistema/ { //RUNNING NODEJS RIGHT HERE
    proxy_read_timeout 600;
    client_max_body_size       500M;
    proxy_pass http://localhost:8080;
    proxy_set_header Host      $host;
    proxy_set_header X-Real-IP $remote_addr;

}
}

I would like to serve my static files or my uploaded files from my NODEJS System through
www.mydomain.com.br/sistema/uploads/path/tofile
www.mydomain.com.br/sistema/views/path/to/views
I am using express.static(__dirname + '/public'); as static folder;

Is there a possibility? I have been trying to set my server blocks, but I get 404 nginx using those url's above.

Thank you,

Enrico Alvarenga

1 Answer
jtittle1 January 19, 2017
Accepted Answer

@marketingcrescimentum

Since the formatting seems to have been chopped up a little bit, can you confirm that this is identical to your server block?

server {
    listen 80;
    listen [::]:80;
    servername www.domain.com.br;

    return 301 http://www.domain.com.br$requesturi;

    root /var/www/html;

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

    location / {
        #try_files $uri $uri/ =404;
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }

    location = /favicon.ico { log_not_found off; access_log off; }
    location = /robots.txt { log_not_found off; access_log off; allow all; }

    location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
        expires max;
        log_not_found off;
    }

    location /sistema/ { //RUNNING NODEJS RIGHT HERE
        proxy_read_timeout 600;
        client_max_body_size       500M;
        proxy_pass http://localhost:8080;
        proxy_set_header Host      $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

If so, to start, this line should be removed:

return 301 http://www.domain.com.br$requesturi;

Why? You're already handling requests at www.domain.com.br by setting up server_name to do the same. The line above would be effective if you setup a second server block which handled redirecting requests from domain.com.br to www.domain.com.br, though inside the server block you've posted, it will simply create issues.

To setup the server block to handle redirects, simply add the following above your existing block (still removing that line from the larger server block -- it's not needed). Now when a requests comes in without the www it'll be redirected to your domain and enforce the www.

server {
    listen 80;
    listen [::]:80;
    servername domain.com.br;

    return 301 http://www.domain.com.br$requesturi;
}

As for the directories, let's break things down a little so I understand the structure you're laying out.

In the server block above, you're defining your root directory as /var/www/html -- Is that the location of your WordPress installation?

If so, what is the physical location of your NodeJS application? Would that be:

/var/www/html/sistema

or

/var/www/html/sistema/public

or another directory? What I need to help you get everything sorted is the full path to each directory you're using for each so I can help you make sure the correct routes are setup.

  • nodejs app location is /var/www/html/liderfuturo/
    /sistema is the url path I want to use.
    /var/www/html/liderfuturo/public is the location of static files.

    • @marketingcrescimentum

      Thanks for clarifying!

      Since you're using Express, my first recommendation would be to use the built-in functions provided by the framework to handle serving your static files.

      Express allows for virtual paths which would allow you configure this setup from within your application and change it on-demand without having to worry about configuring NGINX and using the alias directive.

      For example, you're currently using (from your OP):

      express.static(__dirname + '/public')
      

      All you should need to do is modify that slightly and wrap it as noted here.

      app.use( '/sistema', express.static('public') )
      

      If your NodeJS application exists in

      /var/www/html/liderfuturo
      

      The above should automatically set the public directory to:

      /var/www/html/liderfuturo/public
      

      and you should then be able to make the calls you're trying to make, i.e.

      http://yourdomain.ext/sistema/uploads/path/file.ext

Have another answer? Share your knowledge.