Running app using PM2 gives me a 502 error - node.js

I have a relatively basic Express app that I have sitting behind nginx.
Everything works fine when I run
sudo node server.js
I can access the website without incident.
However when I use
sudo pm2 start server.js I get an 'online' status and no errors (helpful), but when attempting to access the website I get a 502 error. The instructions for PM2 don't indicate any additional steps are required.
I've looked for solutions to this problem but they all seem slightly different (and there are a lot of them! I'm beginning to wonder if it works at all) - I've tried a few things, like targeting bin/www instead of server.js but no dice.
Do I need to perform additional setup for PM2 compatibility? Should I look for alternatives? What could be causing these problems?
Here is my current nginx config (improvements welcome), but I will stress that the setup works fine when not using PM2.
(Domain replaced with X)
server {
listen 80;
listen 443 ssl;
server_name www.X.co.uk;
ssl_certificate /etc/letsencrypt/live/X.co.uk/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/X.co.uk/privkey.pem;
return 301 https://X.co.uk$request_uri;
}
server {
listen 80;
server_name X.co.uk;
return 301 https://X.co.uk$request_uri;
}
server {
listen 443 ssl;
server_name X.co.uk;
root /var/www/X;
ssl_certificate /etc/letsencrypt/live/X.co.uk/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/X.co.uk/privkey.pem;
location / {
proxy_pass https://localhost:4003;
}
include /etc/nginx/h5bp/location/expires.conf;
include /etc/nginx/h5bp/location/cross-domain-fonts.conf;
include /etc/nginx/h5bp/location/protect-system-files.conf;
}
Thanks!

I had the same problem when installed certbot. But
sudo pm2 delete server
sudo pm2 start server.js
did the trick and everything began to work as expected!

Related

Nginx Configuration for node Js application + Docker

I have a nodeJS application which is configured as a docker container.For making the application up and running, i was initially using 443 as the port - which gave a message 443 is a privileged port and updated the .yml with 8443 as the port.
Previously i used to give the url like - https://abc-acde.xyz , but now have to give https://abc-acde.xyz:8443 the port also along with domain name.
To resolve this i installed nginx to use it as a proxy to reroute the request.
The nginx config file is configured with the key , certs along with the server details - the configuration snippet of the conf file is below :
server {
listen 443 ssl;
server_name abc-acde.xyz;
ssl_certificate /opt/ssl/abc-acde.xyz/abc_acde_xyz.cer;
ssl_certificate_key /etc/ssl/ssl_signed_certs/abc-acde.xyz.key;
}
After this run the command - systemctl restart the nginx
Hope this configuration will help me to use the url as it was earlier - use without the port
Thanks in advance,
Rahul

Setting Up of Nginx reverse proxy

I have a node application running on an ec2 instance. Node is running on port 5000. I want to access the api from remote.
this is nginx configuration file.
server {
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
client_max_body_size 20M;
listen 80;
listen [::]:80;
location / {
proxy_pass http://127.0.0.1:5000;
}
location /nginx_status {
# Turn on stats
stub_status on;
access_log off;
}
}
when I try to curl using curl localhost/nginx_status
it returns
Active connections: 1
server accepts handled requests
11 11 12
Reading: 0 Writing: 1 Waiting: 0
Also when I try to access the IP in browser, it shows
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.
Thank you for using nginx.
But if I try to access the ip_address/nginx_status it shows 404 Error for example if I took IP address 123.456.789.098 in browser it shows the above mentioned message and if I took 123.456.789.098/nginx_status it will return 404 error. Even if I try curl ip_address/nginx_status it is also returning 404 error.
My question is, How can I access node application running on port 5000 from outside world?
unfortunately I only see part of your config, is there another server that listens to 80?
You don't use "default_server" for listen either, and without "server_name" I find it difficult to distinguish between them. So maybe another config with the server + port 80 as default_server takes effect. Check in your /etc/nginx/ folder which servers {..} all exist.
The proxy_pass looks correct, if the nodjs server is really listed there, check again whether it is really http or https scheme. For the correct protocol transmission of the proxy_pass.
But you should then add a control for the "stub_status" so that it is information that you do not entrust to everyone, for me it is the case that only one application has access to it internally and under another list what is not released on the internet:
server {
listen 127.0.0.1:10081 default_server;
location /flyingfish_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
}
I'm curious what you find out! :)

