Nginx works as a front end server, which in this case proxies the requests to a node.js server. Therefore you need to setup an nginx config file for node.
This is what I have done in my Ubuntu box:
Create the file yourdomain.com
at /etc/nginx/sites-available/
:
vim /etc/nginx/sites-available/yourdomain.com
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.com www.yourdomain.com;
access_log /var/log/nginx/yourdomain.com.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.com yourdomain.com
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.com
One last note with regards 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.
There is a very important difference between the root
and the alias
directives. This difference exists in the way the path specified in the root
or the alias
is processed.
In case of the root
directive, full path is appended to the root including the location part, whereas in case of the alias
directive, only the portion of the path NOT including the location part is appended to the alias.
To illustrate:
Let's say we have the config
location /static/ {
root /var/www/app/static/;
autoindex off;
}
In this case the final path that Nginx will derive will be
/var/www/app/static/static
This is going to return 404
since there is no static/
within static/
This is because the location part is appended to the path specified in the root
. Hence, with root
, the correct way is
location /static/ {
root /var/www/app/;
autoindex off;
}
On the other hand, with alias
, the location part gets dropped. So for the config
location /static/ {
alias /var/www/app/static/;
autoindex off; ↑
} |
pay attention to this trailing slash
the final path will correctly be formed as
/var/www/app/static
In a way this makes sense. The alias
just let's you define a new path to represent an existing "real" path. The location part is that new path, and so it gets replaced with the real path. Think of it as a symlink.
Root, on the other hand is not a new path, it contains some information that has to be collated with some other info to make the final path. And so, the location part is used, not dropped.
The case for trailing slash in alias
There is no definitive guideline about whether a trailing slash is mandatory per Nginx documentation, but a common observation by people here and elsewhere seems to indicate that it is.
A few more places have discussed this, not conclusively though.
https://serverfault.com/questions/376162/how-can-i-create-a-location-in-nginx-that-works-with-and-without-a-trailing-slas
https://serverfault.com/questions/375602/why-is-my-nginx-alias-not-working
Best Answer
I am breaking down your problem into the small task, as per question and my understanding.
1) The error clearly says that you have not install Lua-nginx-module properly.
Lua-nginx-module documentation
2) The server does not have access to the internet so cannot download from git. *
Assuming you are doing ssh into your machine from windows. So, please check below link to copy the files from windows to Linux.
Installing/Accessing via WinSCP
how-to-copy-files-from-one-machine-to-another-using-ssh
this step will get all the necessary files on your server.
3) Steps to install nginx with lua-nginx-module.
lua nginx module compatibility check.
referance document for nginx compatibility
Prerequisites
- Centos/RHEL[In case if internet is working in your server].
- Downloading .rpm package manually and installing.
Search the prerequisites from the RPM resource site
Copy the file in your Linux box
Install with the following command.
Install-rpm-file-on-linux
- Tarball installation for prerequisites.
Downloading the source
Extracting
Building LuaJIT
To build Nginx with LuaJIT, we need to build LuaJIT first. This is as simple as a make command
Building Nginx
Syntanx Check
Nginx Lua Testing
*As per you nginx file.
Reload / restart nginx
how-to-reload-nginx-systemctl-or-nginx-s