Question

Serving nodejs static files in the same domain as my wordpress

server { listen 80; listen [::]:80; server_name 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


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.

@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.

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.