Nginx configuration on a centos 6.7 - linux

Im working on a centos 6.7 machine and I’m trying to configure nginx to serve a node.js application. I feel like I’m really close but I’m missing something. So heres my nginx.conf and below that is my server.conf thats in my sites-enabled directory.
When I go to the public IP address it gives me a 502 bad gateway error. But if I curl the private IP with the correct port on my centos machine I can see the node application running. What am I missing here? is it a firewall issue or maybe something else?
nginx.conf
user nginx;
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;
include /etc/nginx/sites-enabled/*;
}
sites-enabled/server.conf
server {
listen 80;
#server_name localhost;
location / {
proxy_pass http://192.xxx.x.xx:8000; // private IP
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;
}
}
UPDATE:
I figured this out! heres the server block that worked for me
server {
listen 80 default_server;
listen [::]:80 default_server;
#server_name _;
root /usr/share/nginx/html;
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://127.0.0.1:9000;
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;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}

I want to write a comment but stack overflow does not let me do it.
I am 99% sure that Node.js website does NOT need to work with nginx or apache.
If the script setup correctly, the Node.js Application should listen to the port by itself.
Since you did not really say much of your structure, I guess you can just try to access through the public IP with the port of Node.js.

Related

Random/Intermittent 502 gateway errors with nginx and node deployments using proxy_pass on a k8s cluster

My current configuration is as follows
Bare metal cluster. SELINUX status is off.
A nginx deployment
A nodejs deployment
I am serving static content through the ngnix service and the dynamic content using the node service. Below is my nginx configuration.
worker_processes 4;
#error_log logs/error.log info;
error_log /dev/stdout info;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
tcp_nopush on;
#keepalive_timeout 0;
#keepalive_timeout 5;
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 /dev/stdout main;
server {
listen 1080;
server_name localhost $hostname;
root /usr/share/nginx/static/;
# static content
location ~ some-regex {
alias /usr/share/nginx/static/;
# handle cors see 'NGINX-Cookbook' for production quality
add_header 'Access-Control-Allow-Origin' '*';
}
# forward request to node-service
location / {
client_max_body_size 128M;
proxy_buffer_size 256k;
proxy_buffers 4 512k;
proxy_busy_buffers_size 512k;
proxy_http_version 1.1;
# proxy_set_header Connection "";
# proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $http_host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# proxy_socket_keepalive on;
proxy_pass http://nodeserver:3000;
}
}
include servers/*;
}
In my ingress I am hitting the nginx service. I am able to forward my requests correctly to node service few times and get the proper response but around 50% of the time the request fails with a 502 bad gateway error and I see this error in the nginx pod logs
[error] 20#20: *187 connect() failed (111: Connection refused) while
connecting to upstream, client: 10.44.0.2, server: localhost, request:
"GET /path HTTP/1.1", upstream:
"http://node-service-clusterip:3000/path", host:
"my-nginx-node.example.com"
I have tried multiple directives from the nginx documentation but to no avail. Any help would be much appreciated
There was a mistake in my kubernetes label selectors. I was using same selectors for multiple deployments which caused issues in the routing.

POST Error when running multiple SocketIO endpoints behind nginx

I am trying to run multiple socketio endpoints behind a nginx proxy. My first socket site is at the endpoint '/'. This one works fine. The second socketio endpoint is at '/red/'. This appears to work. The socketio client in my html
var socket = io.connect('http://www.vagrantdevhost.com:8080', {path:'/red/'});
socket.on('connect', function() {
socket.emit('connectevent', {data: 'I\'m connected!'});
});
appears to connect properly. My connect event is called. However, I get the following errors in my console.
POST http://www.vagrantdevhost.com:8080/red/?EIO=3&transport=polling&t=MKUdom- 405 (METHOD NOT ALLOWED) index.js:83
Any explanations? I'm on day three of this and would appreciate any advice. I have included my nginx config.
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /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 {
listen 8080 default_server;
listen [::]:8080 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location /socket.io {
proxy_pass http://127.0.0.1:8092/socket.io;
proxy_redirect off;
proxy_buffering off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
location / {
proxy_pass http://127.0.0.1:8092;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /red/ {
proxy_pass http://127.0.0.1:8093/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
Usually these error messaged are caused by the limitation that Nginx can't serve static content on POST request. So, there is a hack in this post where you redirect 405 to 200.
check here: POST request not allowed - 405 Not Allowed - nginx, even with headers included
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 404 /404.html;
error_page 403 /403.html;
# To allow POST on static pages
error_page 405 =200 $uri;
# ...
}

Getting Nginx to serve more than one virtual server for node apps

So, I am having a really hard time right now. I have two nodejs applications. One is running on port 8080, and one on 8081. They are both running on the same ip addresses. I have two domains, domain1.com and domain2.com. I am using Nginx as a reverse proxy to redirect domain1.com to port 8080 and domain2.com to port 8081. My problem at the moment is that domain1.com is the only on that works. I can only access the other node app by going to domain1.com:8081 or domain2.com:8081.
My nginx file structure:
domain1.com.conf:
server {
listen 80;
server_name domain1.com www.domain1.com;
location / {
proxy_pass http://127.0.0.1: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;
}
}
domain2.com.conf:
server {
listen 80;
server_name domain1.com www.domain1.com;
location / {
proxy_pass http://127.0.0.1:8081;
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.conf:
user nginx;
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;
}
Any help would be greatly appreciated. I have been racking my brain a this for so long, and I can not find many relevant answers online.
BTW, I a running all of this on CentOS 6.3
UPDATE: after troubleshooting some more. I discovered that my problem might not be an nginx problem because I completely shut the nginx service down and my node app was still displaying. It is weird because nothing is running on port 80. I even used the netstat command to check if anything was running on port 80. I am so confused right now. If anyone has any idea on how to fix this or how to troubleshoot further. Please let me know.
I'm not an nginx expert but this kind of setup works for me:
upstream www.domain1.com {
server 127.0.0.1:8080;
}
server {
listen 80;
server_name domain1.com,www.domain1.com;
location / {
proxy_pass http://www.domain1.com;
}
}
# same for domain2
I don't know how or why this worked, but restarting my server seemed to solve this issue. I still haven't the slightest clue what was causing this, but the power flickered at my house, my server restarted, and everything worked fine with the configuration I had to start. Thanks to anyone who tried to help. I am marking ShanShan's answer as the correct one since his/her configuration is valid and works fine.

CentOS/Nginx server isn't working, hosting Node apps

I have a pretty simple server setup that isn't working for some reason.
I have two apps running locally, one on port 1999 and the other on port 8000.
I have disabled Apache, and have nginx installed. Here's my nginx.conf in /etc/nginx/:
user nginx;
worker_processes 8;
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;
server_names_hash_bucket_size 64;
include /etc/nginx/conf.d/*.conf;
}
And here's my default.conf in /etc/nginx/conf.d/ :
server {
listen 80;
server_name attendahh.com;
location / {
proxy_pass http://localhost:1999;
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;
}
}
server {
listen 80;
server_name threadfinder.net;
location / {
proxy_pass http://localhost:8000;
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;
}
}
Navigating to http://my-ip-address:1999 and http://my-ip-address:8000 works just fine, but going to my domains does not.
I get the feeling that NGINX just plain isn't working, and maybe something in my hosts file/something else is messing things up. Any ideas on what steps I can take to work this out? This is a fresh install of NGINX.
EDIT: Also, when I try to access threadfinder.net and attendahh.com nothing appears in my access logs at /var/log/nginx/access.log
Well, do you have problem with DNS?

Node.js + Nginx - What now?

I've set up Node.js and Nginx on my server. Now I want to use it, but, before I start there are 2 questions:
How should they work together? How should I handle the requests?
There are 2 concepts for a Node.js server, which one is better:
a. Create a separate HTTP server for each website that needs it. Then load all JavaScript code at the start of the program, so the code is interpreted once.
b. Create one single Node.js server which handles all Node.js requests. This reads the requested files and evals their contents. So the files are interpreted on each request, but the server logic is much simpler.
It's not clear for me how to use Node.js correctly.
Nginx works as a front end server, which in this case proxies the requests to a node.js server. Therefore you need to set up an Nginx config file for node.
This is what I have done in my Ubuntu box:
Create the file yourdomain.example at /etc/nginx/sites-available/:
vim /etc/nginx/sites-available/yourdomain.example
In it you should have something like:
# the IP(s) on which your node server is running. I chose port 3000.
upstream app_yourdomain {
server 127.0.0.1:3000;
keepalive 8;
}
# the nginx server instance
server {
listen 80;
listen [::]:80;
server_name yourdomain.example www.yourdomain.example;
access_log /var/log/nginx/yourdomain.example.log;
# pass the request to the node.js server with the correct headers
# and much more can be added, see nginx config options
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://app_yourdomain/;
proxy_redirect off;
}
}
If you want Nginx (>= 1.3.13) to handle websocket requests as well, add the following lines in the location / section:
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
Once you have this setup you must enable the site defined in the config file above:
cd /etc/nginx/sites-enabled/
ln -s /etc/nginx/sites-available/yourdomain.example yourdomain.example
Create your node server app at /var/www/yourdomain/app.js and run it at localhost:3000
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(3000, "127.0.0.1");
console.log('Server running at http://127.0.0.1:3000/');
Test for syntax mistakes:
nginx -t
Restart Nginx:
sudo /etc/init.d/nginx restart
Lastly start the node server:
cd /var/www/yourdomain/ && node app.js
Now you should see "Hello World" at yourdomain.example
One last note with to starting the node server: you should use some kind of monitoring system for the node daemon. There is an awesome tutorial on node with upstart and monit.
You can also setup multiple domain with Nginx, forwarding to multiple node.js processes.
For example to achieve these:
domain1.example -> to Node.js process running locally http://127.0.0.1:4000
domain2.example -> to Node.js process running locally http://127.0.0.1:5000
These ports (4000 and 5000) should be used to listen the app requests in your app code.
/etc/nginx/sites-enabled/domain1
server {
listen 80;
listen [::]:80;
server_name domain1.example;
access_log /var/log/nginx/domain1.access.log;
location / {
proxy_pass http://127.0.0.1:4000/;
}
}
In /etc/nginx/sites-enabled/domain2
server {
listen 80;
listen [::]:80;
server_name domain2.example;
access_log /var/log/nginx/domain2.access.log;
location / {
proxy_pass http://127.0.0.1:5000/;
}
}
You can also have different URLs for apps in one server configuration:
yourdomain.example/app1/* -> to Node.js process running locally
http://127.0.0.1:3000
yourdomain.example/app2/* -> to Node.js process
running locally http://127.0.0.1:4000
In /etc/nginx/sites-enabled/yourdomain:
server {
listen 80;
listen [::]:80;
server_name yourdomain.example;
location ^~ /app1/{
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://127.0.0.1:3000/;
}
location ^~ /app2/{
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://127.0.0.1:4000/;
}
}
Restart Nginx:
sudo service nginx restart
Starting applications.
node app1.js
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello from app1!\n');
}).listen(3000, "127.0.0.1");
console.log('Server running at http://127.0.0.1:3000/');
node app2.js
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello from app2!\n');
}).listen(4000, "127.0.0.1");
console.log('Server running at http://127.0.0.1:4000/');
I proxy independent Node Express applications through Nginx.
Thus new applications can be easily mounted and I can also run other stuff on the same server at different locations.
Here are more details on my setup with Nginx configuration example:
Deploy multiple Node applications on one web server in subfolders with Nginx
Things get tricky with Node when you need to move your application from from localhost to the internet.
There is no common approach for Node deployment.
Google can find tons of articles on this topic, but I was struggling to find the proper solution for the setup I need.
Basically, I have a web server and I want Node applications to be mounted to subfolders (i.e. http://myhost/demo/pet-project/) without introducing any configuration dependency to the application code.
At the same time I want other stuff like blog to run on the same web server.
Sounds simple huh? Apparently not.
In many examples on the web Node applications either run on port 80 or proxied by Nginx to the root.
Even though both approaches are valid for certain use cases, they do not meet my simple yet a little bit exotic criteria.
That is why I created my own Nginx configuration and here is an extract:
upstream pet_project {
server localhost:3000;
}
server {
listen 80;
listen [::]:80;
server_name frontend;
location /demo/pet-project {
alias /opt/demo/pet-project/public/;
try_files $uri $uri/ #pet-project;
}
location #pet-project {
rewrite /demo/pet-project(.*) $1 break;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $proxy_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://pet_project;
proxy_redirect http://pet_project/ /demo/pet-project/;
}
}
From this example you can notice that I mount my Pet Project Node application running on port 3000 to http://myhost/demo/pet-project.
First Nginx checks if whether the requested resource is a static file available at /opt/demo/pet-project/public/ and if so it serves it as is that is highly efficient, so we do not need to have a redundant layer like Connect static middleware.
Then all other requests are overwritten and proxied to Pet Project Node application, so the Node application does not need to know where it is actually mounted and thus can be moved anywhere purely by configuration.
proxy_redirect is a must to handle Location header properly. This is extremely important if you use res.redirect() in your Node application.
You can easily replicate this setup for multiple Node applications running on different ports and add more location handlers for other purposes.
From: http://skovalyov.blogspot.dk/2012/07/deploy-multiple-node-applications-on.html
Node.js with Nginx configuration.
$ sudo nano /etc/nginx/sites-available/subdomain.your-domain.example
add the following configuration so that Nginx acting as a proxy redirect to port 3000 traffic from the server when we come from subdomain.your_domain.example
upstream subdomain.your-domain.example {
server 127.0.0.1:3000;
}
server {
listen 80;
listen [::]:80;
server_name subdomain.your-domain.example;
access_log /var/log/nginx/subdomain.your-domain.access.log;
error_log /var/log/nginx/subdomain.your-domain.error.log debug;
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://subdomain.your-domain.example;
proxy_redirect off;
}
}
I made a repository in Github which you can clone, vagrant-node-nginx-boilerplate
basically the node.js app at /var/www/nodeapp is
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(4570, '127.0.0.1');
console.log('Node Server running at 127.0.0.1:4570/');
and the nginx config at /etc/nginx/sites-available/ is
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/nodeapp;
index index.html index.htm;
server_name localhost;
location / {
proxy_pass http://127.0.0.1:4570;
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;
}
}
answering your question 2:
I would use option b simply because it consumes much less resources. with option 'a', every client will cause the server to consume a lot of memory, loading all the files you need (even though i like php, this is one of the problems with it). With option 'b' you can load your libraries (reusable code) and share them among all client requests.
But be ware that if you have multiple cores you should tweak node.js to use all of them.
Nginx can act as a reverse proxy server which works just like a project manager. When it gets a request it analyses it and forwards the request to upstream(project members) or handles itself. Nginx has two ways of handling a request based on how its configured.
serve the request
forward the request to another server
server{
server_name mydomain.example sub.mydomain.example;
location /{
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_pass_request_headers on;
}
location /static/{
alias /my/static/files/path;
}
}
Server the request
With this configuration, when the request URL is
mydomain.example/static/myjs.js it returns the myjs.js file in
/my/static/files/path folder. When you configure Nginx to serve
static files, it handles the request itself.
forward the request to another server
When the request URL is mydomain.example/dothis Nginx will forwards the
request to http://127.0.0.1:8000. The service which is running on the
localhost 8000 port will receive the request and returns the response
to Nginx and Nginx returns the response to the client.
When you run node.js server on the port 8000 Nginx will forward the request to node.js. Write node.js logic and handle the request. That's it you have your nodejs server running behind the Nginx server.
If you wish to run any other services other than nodejs just run another service like Django, flask, PHP on different ports and config it in Nginx.
You could also use node.js to generate static files into a directory served by nginx. Of course, some dynamic parts of your site could be served by node, and some by nginx (static).
Having some of them served by nginx increases your performance..
We can easily setup a Nodejs app by Nginx acting as a reverse proxy.
The following configuration assumes the NodeJS application is running on 127.0.0.1:8080,
server{
server_name domain.example sub.domain.example; # multiple domains
location /{
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_pass_request_headers on;
}
location /static/{
alias /absolute/path/to/static/files; # nginx will handle js/css
}
}
in above setup your Nodejs app will,
get HTTP_HOST header where you can apply domain specific logic to serve the response. '
Your Application must be managed by a process manager like pm2 or supervisor for handling situations/reusing sockets or resources etc.
Setup an error reporting service for getting production errors like sentry or rollbar
NOTE: you can setup logic for handing domain specific request routes, create a middleware for expressjs application
The best and simpler setup with Nginx and Nodejs is to use Nginx as an HTTP and TCP load balancer with proxy_protocol enabled. In this context, Nginx will be able to proxy incoming requests to nodejs, and also terminate SSL connections to the backend Nginx server(s), and not to the proxy server itself. (SSL-PassThrough)
In my opinion, there is no point in giving non-SSL examples, since all web apps are (or should be) using secure environments.
Example config for the proxy server, in /etc/nginx/nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
upstream webserver-http {
server 192.168.1.4; #use a host port instead if using docker
server 192.168.1.5; #use a host port instead if using docker
}
upstream nodejs-http {
server 192.168.1.4:8080; #nodejs listening port
server 192.168.1.5:8080; #nodejs listening port
}
server {
server_name example.com;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header Connection "";
add_header X-Upstream $upstream_addr;
proxy_redirect off;
proxy_connect_timeout 300;
proxy_http_version 1.1;
proxy_buffers 16 16k;
proxy_buffer_size 16k;
proxy_cache_background_update on;
proxy_pass http://webserver-http$request_uri;
}
}
server {
server_name node.example.com;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
add_header X-Upstream $upstream_addr;
proxy_redirect off;
proxy_connect_timeout 300;
proxy_http_version 1.1;
proxy_buffers 16 16k;
proxy_buffer_size 16k;
proxy_cache_background_update on;
proxy_pass http://nodejs-http$request_uri;
}
}
}
stream {
upstream webserver-https {
server 192.168.1.4:443; #use a host port instead if using docker
server 192.168.1.5:443; #use a host port instead if using docker
}
server {
proxy_protocol on;
tcp_nodelay on;
listen 443;
proxy_pass webserver-https;
}
log_format proxy 'Protocol: $protocol - $status $bytes_sent $bytes_received $session_time';
access_log /var/log/nginx/access.log proxy;
error_log /var/log/nginx/error.log debug;
}
Now, let's handle the backend webserver.
/etc/nginx/nginx.conf:
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
load_module /etc/nginx/modules/ngx_http_geoip2_module.so; # GeoIP2
events {
worker_connections 1024;
}
http {
variables_hash_bucket_size 64;
variables_hash_max_size 2048;
server_tokens off;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
autoindex off;
keepalive_timeout 30;
types_hash_bucket_size 256;
client_max_body_size 100m;
server_names_hash_bucket_size 256;
include mime.types;
default_type application/octet-stream;
index index.php index.html index.htm;
# GeoIP2
log_format main 'Proxy Protocol Address: [$proxy_protocol_addr] '
'"$request" $remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
# GeoIP2
log_format main_geo 'Original Client Address: [$realip_remote_addr]- Proxy Protocol Address: [$proxy_protocol_addr] '
'Proxy Protocol Server Address:$proxy_protocol_server_addr - '
'"$request" $remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'$geoip2_data_country_iso $geoip2_data_country_name';
access_log /var/log/nginx/access.log main_geo; # GeoIP2
#===================== GEOIP2 =====================#
geoip2 /usr/share/geoip/GeoLite2-Country.mmdb {
$geoip2_metadata_country_build metadata build_epoch;
$geoip2_data_country_geonameid country geoname_id;
$geoip2_data_country_iso country iso_code;
$geoip2_data_country_name country names en;
$geoip2_data_country_is_eu country is_in_european_union;
}
#geoip2 /usr/share/geoip/GeoLite2-City.mmdb {
# $geoip2_data_city_name city names en;
# $geoip2_data_city_geonameid city geoname_id;
# $geoip2_data_continent_code continent code;
# $geoip2_data_continent_geonameid continent geoname_id;
# $geoip2_data_continent_name continent names en;
# $geoip2_data_location_accuracyradius location accuracy_radius;
# $geoip2_data_location_latitude location latitude;
# $geoip2_data_location_longitude location longitude;
# $geoip2_data_location_metrocode location metro_code;
# $geoip2_data_location_timezone location time_zone;
# $geoip2_data_postal_code postal code;
# $geoip2_data_rcountry_geonameid registered_country geoname_id;
# $geoip2_data_rcountry_iso registered_country iso_code;
# $geoip2_data_rcountry_name registered_country names en;
# $geoip2_data_rcountry_is_eu registered_country is_in_european_union;
# $geoip2_data_region_geonameid subdivisions 0 geoname_id;
# $geoip2_data_region_iso subdivisions 0 iso_code;
# $geoip2_data_region_name subdivisions 0 names en;
#}
#=================Basic Compression=================#
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/css text/xml text/plain application/javascript image/jpeg image/png image/gif image/x-icon image/svg+xml image/webp application/font-woff application/json application/vnd.ms-fontobject application/vnd.ms-powerpoint;
gzip_static on;
include /etc/nginx/sites-enabled/example.com-https.conf;
}
Now, let's configure the virtual host with this SSL and proxy_protocol enabled config at /etc/nginx/sites-available/example.com-https.conf:
server {
real_ip_header proxy_protocol;
set_real_ip_from 192.168.1.1; #proxy server ip address
#set_real_ip_from proxy; #proxy container hostname if you are using docker
server_name 192.168.1.4; #Your current server ip address. It will redirect to the domain name.
listen 80;
listen 443 ssl http2;
listen [::]:80;
listen [::]:443 ssl http2;
ssl_certificate /etc/nginx/certs/example.com.crt;
ssl_certificate_key /etc/nginx/certs/example.com.key;
ssl_dhparam /etc/nginx/ssl/dhparam.pem;
return 301 https://example.com$request_uri;
}
server {
real_ip_header proxy_protocol;
set_real_ip_from 192.168.1.1; #proxy server ip address
#set_real_ip_from proxy; #proxy container hostname if you are using docker
server_name example.com;
listen *:80;
return 301 https://example.com$request_uri;
}
server {
real_ip_header proxy_protocol;
set_real_ip_from 192.168.1.1; #proxy server ip address
#set_real_ip_from proxy; #proxy container hostname if you are using docker
server_name www.example.com;
listen 80;
listen 443 http2;
listen [::]:80;
listen [::]:443 ssl http2 ;
ssl_certificate /etc/nginx/certs/example.com.crt;
ssl_certificate_key /etc/nginx/certs/example.com.key;
ssl_dhparam /etc/nginx/ssl/dhparam.pem;
return 301 https://example.com$request_uri;
}
server {
real_ip_header proxy_protocol;
set_real_ip_from 192.168.1.1; #proxy server ip address
#set_real_ip_from proxy; #proxy container hostname if you are using docker
server_name example.com;
listen 443 proxy_protocol ssl http2;
listen [::]:443 proxy_protocol ssl http2;
root /var/www/html;
charset UTF-8;
add_header Strict-Transport-Security 'max-age=31536000; includeSubDomains; preload';
add_header X-Frame-Options SAMEORIGIN;
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";
add_header Referrer-Policy no-referrer;
ssl_prefer_server_ciphers on;
ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH";
ssl_protocols TLSv1.2 TLSv1.1 TLSv1;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
keepalive_timeout 70;
ssl_buffer_size 1400;
ssl_dhparam /etc/nginx/ssl/dhparam.pem;
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8 8.8.4.4 valid=86400;
resolver_timeout 10;
ssl_certificate /etc/nginx/certs/example.com.crt;
ssl_certificate_key /etc/nginx/certs/example.com.key;
ssl_trusted_certificate /etc/nginx/certs/example.com.crt;
location ~* \.(jpg|jpe?g|gif|png|ico|cur|gz|svgz|mp4|ogg|ogv|webm|htc|css|js|otf|eot|svg|ttf|woff|woff2)(\?ver=[0-9.]+)?$ {
expires modified 1M;
add_header Access-Control-Allow-Origin '*';
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
access_log off;
}
location ~ /.well-known { #For issuing LetsEncrypt Certificates
allow all;
}
location / {
index index.php;
try_files $uri $uri/ /index.php?$args;
}
error_page 404 /404.php;
location ~ \.php$ {
try_files $uri =404;
fastcgi_index index.php;
fastcgi_pass unix:/tmp/php7-fpm.sock;
#fastcgi_pass php-container-hostname:9000; (if using docker)
fastcgi_pass_request_headers on;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
fastcgi_ignore_client_abort off;
fastcgi_connect_timeout 60;
fastcgi_send_timeout 180;
fastcgi_read_timeout 180;
fastcgi_request_buffering on;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
include fastcgi_params;
}
location = /robots.txt {
access_log off;
log_not_found off;
}
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
}
And lastly, a sample of 2 nodejs webservers:
First server:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello From Nodejs\n');
}).listen(8080, "192.168.1.4");
console.log('Server running at http://192.168.1.4:8080/');
Second server:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello From Nodejs\n');
}).listen(8080, "192.168.1.5");
console.log('Server running at http://192.168.1.5:8080/');
Now everything should be perfectly working and load-balanced.
A while back I wrote about How to set up Nginx as a TCP load balancer in Docker. Check it out if you are using Docker.
You can run nodejs using pm2 if you want to manage each microservice means and run it. Node will be running in a port right just configure that port in Nginx (/etc/nginx/sites-enabled/domain.example)
server{
listen 80;
server_name domain.example www.domain.example;
location / {
return 403;
}
location /url {
proxy_pass http://localhost:51967/info;
}
}
Check weather localhost is running or not by using ping.
And
Create one single Node.js server which handles all Node.js requests. This reads the requested files and evals their contents. So the files are interpreted on each request, but the server logic is much simpler.
This is best and as you said easier too

Resources