getting a 500 internl server error with nginx? - node.js

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

Related

Enabling React/Express Route with Query Parameters in Nginx

Working on a link shortening website. Site works as intended in my localhost production environment, but I can't seem to get an Express GET route with query parameters working after enabling Nginx on my deployed Digital Ocean Ubuntu Linux server.
Node.js/Express GET route:
router.get("/:code", async (req, res) => {
try {
const url = await Url.findOne({ urlCode: req.params.code });
if (url) {
return res.redirect(url.longUrl);
} else {
return res.status(404).json("no url found");
}
} catch (error) {
console.error(error);
res.status(500).json("server error");
}
});
Nginx config file (etc/nginx/sites-available/default):
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
server_name myname.com www.myname.com;
location / {
proxy_pass http://localhost:3000/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_cache_bypass $http_upgrade;
}
If I change the localhost port to my Express server (7777), the GET route works with the URL query parameter (ie: http://example.com/random8chars), but the React front end doesn't load.
As currently configured (port 3000/React server), a Postman GET route to "/:code" returns the desired result, but when I enter the converted link into the URL bar in Chrome it returns the default splash page. In fact, when I enter ANY extension beyond my site name in Chrome it always shows the default splash page. I know this is an issue with Nginx, but I can't seem to get it to work.
Been working on it all day to no avail. Found multiple Stack Overflow threads touching on the subject but nothing works. I tried adding a second location route to the Nginx config file, to no avail. an example of what I've tried:
location /:code {
proxy_pass http://localhost:7777/:code;
}
Please help! I am stuck and feel like I am so close to getting this working. I would greatly appreciate any insight into fixing this. Thank you.
I was just add a comment, but I don't have the rep yet. So it seems to me that Nginx is trying to serve another file, that doesn't exists. Try to add:
location / {
try_files $uri /index.html;
}
This will make nginx always try in the rigth file ignoring the path.
so i finally solved the issue! i was initially approaching this problem from the wrong perspective. see, when in my development environment, i had a proxy set up on my react server to bounce any unknown requests to my express server. the thing is, that proxy set in the package.json doesn't work in a production environment, which is why my link shortening app worked in dev but wasn't duplicated in production.
this is how i solved it:
i set up nginx to point to my express server # port 7777. i used the npm build command to export my react application to the build folder, and then i set up my express server to serve the build folder. that's it. when you access the website's default URL it loads the react build folder and then when i put in a shortened link (GET /:code) it works like a charm. thanks!

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

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.

how to hosting multiple Nodejs applications with nginx

I understand that multiple node.js can be run on one server using Nginx. I've got Nginx setup and running on a Ubuntu server. I have two node js applications :
127.0.0.1:3001 and
127.0.0.1:3002. I want to access the different application with different url. For example, if I want access
127.0.0.1:3001, I would use url: http://121.42.20.100/. And if want access the application of
127.0.0.1:3002, I would use url: http://121.42.20.100/admin
the default file on the folder sites-available follows :
server {
#listen 80; ## listen for ipv4; this line is default and implied
#listen [::]:80 default ipv6only=on; ## listen for ipv6
root /usr/share/nginx/www;
index index.html index.htm;
# Make site accessible from ` http://localhost/`
server_name 0.0.0.0;
location / {
proxy_pass ` http://127.0.0.1:3001`;
}
location /admin/ {
proxy_pass ` http://127.0.0.1:3002`;
}
}
When I access url like http://121.42.29.100/, it works that get response from
127.0.0.1:3001. However, when I access url like http://121.42.29.100/admin, it does not work that it shows error of "Cannot GET /admin/". How can I configure the nginx to get it work?

Resources