Setup of nginx with node.js - 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...)

Related

Express server not working on reverse proxy (NGINX)

I am testing my express server running on proxy. My nginx config file for in sites-available is:
server {
server_name open.yousico.com;
gzip on;
gzip_proxied any;
gzip_types application/javascript application/x-javascript text/css text/javascript;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_min_length 256;
location /_next/static/ {
alias /var/www/yousi-website/.next/static/;
expires 365d;
access_log off;
}
location / {
proxy_pass http://127.0.0.1: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;
}
location /api {
rewrite ^/codify(.*) $1 break;
proxy_pass "http://127.0.0.1:3001";
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/open.yousico.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/open.yousico.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = open.yousico.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name open.yousico.com;
return 404; # managed by Certbot
}
My server file for my express server listening on 3001 is:
const express = require('express');
const app = express();
app.listen(3001, console.log("server is running on 3001"));
app.get('/', (req, res) => {
res.send('API is running');
});
app.get('/hello', (req, res) => {
res.send('Hello World');
});
I tested this express server on my local machine, and it seems to be working fine, but when I deploy it to my cloud server, it displays "Cannot GET /api", is there something wrong with my config file? My understanding is that I set my location to /api and direct it to port 3001, and I'm sure that the server is running on 3001.
"Cannot XXX /YYY" is the Express default response when there is no handler for method XXX for path YYY.
In your case, I don't see an app.get("/api", ...) handler in your server code, so the error is quite warranted.
The rewrite statement in your nginx config,
rewrite ^/codify(.*) $1 break;
doesn't make sense either since there's no way for location /api to match /codify.
If you meant to strip the /api out before the URL is passed to your Express server,
rewrite ^/api(.*) $1 break;
in which case /api would be passed as /, and that would match your app.get("/")...

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

Nginx Reverse Proxy not working with Nodejs at localhost

I have a dedicated server with 5 usable IP address,say X.X.X.1 - X.X.X.5 below is my default schema for IP addresses
X.X.X.1 - ns1.ex.com - name server using BIND
X.X.X.2 - ns2.ex.com
X.X.X.3 - www.ex.com - using nginx as the web server
X.X.X.4 - nothing
X.X.X.5 - nothing
Now I am trying to do reverse proxying on nodejs(127.0.0.1:4501).... I have started the app and when I am trying to access the node app through reverse proxy it's not working. I have even tried to call curl like http://localhost:4501/ and also http://localhost:4501/test/ as the app.js is present at /var/www/test/app.js. I also tried changing the app ip addresses to X.X.X.3 but with no result
When I don't set the IP address of the node app then it is working on any port that I put in. I want to set the IP address to localhost so that it is only accessible through Nginx and I have my database that I also want to hide behind Nginx.
Below are my conf files:
nginx.conf: present at /usr/local/nginx/conf/nginx.conf
#===============================================================================
# Main Configuration Settings
#===============================================================================
user root admins;
worker_processes auto;
master_process on;
worker_rlimit_nofile 16384;
worker_priority 0;
#================================================================================
# Error Log Setting Goes HEre
#================================================================================
events {
multi_accept off;
worker_connections 5120;
}
http {
include mime.types;
default_type application/octet-stream;
open_file_cache max=10000 inactive=30s;
open_file_cache_errors on;
client_body_buffer_size 200M; #200 MB
log_not_found on; #LOG All 404 error code
log_format main '{'
' IP:"$remote_addr:$remote_port", Time:"$time_local", Request_Type:"$request", '
' Status:"$status", Referer:"$http_referer", '
' Agent:"$http_user_agent", Forwarded_By:"$http_x_forwarded_for" '
'}';
#===========================================
# Caching DNS records For 1 HR
#==========================================
resolver 8.8.8.8 8.8.4.4 valid=1h;
resolver_timeout 10s;
#sendfile on;
keepalive_timeout 10;
#=================================================================================
# Gzip Module
#=================================================================================
gzip on;
gzip_comp_level 4;
gzip_min_length 20;
gzip_vary on;
gzip_proxied any;
upstream localhost_servers {
server 127.0.0.1:4501;
keepalive 64;
}
server {
listen 80;
server_name www.ex.com ex.com;
charset UTF-8;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarder-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost_servers;
proxy_redirect off;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
app.js: at /var/www/test/app.js
var express = require('express');
var morgan = require('morgan');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
var app = express();
app.use(express.static(__dirname + '/public'));
app.use(morgan('dev'));
app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());
app.use(methodOverride());
app.get('/',function(req,res){
console.log(req.ip,req.host,req.path,req.originalUrl);
res.send(req.body);
});
app.listen('127.0.0.1',4501);
console.log('Magic happens on port 80');
It was a Very silly mistake from my side as pointed by #Ben Fortune express listen method takes port first and then ip address

How to use nginx proxy_pass subroutes from node app?

I have a node app running on port 8002 with different subroutes like '/login' or '/facebook', i also have nginx (v1.6.0) and the following config:
server {
listen 80;
server_name my-ghost-blog.com ;
client_max_body_size 10M;
location / {
proxy_pass http://localhost:2368/;
proxy_set_header Host $host;
proxy_buffering off;
}
location ~ ^/(sitemap.xml) {
root /var/www/ghost;
}
location ~ ^/(robots.txt) {
root /var/www/ghost;
}
#proxy to a node app running on 8002 port
location ^~ /auth/ {
proxy_pass http://localhost:8002/;
}
}
when i go to '/auth/' it works, but when i try to go to a node app'subroute, 404 appears because nginx dont know how to handle it.
any ideas?
thanks

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

Resources