"pm2 serve build PORT" echoes 404 not found - javascript

I've done this tutorial,
https://reactjs.org/tutorial/tutorial.html
Then uploaded it to production server, running on ubuntu nginx with SSL. Ran npm run build, created the build files and served static files with serve module serve -s -p 8083 build. It runs OK on port 8083.
But when I try to add it as a pm2 service pm2 serve ./build/ 8083 I get a 404 not found (when it's not running the error is 502 Bad Gateway)
I tried several ways, with pm2 serve ./ 8083, pm2 serve ./public/ 8083, etc.
Nginx config:
location ~* /.(js|jpg|png|css)$ {
access_log off;
expires max;
}
location = /react-game {
root /var/www/test.com/html/react-game/build;
proxy_pass http://localhost:8083;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
autoindex off;
}
Any ideas how to replicate serve -s -p 8083 build with pm2?
Thanks!

I think the main issue is serve is meant to serve static files and pm2 is meant to persistently run a script. Here is an article about serving react with pm2:
https://ygamretuta.xyz/deploy-create-react-app-with-pm2-16beb90ce52
But is there a reason you don't want to just make nginx listen on port 8083 and serve the build files statically by itself? Or is there a reason you're not serving the build files on port 80?

Related

nuxt-img and js files cannot loaded on production mode on server

I'm just learning Nuxt JS, and it's fine on my local computer until i want to try to deploy it on the server,
It's just a simple App, just Image and Text, but when i deploy it on the server the image won't load.
I'm using PM2 to deploy my App on my Ubuntu VPS
You guys can look at the error on https://bappedabjn.id as it look like this
EDIT :
I'm sorry i didn't include detail
Here is where i put the image at static/img/logo.png
And here how i called the image
<nuxt-img src="/img/logo.png" width="100" class="mb-10" />
And lastly here is my nginx.conf for #Lykos94 asking it
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_cache_bypass $http_upgrade;
I tried to create a new NuxtJS project based on your information and added the #nuxt/image and I managed to reproduce your issue. You are currently using the target: 'server' (or none, and it's the default) option in your nuxt.config.js, which requires to have additional configuration for you to use the #nuxt/image module.
In particular you should follow the following guide at this link and add this to your plugins:
import { createIPX, createIPXMiddleware } from "ipx";
// https://github.com/unjs/ipx
const ipx = createIPX({
dir: "static/", // absolute path to images dir
});
export default createIPXMiddleware(ipx);
After following the guide I managed to run the npm run build and run my server in production, displaying correctly the image.

How do I use NGINX or Apache2 on openSUSE to re-route my port 80 to localhost:3000 so I can run my Node.js app

As described by the problem statement, I am running openSUSE (leap 42.3). I have a node app running on localhost:3000 which I would like to publish it (make it available to people outside my network). I already have a domain and currently, the files are being served by apache2 on port 80. I have found tons of similar problems online and solutions for them, but none are specific to mine (I think it is because of the operating system). Can anyone give me step by step solution to what I must do?
The first solution I found told me to change the configuration file and this is what I have right now:
<VirtualHost *:80>
ServerName test.mytestsitebyrichard.com
ServerAlias *.test.mytestsitebyrichard.com
DocumentRoot /srv/www/htdocs
#ProxyRequests on <--currently commented but this is what online tutorials told me to do. However, it is crashing the apache2
#ProxyPass /cs/ http://localhost:3000/
</VirtualHost>
Do I need to enable anything? I have enabled the ProxyPass and ProxyPassReverse from the configuration menu. Any help would be appreciated. Thank you.
Note please refer to the screenshots below:
You can achieve this in Nginx with Reverse Proxy.
In your /etc/nginx/sites-enabled/ directory add a new configuration file (e.g. myapp.conf) with following configuration:
server {
listen 80;
server_name yoururl.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_cache_bypass $http_upgrade;
}
}

Nginx only serving first 72kbs of my JS files