Update server_name in nginx.conf programmatically

I have a trading bot built with flask+gunicorn+nginx deployed on AWS EC2 which runs for 8 hours a day. Everytime I start the instance, the public IP changes which I need to update the server_name in nginx.conf.
Is there a way to update this daily with the help of a script or service? My conf file looks like this :
server{
listen 80;
server_name "public ip I need to change everyday";
location / {
.
.}
}
Thanks.
Since your Nginx configuration has only one server block, make that block the default block. It will capture all requests regardless of the server IP. Therefore, you don't need to specify an IP for the server_name directive.
server {
listen 80 default_server;
server_name _;
# Please put other settings below.
#...
}
For more information, visit the Server names document.

How to connect nginx to local mongodb

I've got nginx to run my (node.js/react) application on the server. But I can't seem to connect to the database.
In the nginx.conf file I've added the following inside http.
http {
...
server {
listen 80;
server_name localhost;
...}
...}
And above the http section I have the following,
stream {
server {
listen 4000;
proxy_connect_timeout 1s;
proxy_timeout 3s;
proxy_pass stream_mongo_backend;
}
upstream stream_mongo_backend {
server 127.0.0.1:27017;
}
}
I start the nginx server, the application runs on localhost, opens up the login page but I can't login because it's still not connected to the database (mongodb).
I'm not sure if I've got my port numbers wrong or if I'm missing some configuration line inside nginx.conf.
EDIT: Ok, I had it wrong before. I wasn't supposed to connect to mongodb at this point. I was supposed to connect to the backend server of my application which would run at 4000. So now I've added a new location for /api/ inside http and proxied all requests to 4000. I still have one question though. I have to run my backend server separately for this to work. For the frontend I've created a folder and put all my build files in there so nginx starts up the webserver from there. Doing the same for the backend did not start up the server. Is there a way to get nginx to start the backend server as well?
Also can I get the frontend to run directly without the build files ? Like node would with npm start?
the port number is right. try to open up a mongo shell and see if you are able to access a mongo instance. if not, you will need to run sudo service mongodb start to start it up.
Guess it's knida late but you don't need to setup nginx for your backend to connect local mongodb.
And you need to run the frontend and backend server first by yarn start, node run or something like that if you want to run it without build files.
And then bypass the calls from 80 port to the local host servers.
For example, your FE run at 3000 port, BE run at 5000 port.
Then your nginx should be:
http {
...
server {
listen 80;
server_name localhost;
location /api/ {
proxy_pass localhost:5000;
}
location / {
proxy_pass localhost:3000;
}
...}
...}

Nginx Phusion Passenger: "passenger_log_file" directive is not allowed

I'm currently running: nginx/1.12.2 with Phusion Passenger 5.1.12.
Whenever I try to use "passenger_log_file", "passenger_default_user" or "passenger_default_group" I get the following error.
nginx: [emerg] "passenger_log_file" directive is not allowed here in /etc/nginx/sites-enabled/test:11
My sites-enabled file looks like this:
server {
listen 80;
server_name example.com;
root /home/test/api;
passenger_app_root /home/test/api/dist;
passenger_enabled on;
passenger_app_type node;
passenger_startup_file app.js;
passenger_log_file /var/log/test/access.log;
}
If I remove the passenger_log_file everything works perfectly fine. What am I missing here?
I had the same issue. I think the correct option for your case is passenger_app_log_file which allow to define a log file for each app. Unfortunately this is an Enterprise level option. In other hand if you move your passenger_log_file a level up it should change all your passenger apps log file from the default one.
passenger_log_file /var/www/vhosts/system/xxx.pasi-consulting.com/logs/proxy_error_log;
server {
listen 137.74.195.27:443 ssl http2;
server_name www.pasi-consulting.com;
server_name www.xxx.pasi-consulting.com;
}
Per the documentation
passenger_log_file is only available at the http context level. In your provided example it is in the server context.
Both passenger_default_user and passenger_default_group are allowed in the server context so that is confusing.

Resources