Nginx configuration to serve a dynamic files within a root folder - node.js

I have a mern stack application with the following structure
.app/
--client folder
-server.js
-storage folder
The application works just fine in local, though when i have deployed to amazone (ec2) and configured pm2 and nginx everything works fine but the only issue that images saved in my storage folder do not seems to be served.
Static images in the build folder are displayed normaly but when the user uplaods his profile image for example which saved in the storage, the image gets saved but not displayed.
My nginx config are very common:
root /path/tp/build;
location / {
try_files $uri /index.html;
}
location /api/ {
proxy_pass http://localhost:8080;
//other default configs;
}
I was trying to play with config to make it works but wasn't successfull.
I also added a location in the nginx config for the storage folder
location /storage/ {
root /path/to/storage/;
try_files $uri /;
}
I would appreciate any help, thank you.

Related

How can I configure my Nginx file to correctly display my React app?

I've got my build folder on my server and I assume I've correctly configured nginx to be able to see it (I copied the config from Deploy Create-React-App on Nginx), but I still get a 404 page when I try to navigate to it. Is my nginx file just not configured right? I can't figure out what could be causing this problem.
I've tried following Deploy Create-React-App on Nginx exactly, changing things to match my own server name etc. when appropriate.
This is the section I've added to my config file:
server {
listen 80;
server_name xxx.net www.xxx.net;
root /var/www/xxx.net/html/build;
index index.html;
location /news {
try_files $uri /index.html;
}
}

Static files are served locally but not through nginx

I'm running a node.js API on a VPS, which is served by nginx on Ubuntu 13.04.
I'm using restify and serving static files like this:
server.get(
/\/static\/?.*/,
restify.serveStatic({
directory: __dirname // => /home/misha/rxviz-api
})
);
Here is the relevant bits from nginx config:
server {
listen 80;
server_name api.rxviz.com;
location / {
proxy_pass http://localhost:4010/;
}
}
(full config is here)
When running:
curl http://localhost:4010/static/.well-known/acme-challenge/test.json
on the VPS, I get the contents of test.json.
However, when navigating to:
http://api.rxviz.com/static/.well-known/acme-challenge/test.json
in a browser, I get 404.
nginx error logs show that /opt/nginx/html/static/.well-known/acme-challenge/test.json not found.
Why does nginx trying to access test.json in /opt/nginx/html rather than /home/misha/rxviz-api?
Few more points:
static directory permissions are drwxrwxr-x
I can access http://api.rxviz.com/ in the browser successfully
It's hard for me to be sure, as I can't test it, BUT..
I think you have a problem with the nginx settings:
location /static/ {
alias /home/misha/rxviz-api/static/;
}
the /static is appended to the alias including the location part, so it's searches for /home/misha/rxviz-api/static/static
This is going to return 404 since there is no static/ within static/
try to fix to:
location /static/ {
alias /home/misha/rxviz-api/;
}
....
...
and also I wanted just to mention.
It's seems like your node server (using restify) is serving the content as you wanted (127.0.0.1:4010/static/... works as you say),
BUT note that nginx is anyway not redirecting any of the /static calls into your node, so your node restify is unused.
why?
This section:
location /static/ {
alias /home/misha/rxviz-api/static/;
}
tells nginx to NOT redirect those /static calls into your node service, and instead try to find and serve the file from the local path.
so if you want to use the restify... just remove this entire location /static part, and your node will serve those files too.

nginx proxy to remote node.js express app in subdirectory