Nginx appears to only be loading the first 72kbs of my javascript files. I've searched all around my nginx config files and cannot see this setting anywhere. I've added things like
location / {
...
proxy_max_temp_file_size 1m;
...
}
and
location / {
...
sendfile on;
sendfile_max_chunk 1m;
...
}
But still I'm unable to overwrite this weird setting that is only allowing first part of the file to load.
The connection uses nginx proxy_pass to foward port 80 to kibanas port '5601'. I feel like there could be a setting that limits file transfer over proxy? Just not sure where to find it.
proxypass connection looks like:
server {
listen 80;
server_name logs.mydomain.com;
auth_basic "Restricted Access";
auth_basic_user_file /etc/nginx/htpasswd.users;
location / {
proxy_pass http://localhost:5601;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
And my default nginx settings is posted here:
http://www.heypasteit.com/clip/OEKIR
Would appear when disk space is low NGINX defaults to minimal safe settings.
When I checked the VMs health I noticed that the disk drive was full. Elasticsearch was logging a couple of GBs worth of text in to error logs each day. I still Have not fully identified why elastic search is flooding the error logs.
But I think excessive disk space usage has contributed to this error. Nginx could be detecting this and switching over to minimal safe configuration which would only allow 72kbs of data to be served per file.
Once I cleared out the excessive logs, Nginx began serving the full JS files again without needing to restart nginx or kibana.

http to a Node server over https nginx website

Is it possible to have a http connection to a node.js server over a website which is generally secured by https ?
how do you suggest to combine a node connection with a website that is operating on https .
As already mentioned in the comments, it is useful to create a proxy to your node.js applications.
A good tutorial is, for instance, from DigitalOcean: https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-14-04
In this tutorial, it shows that the host configuration can look like this:
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://APP_PRIVATE_IP_ADDRESS:8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
In this case, a reverse proxy to the port 8080 was created. Generally, this method can be used for every node.js application.
In your case: add the location block in your https server block and modify the route.
Support for https was just added to node-http-server it can spin up both http and https servers at the same time. It also has some test certs you can use and a cert generation guide for self signing.
note if you want to spin up on port 80 and 443 you'll need to run with sudo
An example for spinning up the same code on both http and https :
var server=require('node-http-server');
server.deploy(
{
port:8000,
root:'~/myApp/',
https:{
privateKey:`/path/to/your/certs/private/server.key`,
certificate:`/path/to/your/certs/server.pub`,
port:4433
}
}
);
Its super simple and has pretty good documentation and several examples in the examples folder.

Error: listen EACCES 0.0.0.0:80 OSx Node.js

I'm following a tutorial in a angularJS book and have to setup a server. This is the server.js file:
var express = require('express');
var app = express();
app.use('/', express.static('./'));
app.listen(80);
I get this error:
$ node server.js
events.js:154
throw er; // Unhandled 'error' event
^
Error: listen EACCES 0.0.0.0:80
I know already, that the Error EACCES means that i don't have access rights to the port 80, but i don't know how to fix this.
Any help much appreciated!
If you need to run the server on port 80 you should use a reverse proxy like nginx that will run using a system account on a privileged port and proxy the requests to your Node.js server running on an unprivileged port (> 1024).
When running in development environment you're pretty much free to run as root (ie. sudo node server.js), but that is rather dangerous in production environment.
Here's a sample nginx config that will see if the request is for a file that exists in the filesystem, and if not, proxy the request to your Node.js server running on port 9000
upstream yournodeapp {
server localhost:9000 fail_timeout=0;
keepalive 60;
}
server {
server_name localhost;
listen 80 default_server;
# Serve static assets from this folder
root /home/user/project/public;
location / {
try_files $uri #yournodeapp;
}
location #yournodeapp {
proxy_pass http://yournodeapp;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
to give root access to node and start server on port 80
you can do
sudo node app.js
this will start the server giving permission on port 80
Foremost, do not run as root. That's asking for 'it'. "It" is really bad. Go see the movie. Then, don't run your node web project as root.
// DO NOT DO THIS!
$ sudo node app.js
// DO NOT DO THIS EITHER!
$ sudo su -
# node app.js
Instead, use PM2 and authbind to do this:
// %user% is whatever user is running your code
sudo touch /etc/authbind/byport/80
sudo chown %user% /etc/authbind/byport/80
sudo chmod 755 /etc/authbind/byport/80
Next, add this alias to your ~/.bash_aliases or ~/.bashrc or ~/.bash_profile:
alias pm2='authbind --deep pm2'
Then, try it with pm2:
pm2 start app.js
On Windows I fixed this by setting Express to listen on port 8080 for HTTP and 8443 for HTTPS. It really doesn't like using those lower number ports. Also I have IIS installed and running so it might be some sort of port conflict there too.
there is
sudo ufw allow 80/tcp
and it should be port forwarded, worked for me atleast

Categories