NGINX (13 Permission Denied) When Passing Port Through PM2 EcoSystem Config File - node.js

I am using NGINX and PM2 to run a NodeJS app on an EC2 instance. The app runs on port 9443 and NGINX is listening in on port 443.
When I hardcode port 9443 directly into my index.js everything works great, but if I pass in the port via the PM2 ecosystem config file, I get a 502/bad gateway error and the 13: Permission Denied error in my NGINX error.log file. Could someone help me with this?
nginx.conf
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
}
server.conf
server {
listen 80;
listen [::]:80;
server_name $MY_SERVER_NAME$;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
listen [::]:443 ssl;
access_log /location/to/logs;
ssl_certificate /location/to/cert;
ssl_certificate_key /location/to/cert/key;
server_name $MY_SERVER_NAME$;
location / {
proxy_set_header HOST $host;
proxy_set_header X-REal-IP $remote_addr;
proxy_pass http://127.0.0.1:9080;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
ecosystem.config.js
module.exports = {
apps: [
{
name: 'my-server',
cwd: '/home/centos/my-server',
script: '/home/centos/my-server/index.js',
watch: ['/home/centos/my-server/'],
ignore_watch: ['/home/centos/my-server/node_modules'],
env_production: {
NODE_ENV: 'production',
PROD_PORT: 9080,
},
},
],
};
Output of ps aux | grep nginx and ps aux | grep pm2
root 847 0.0 0.0 119320 2240 ? Ss 16:52 0:00 nginx: master process /usr/sbin/nginx
nginx 848 0.0 0.2 152000 10084 ? S 16:52 0:00 nginx: worker process
nginx 849 0.0 0.2 152000 8076 ? S 16:52 0:00 nginx: worker process
centos 1259 0.6 1.4 837268 54648 ? Ssl 17:00 0:00 PM2 v4.5.6: God Daemon (/home/centos/.pm2)
index.js: not the full file, but the part where the PORT is used
const http = require('http');
const winston = require('winston');
const nconf = require('nconf');
module.exports = async function (app) {
const DEV_PORT = 9080;
if (nconf.get('nodeEnv') === 'local') {
app.listen(DEV_PORT, () => winston.info(`Listening on port ${DEV_PORT}...`));
} else {
http.createServer(app).listen(process.env.PROD_PORT);
}
};

Related

Not found error with two Node projects served by NGINX with Docker

I'm learning Docker and my goal is to serve two Node.js projects whit same docker-compose using NGINX. My two project(A and B) are simple hello world:
'use strict';
const express = require('express');
// Constants
const PORT = 8301;
const HOST = '0.0.0.0';
const PATH = '/project-a';
// App
const app = express();
app.get(PATH, (req, res) => {
res.send('<h1>Hello World</h1><p>Project A</p>');
});
app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}${PATH}`);
Above there is A, B has the same code but change only port 8302 and path /project-b. Below the docker-compose:
version: '3.7'
services:
website_a:
image: project_a/node
build:
context: ./projects_a
dockerfile: Dockerfile
container_name: project_a
restart: always
command: sh -c "node server.js"
expose:
- 8301
website_b:
image: project_b/node
build:
context: ./projects_b
dockerfile: Dockerfile
container_name: project_b
restart: always
command: sh -c "node server.js"
expose:
- 8302
nginx:
image: node-project-multisite/nginx
build: nginx
container_name: multisite_project_nginx
ports:
- 80:80
depends_on:
- website_a
- website_b
And the nginx's conf:
server {
listen 80;
listen [::]:80;
server_name 127.0.0.1;
# Logging
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
location /project-a {
proxy_pass http://website_a:8301/project-a;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
# max uploadable file size
client_max_body_size 4G;
}
}
server {
listen 80;
listen [::]:80;
server_name 127.0.0.1;
# Logging
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
location /project-b {
proxy_pass http://website_b:8302/project-b;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
# max uploadable file size
client_max_body_size 4G;
}
}
Using the docker-compose without NGINX I can see both hello world, but with NGINX I can use only A, in B there is the message below:
Where I put a mistake?
You should use only one "server" since they share server_name and port
server {
listen 80;
listen [::]:80;
server_name 127.0.0.1;
# Logging
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
location /project-a {
proxy_pass http://website_a:8301/project-a;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
# max uploadable file size
client_max_body_size 4G;
}
location /project-b {
proxy_pass http://website_b:8302/project-b;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
# max uploadable file size
client_max_body_size 4G;
}
}

Nodejs not running in server

I am trying to launch nodejs in my server. this is my node.js code:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(8080, 'xx.xx.xx.xx');
console.log('Server running at http://xx.xx.xx.xx:8080/');
this is my /etc/nginx/nginx.conf
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server{
location / {
proxy_pass http://xx.xx.xx.xx: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;
}
}
}
#
I also deleted a few lines from default.conf because it was showing up error nginx: [emerg] bind() to [::]:80 failed (98: Address already in use) and following #rednaw's answer
I am trying to run node.js in my xx.xx.xx.xx server. Now it's just showing this at http://xx.xx.xx.xx/ and This site can’t be reached xx.xx.xx.xx refused to connect. ERR_CONNECTION_REFUSED at http://xx.xx.xx.xx:8080/
You have to mention the port number i.e not in use to listen.
Do it some thing like this----
var http = require("http");
function onRequest(request, response) {
console.log("Request received.");
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}
http.createServer(onRequest).listen(8083);
In my scenario the port number 8080 was already in use....So i used
another port number like 8083....It works...

How to connect to a nodejs express server through proxy

We have a nodejs http server running on port 8090 on IP 182.74.215.86 and we want to connect to this server via proxy 104.236.147.107:56220.
Our client code is
this.socket = require('socket.io-client').connect(url,
{
// This line ensures that each client connection will have different client.id
'force new connection': true,
});
this.socket.once('connect', function()
{
})
.on('disconnect', function()
{
})
.on('error', function(e)
{
console.error("(storetalk.js) ASL: Unable to connect to garuda gateway:- " + e);
});
I have solved this issue using NGinx.
Installed nginx on my red hat server on ip 192.168.10.238
My node application server is running on same ip on port 8090
configured nginx to listen on 8020 and configured it with to support socket.io
my nodejs client is connected to application server via this nginx proxy
Below is my /etc/nginx/nginx.conf configuration file
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events
{
worker_connections 1024;
}
http
{
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
map $http_upgrade $connection_upgrade
{
default upgrade;
'' close;
}
server
{
listen 8020;
location /
{
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://192.168.10.238:8090;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
}

Can't hide location's port with nginx

I'm trying to set up a domain for my node project with nginx (v1.5.11), i have succesfull redirected the domain to the web, but i need to use 3000 port, so now, my web location looks like http://www.myweb.com:3000/ and of course, i want to keep only "www.myweb.com" part like this: http://www.myweb.com/
I have search and try many configurations but no one seems to work for me, i dont know why, this is my local nginx.conf file, i want to change http://localhost:8000/ text to http://myName/ text, remember that the redirect is working, i only want to "hide" the port on the location.
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 8000;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://localhost:8000/;
proxy_redirect http://localhost:8000/ http://myName/;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
pd. I'm trying to fix it on my local windows 8 machine, but if other OS is required, my remote server works on Ubuntu 12.04 LTS
Thanks you all.
Add this to your server block:
port_in_redirect off;
E.g.
server {
listen 80;
server_name localhost;
port_in_redirect off;
}
Documentation reference.
You should also change server_name to myName. server_name should be your domain name.
You should also be listening on port 80, and then use proxy_pass to redirect to whatever is listening on port 8000.
The finished result should look like this:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.myweb.com;
location / {
proxy_pass http://localhost:8000/;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
Comments were removed for clarity.
Hiding the port during proxying needs these two lines in server body:
server_name_in_redirect off;
proxy_set_header Host $host:$server_port;
The conf is like:
server
{
listen 80;
server_name example.com;
server_name_in_redirect off;
proxy_set_header Host $host:$server_port;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://localhost:8080;
}
access_log off;
}

Setup of nginx with node.js

I have setup nginx as a front end to an node.js app.
My nginx conf is:
worker_processes 1;
error_log /tmp/logs/error.log;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
access_log /tmp/logs/access.log;
sendfile on;
keepalive_timeout 65;
include /etc/nginx/sites-enabled/*;
# BELOW IS THE PART TO PROXY MY NODE.JS APP
upstream node_entry {
server unix:/tmp/express.sock
fail_timeout=0;
}
server {
listen 8888;
client_max_body_size 4G;
server_name localhost;
keepalive_timeout 5;
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://node_entry;
}
}
}
My node.js app is:
express = require('express');
app = express.createServer();
app.get('/test', function(req, res){
res.send('TEST');
});
app.listen('/tmp/express.sock');
When I issue a:
curl -XGET 'http://localhost:8888/test'
I get an error instead of proxying to my node.js app.
Any idea ?
I'm doing something similar, but it's all on one host, and I'm using a predefined port number that nginx and node both know (though I'd rather use your way if you can get it working).
Does it work if you have node listen on a specific port, and proxy_pass to http://127.0.0.1:{that_port}? (assuming both are on the same server...)

Resources