I am completely stuck with a situation where I want to have several node applications on one server. I get this working fine by having the applications running on different ports. I can access the applications by putting in the ip address with port.
I would like to proxy the applications from my nginx server by using different sub-directories like so:
my.domain
location /app1 {
proxy_pass http://10.131.6.181:3001;
}
location /app2 {
proxy_pass http://10.131.6.181:3002;
}
Doing this I had to move the all the express routes to /app1 for application1. This works but now I am stuck with the static files.
I can now access the application with http://10.131.6.181:3001/app1 which is great, but via http://my.domain/app1 the static files are not loaded.
The static files can be accessed directly http://10.131.6.181:3001/css but not via the proxy http://my.domain/css
Ideally I would like to have the applications on different ports without the sub-directory in the express routes but only sub-directories in the proxy. I tried to put my head through the wall for the last 5 hours but didn't achieve anything.
Now I would happy if can at least get the static files via the nginx proxy.
An updated answer for anyone who needs:
instead of
location /app1 {
proxy_pass http://10.131.6.181:3001/app1;
}
use
location /app1/ {
proxy_pass http://10.131.6.181:3001/;
}
or if on local
location /app1/ {
proxy_pass http://localhost:3000/;
}
This is the correct way and this way you will not need to modify express. Express will receive only the part after /app1/
I finally worked it out after a google surge.
I added the directories to the nginx proxy_pass
my.domain
location /app1 {
proxy_pass http://10.131.6.181:3001/app1;
}
location /app2 {
proxy_pass http://10.131.6.181:3002/app2;
}
And I had to change the express applications to use the subdirectory
app.use('/app1', express.static(path.join(__dirname, 'public')));
app.use('/app1'', require('./routes'));
In the router I had to prefix all the redirects.
router.get('/logout', function (req, res) {
req.logout();
res.redirect('/app1/login');
});
The static files are called like so from html
<link rel="stylesheet" href="/app1/css/style.css"/>
A bit of a pain to change all the redirects and static url. I am sure there is a smarter way by setting a global variable in my node-express app. If anybody knows an easier way please post...

Serving index.html displays "Internal Error: Missing Template ERR_CONNECT_FAIL" in browser

I'm trying to get my Node server up and running on Ubuntu 14.04. I followed a tutorial from DigitalOcean to set up nginx and server blocks to serve my content.
I have the server setup correctly, I believe because I can whois my-site.com and also ping my-site.com. when I visit the web address in the browser, however I get just this error that displays in the page: "Internal Error: Missing Template ERR_CONNECT_FAIL".
I thought that maybe I pointed the nginx server block to the incorrect path, because of of the "Missing Template", but it points to the right file. It is supposed to display a simple index.html file located in /var/www/my-site.com/html.
Here is my server block if this sheds some light on the error:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=off;
root /var/www/my-site.com/html;
index index.html index.htm;
# Make site accessible from http://localhost/
server_name my-site.com www.my-site.com;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
}
This file is located in /etc/nginx/sites-available/my-site.com and I've copied it to the sites-enabled directory as well.
What am I missing here?
This is a pretty standard error message, and in fact as of this moment nodejs.org is displaying that exact same message. I believe it is generated by a reverse proxy: for example, https://searchcode.com/?q=ERR_CONNECT_FAIL shows that ERR_CONNECT_FAIL appears in the squid reverse proxy software. I couldn't find something similar a quick search through the nginx source code.
When I encountered this error message, I was deploying through the digitalocean one-click dokku app and I did not have a domain in /home/dokku/VHOST, so it was being assigned a random internal IP address. I accessed it using [domain]:[port]. Hope that gives you a clue.

getting a 500 internl server error with nginx?

i have installed nginx on my ubuntu ec2 instance and im building an app using node.js, and when i go my amazon url i.e.
http://elastic.ip.address
it works fine, so its running the / file:
app.get('/', function(req, res) {
return res.render('home');
});
however when i try go to http://elastic.ip.address/page2:
app.get('/page2', function(req, res) {
return res.render('page2');
});
I get the 500 internal server error, so i really don't know whats happening, this works on my localhost without running nginx, but not my ec2.
this is my nginx configuration file layout:
server {
listen 80;
root /home/ubuntu/project/;
server_name static_ip.compute-1.amazonaws.com;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to index.html
try_files $uri $uri/ /index.html;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
proxy_pass http://127.0.0.1:8124/;
}
A couple things to try:
Have a look at /var/log/nginx/error.log and see if you can get some better information about what's going wrong.
Make sure that the user that nginx is running as has read permissions to /home/ubuntu/project/.

Resources