I have a vm linux machine access from my uni and I have to deploy simple nodejs app to it. I can ssh the vm machine and everything and I have installed Nginx on it but whenever i try to run a simple "hello world" node app and access it from my browser at home i get redirected to other sites online or i get no response.
This is my Nginx config file
# the nginx server instance
server {
listen 80;
listen [::]:80;
server_name fff.com www.fff.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_fff/;
proxy_redirect off;
}
}
and my hello world app
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/');
I run the app and i can curl it from another ssh but when i go to fff.com i either get site can't be reached or get redirected to a normal website.
What am i doing wrong??? How can I type host this stupid app on my uni linux vm and access it from my browser at home??
Related
I have purchased AWS linux 2 server to host My Web application
I have published My Web node js application and want to run on default IP
I have configured the below things in /etc/nginx/sites-available/migrate.test.com
# the IP(s) on which your node server is running. I chose port 3000.
upstream migrate.test.com {
server privateIPaddress:3000;
keepalive 8;
}
# the nginx server instance
server {
listen 80;
listen [::]:80;
server_name migrate.test.com;
access_log /var/log/nginx/migrate.test.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_read_timeout 3600;
proxy_pass http://migrate.test.com/;
proxy_redirect off;
}
}
```````
**My app.js code**
```````
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(3000, "privateIPaddress");
console.log('Server running success');
``````
please check by browser output
[output][1]
[1]: https://i.stack.imgur.com/1BD5X.png
Specify the port 3000 that your node application is listening on as well, since the default is 80 for http -
proxy_pass http://migrate.test.com:3000;
Also, if your nginx server is running on the same machine as your node application server, you can also do -
proxy_pass http://localhost:3000;
https://docs.nginx.com/nginx/admin-guide/web-server/reverse-proxy/
I'm trying to point my nodejs app to nginx so i can access the node.js app with my domain name, been trying for hours but nothing works, the name server are setup correctly, in fact i can access my nodejs app if i type in url the ip address of my server + the port
e.g.
192.168.1.1:3000
browser displays... Hello world!
but when i go to my domain name
e.g.
mydomainname.com
browser displays... 502 Bad Gateway nginx/1.18.0
server.js
...
//routes
app.use("/", require("./routes/web"));
app.use("/api", require("./routes/api"));
app.use(function(req, res, next) {
res.status(404).send("Error 404");
});
const http = require('http');
const hostname = '192.168.1.1';
const port = 3000;
const server = http.createServer(app);
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
...
/etc/nginx/conf.d/domainname.com
server {
listen 80;
server_name mydomainname.com;
location / {
proxy_pass http://192.168.1.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;
}
}
for other folders and config files like sites-enabled/default is configure correctly and etc/nginx/nginx.conf is configured correctly since if i run
sudo nginx -t there's no error and the syntax is ok.
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Please Help, all tutorials and videos about this issue are way back from 2014-2018, there's nothing from 2019 or 2020
i have an Nginx server with the following configuration:
server
{
listen 80;
server_name example.ca www.example.ca;
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;
}
}
and on the node server am listening to port 3000 using node express as follow;
app.get('*', function (req, res) {
console.log(' request received ', req.headers.host);
});
and i have a domain www.example.com which is pointing to the Nginx server IP, the test am doing is:
http://www.example.com/test result ===> request received
www.example.com
http://www.example.com/test/abc result ===> request received
www.example.com
http://www.example.com result ===> ( i get no result !! its like the
app.get did not triggered )
so can any one help me in this issue ? when i browse the domain without any path parameter am not getting any result, so its like the Nginx didn't forward this request to the node server !!
First of all when you browse abc.com using browser or curl it will always send the path as /. There is no way to send a blank path
Next your nginx config is of a socket.io implementation. It should be below
server
{
listen 80;
server_name example.ca www.example.ca;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Host $host;
}
}
Next you are not ending your requests they will be just timing out. Change your code to below
app.get('*', function (req, res) {
console.log(' request received ', req.headers.host);
res.status(200).send("all ok");
});
Also when you have issues you can test it using curl like below
curl -v http://www.example.com
It redirects everyone including my.personal.ip.address but it's only supposed to redirect people besides me to the blog. It was working when I put it in the default nginx file but now that I'm trying to get my node app to run, I'm unsure where to put the if statement and why it won't work.
# the IP(s) on which your node server is running. I chose port 3000.
upstream mywebsite {
server 127.0.0.1:3000;
keepalive 8;
}
# the nginx server instance
server {
listen 0.0.0.0:80;
server_name mywebsite.ca mywebsite;
access_log /usr/share/nginx/html/yourdomain.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://mywebsite/;
if ($remote_addr != my.personal.ip.address){
rewrite ^ http://blog.mywebsite.ca;
}
proxy_redirect off;
}
}
Is your node app listening on 127.0.0.1, too ?
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(3000, "127.0.0.1");
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(9000, "127.0.0.1");
console.log('Server running at http://127.0.0.1:9000/');
I have the above code to get started with nodejs, when I start the process and run on a browser I get the response Once, but after that I dont get any response. Everytime I restart I get 1 response and as always it stops. How can I get this is run continuously. Thanks in advance!
Just adding more information related to this issue. Here is a snippet from the nginx conf file
server {
listen 80;
client_max_body_size 2M;
server_name my_domain;
root /home/node/My_Folder;
# access_log /var/log/nginx.vhost.access.log main;
send_timeout 1;
location ~* ^.+\.(jpg|jpeg|JPG|JPEG|GIF|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|mov|html)$ {
autoindex on;
root /home/node/My_Folder;
expires 30d;
break;
}
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_redirect off;
#proxy_connect_timeout 50ms;
#proxy_send_timeout 200ms;
#proxy_read_timeout 200ms;
proxy_next_upstream error;
proxy_pass http://Handler;
#index no_ads.html no_ads.htm;
break;
}
}
upstream Handler {
server 127.0.0.1:8010;
server 127.0.0.1:8011;
server 127.0.0.1:8012;
server 127.0.0.1:8013;
server 127.0.0.1:8014;
server 127.0.0.1:8015;
server 127.0.0.1:8016;
server 127.0.0.1:8017;
server 127.0.0.1:8018;
server 127.0.0.1:8019;
server 127.0.0.1:9000;
}
I tried using both
node app.js
forever start -a app.js
to start the app, but either ways I get just one response and then a time-out. I do have a couple of other node apps running on the same server and those seem to be working fine. So I am totally lost
Your Node.js application runs on port 9000.
Inside your NGinx configuration file, you have the setting
proxy_pass http://Handler;
which shall redirect the incoming requests to the Node.js applicaton, but you are not directly redirecting the requests there, but to an upstream that is configured as follows:
upstream Handler {
server 127.0.0.1:8010;
server 127.0.0.1:8011;
server 127.0.0.1:8012;
server 127.0.0.1:8013;
server 127.0.0.1:8014;
server 127.0.0.1:8015;
server 127.0.0.1:8016;
server 127.0.0.1:8017;
server 127.0.0.1:8018;
server 127.0.0.1:8019;
server 127.0.0.1:9000;
}
As NGinx by default uses round-robin for upstreams that means that in one of eleven times NGinx tries to connect to port 9000 (which works), and the next ten times tries to access a server that does not exist.
Hence no connection can be made, and you'll get the error message.
Remove all the other server entries within the upstream block, remove the upstream block entirely and configure the single Node.js server directly as proxy, or start additional Node.js servers using the ports 8010, 8011, ..., and everything should work.
For details on how to configure upstreams, please have a look at the NGinx documentation on the HttpUpstreamModule.