Nginx docker as nodejs proxy in local - node.js
At the moment I am using nginx in docker to proxy my node applications
for example I add website.dev in my host file
and my actual nginx config is
worker_processes 4;
pid /run/nginx.pid;
events {}
http {
server {
listen 80;
server_name website.dev;
location / {
proxy_pass http://localnode:3000;
proxy_set_header X-Base-Path "/";
proxy_set_header Website-Name "test";
}
}
}
And I a starting my docker container with this command
docker run --name infra-nginx --add-host localnode:$(ifconfig | grep inet | grep -v inet6 | grep -v 127.0.0.1 | awk '{print $2}') -p 80:80 -d docker-registry.host.com:5000/infra-nginx:dev
Where
$(ifconfig | grep inet | grep -v inet6 | grep -v 127.0.0.1 | awk '{print $2}') is getting my local ip, not working all the time.
The problem is if I want to work without internet, I can't.
And when my ip change, I have to restart the container with the new ip.
I tried with this config instead
worker_processes 4;
pid /run/nginx.pid;
events {}
http {
server {
listen 80;
server_name localhost;
location / {
return 200 'gangnam style!';
}
}
server {
listen 80;
server_name website.dev;
location / {
proxy_pass http://localhost:3000;
proxy_set_header X-Base-Path "/";
proxy_set_header Website-Name "test";
}
}
}
And running
docker run --name infra-nginx --network host -d docker-registry.host.com:5000/infra-nginx:dev
In this case, when I am running:
curl http://website.dev/
I have
curl: (7) Failed to connect to website.dev port 80: Connection refused
The docker ps is giving
81da561dd131 ajouve/infra-nginx:dev "nginx -g 'daemon ..." 32 minutes ago Up 32 minutes infra-nginx
the netstat -plant is giving me
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1/nginx: master pro
tcp 0 0 172.17.0.1:35962 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 172.17.0.1:35938 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 172.17.0.1:35994 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 172.17.0.1:45080 172.17.0.5:6379 ESTABLISHED -
tcp 0 0 172.17.0.1:35990 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 192.168.65.2:44900 151.101.0.204:80 TIME_WAIT -
tcp 0 0 172.17.0.1:45126 172.17.0.5:6379 ESTABLISHED -
tcp 0 0 172.17.0.1:36000 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 172.17.0.1:35958 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 172.17.0.1:59172 172.17.0.3:5672 ESTABLISHED -
tcp 0 0 172.17.0.1:35976 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 172.17.0.1:59106 172.17.0.3:5672 ESTABLISHED -
tcp 0 0 172.17.0.1:35980 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 172.17.0.1:35996 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 172.17.0.1:58356 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 172.17.0.1:35966 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 172.17.0.1:45112 172.17.0.5:6379 ESTABLISHED -
tcp 0 0 172.17.0.1:35932 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 172.17.0.1:58366 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 172.17.0.1:35998 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 192.168.65.2:41386 206.251.255.63:80 TIME_WAIT -
tcp 0 0 172.17.0.1:58358 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 172.17.0.1:35956 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 172.17.0.1:35924 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 172.17.0.1:36004 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 172.17.0.1:58360 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 172.17.0.1:35964 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 172.17.0.1:35916 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 172.17.0.1:58362 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 172.17.0.1:59148 172.17.0.3:5672 ESTABLISHED -
tcp 0 0 172.17.0.1:59166 172.17.0.3:5672 ESTABLISHED -
tcp 0 0 172.17.0.1:35944 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 172.17.0.1:35912 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 172.17.0.1:35954 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 172.17.0.1:45116 172.17.0.5:6379 ESTABLISHED -
tcp 0 0 172.17.0.1:58354 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 172.17.0.1:35988 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 172.17.0.1:59122 172.17.0.3:5672 ESTABLISHED -
tcp 0 0 192.168.65.2:34936 5.153.231.4:80 TIME_WAIT -
tcp 0 0 192.168.65.2:44904 151.101.0.204:80 TIME_WAIT -
tcp 0 0 172.17.0.1:59162 172.17.0.3:5672 ESTABLISHED -
tcp 0 0 172.17.0.1:59180 172.17.0.3:5672 ESTABLISHED -
tcp 0 0 172.17.0.1:45130 172.17.0.5:6379 ESTABLISHED -
tcp 0 0 172.17.0.1:59140 172.17.0.3:5672 ESTABLISHED -
tcp 0 0 172.17.0.1:36002 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 172.17.0.1:35922 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 172.17.0.1:35970 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 172.17.0.1:58364 172.17.0.4:27017 ESTABLISHED -
tcp6 0 0 :::6379 :::* LISTEN -
tcp6 0 0 :::15672 :::* LISTEN -
tcp6 0 0 :::5672 :::* LISTEN -
tcp6 0 0 :::27017 :::* LISTEN -
From the docker container:
curl -v localhost
Is giving
* Rebuilt URL to: localhost/
* Hostname was NOT found in DNS cache
* Trying ::1...
* connect to ::1 port 80 failed: Connection refused
* Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.38.0
> Host: localhost
> Accept: */*
>
< HTTP/1.1 200 OK
* Server nginx/1.10.3 is not blacklisted
< Server: nginx/1.10.3
< Date: Fri, 29 Sep 2017 08:46:10 GMT
< Content-Type: text/plain
< Content-Length: 14
< Connection: keep-alive
<
* Connection #0 to host localhost left intact
gangnam style!
But curl -v website.dev is returning
* Rebuilt URL to: website.dev/
* Hostname was NOT found in DNS cache
* Trying 127.0.0.1...
* Connected to website.dev (127.0.0.1) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.38.0
> Host: website.dev
> Accept: */*
>
< HTTP/1.1 502 Bad Gateway
* Server nginx/1.10.3 is not blacklisted
< Server: nginx/1.10.3
< Date: Fri, 29 Sep 2017 08:46:37 GMT
< Content-Type: text/html
< Content-Length: 173
< Connection: keep-alive
<
<html>
<head><title>502 Bad Gateway</title></head>
<body bgcolor="white">
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx/1.10.3</center>
</body>
</html>
* Connection #0 to host website.dev left intact
And docker inspect infra-nginx
[
{
"Id": "16941d22442a257f0874a772df935514c658ac16ec67eb3f65606b4d7c0ee62e",
"Created": "2017-09-29T08:31:21.144827953Z",
"Path": "nginx",
"Args": [
"-g",
"daemon off;"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 2656,
"ExitCode": 0,
"Error": "",
"StartedAt": "2017-09-29T08:31:21.548119911Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:25d085baee52923e32f8d134048238fb67e71173e01f758c391119235f7fc565",
"ResolvConfPath": "/var/lib/docker/containers/16941d22442a257f0874a772df935514c658ac16ec67eb3f65606b4d7c0ee62e/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/16941d22442a257f0874a772df935514c658ac16ec67eb3f65606b4d7c0ee62e/hostname",
"HostsPath": "/var/lib/docker/containers/16941d22442a257f0874a772df935514c658ac16ec67eb3f65606b4d7c0ee62e/hosts",
"LogPath": "/var/lib/docker/containers/16941d22442a257f0874a772df935514c658ac16ec67eb3f65606b4d7c0ee62e/16941d22442a257f0874a772df935514c658ac16ec67eb3f65606b4d7c0ee62e-json.log",
"Name": "/infra-nginx",
"RestartCount": 0,
"Driver": "aufs",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "host",
"PortBindings": {},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "runc",
"ConsoleSize": [
0,
0
],
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": null,
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DeviceCgroupRules": null,
"DiskQuota": 0,
"KernelMemory": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": -1,
"OomKillDisable": false,
"PidsLimit": 0,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0
},
"GraphDriver": {
"Data": null,
"Name": "aufs"
},
"Mounts": [],
"Config": {
"Hostname": "moby",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"443/tcp": {},
"80/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"NGINX_VERSION=1.10.3-1~jessie"
],
"Cmd": [
"nginx",
"-g",
"daemon off;"
],
"ArgsEscaped": true,
"Image": "ajouve/infra-nginx:dev",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "175272649c9a9c5abbfde7516328bdab5cb3825e1e027eee0580eb18f7ff77cb",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/default",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "",
"Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"MacAddress": "",
"Networks": {
"host": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "3e04a4c12b5a5b3f55e7b4e918dadec64806b5c926fc249e8aa3e28398a02954",
"EndpointID": "7de54daaa31230c9492a463792015af727e9562eaacbaa0c2d70cdc3d3b04236",
"Gateway": "",
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "",
"DriverOpts": null
}
}
}
}
]
So when you run it using --net host it actually still is not on mac network as such. That is why it is not working.
From the documentation
The Mac has a changing IP address (or none if you have no network access). From 17.06 onwards our recommendation is to connect to the special Mac-only DNS name docker.for.mac.localhost which will resolve to the internal IP address used by the host.
So you need to change your config to
worker_processes 4;
pid /run/nginx.pid;
events {}
http {
server {
listen 80;
server_name localhost;
location / {
return 200 'gangnam style!';
}
}
server {
listen 80;
server_name website.dev;
location / {
proxy_pass http://docker.for.mac.localhost:3000;
proxy_set_header X-Base-Path "/";
proxy_set_header Website-Name "test";
}
}
}
And you should run the container as below
docker run --name infra-nginx -p 80:80 -d docker-registry.host.com:5000/infra-nginx:dev
You're making it way too complex. Just EXPOSE a port like 8080 from your app container.
In nginx use:
listen 80;
server_name _;
location / {
proxy_pass http://127.0.0.2:8080;
}
Ok I think the solution I did is simple enough.
First I have an app in nodejs running on the port 3000 on my local machine, so if I go to localhost:3000 in the browser I can see the app running.
Then I create a file called default.conf inside a folder in my machine ~/projects/docker/default.conf, you can create that file wherever you want. And paste this code inside the file:
server {
listen 80;
server_name myapp.com;
location / {
proxy_pass http://localhost: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;
}
}
Look the line proxy_pass http://localhost:3000; I am redirecting to my app running on my local machine.
Then I run a nginx container with the following command:
sudo docker run -d -p 80:80 --name="nginx" --net="host" -v ~/projects/docker/default.conf:/etc/nginx/conf.d/default.conf:ro nginx
The -p 80:80 is to bind that port to the nginx container
The --net="host" is to tell the container that the network it will use is the same that the host, it means the same as my machine, with this I can forward to the localhost:3000 from inside the container.
The -v ~/projects/docker/default.conf:/etc/nginx/conf.d/default.conf:ro
is to tell to the nginx that its configuration file will be the file I have in my local machine.
It means something like: You will find this /etc/nginx/conf.d/default.conf file in this ~/projects/docker/default.conf location
And that's all.
If I go to my browser and type just localhost (without the port) it will go through the nginx container and will be redirected to the app running in the localhost:3000 in my local machine
Let me know if this helps you
Related
Reverse proxy partially works on docker swarm
I setup a docker swarm with 3 nodes : s1 : manager + worker s2 : worker s3 : worker I deployed a nginx as a reverse proxy to a docker swarm service on each node with publishing port as mode=host to get the real ip. Nginx works "fine", i'am able to serve static content, use over https, etc ... The part which doesn't work is the reverse_proxy : if the nginx and the service are on the same node, everything works if the nginx and the service aren't one the same node, i can only GET / because others requests ( like /css/style.css ) will fails with 499 ( from nginx point ) nginx network is an overlay network swarm-scopped and ip forwarding is enabled. Here is my nginx configuration : server { listen 80; server_name service.foo.bar; location / { proxy_pass http://service:80; } } server { listen 443 ssl; server_name service.foo.bar; ssl_certificate /ssl/service.foo.bar/fullchain.pem; ssl_certificate_key /ssl/service.foo.bar/privkey.pem; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers HIGH:!aNULL:!MD5; location / { proxy_pass http://service:80; } } Here is how i deployed my nginx : docker service create --name nginx --mount /etc/nginx/nginx.conf:/etc/nginx/nginx.conf --mode=global --publish mode=host,published=80,target=80 --publish mode=host,published=443,target=443 --network nginx nginx If i curl the node who hosts the service : * TCP_NODELAY set * Connected to service.foo.bar port 80 (#0) > GET /app/polyfills.d92dcdb0a986e964fec8.js HTTP/1.1 > Host: service.foo.bar > User-Agent: curl/7.68.0 > Accept: */* > * Mark bundle as not supporting multiuse < HTTP/1.1 200 OK < Server: nginx/1.21.6 < Date: Mon, 13 Jun 2022 15:38:36 GMT < Content-Type: application/javascript < Content-Length: 257750 < Connection: keep-alive < cache-control: public, immutable, max-age=604800 < expires: Mon, 20 Jun 2022 15:38:36 GMT < permissions-policy: accelerometer=(), ambient-light-sensor=(), autoplay=(), camera=(), encrypted-media=(), fullscreen=(), geolocation=(), gyroscope=(), magnetometer=(), microphone=(), midi=(), payment=(), picture-in-picture=(), sync-xhr=(self "https://haveibeenpwned.com" "https://2fa.directory"), usb=(), vr=() < x-content-type-options: nosniff < x-frame-options: SAMEORIGIN < referrer-policy: same-origin < x-xss-protection: 0 < /*! For license information please see polyfills.d92dcdb0a986e964fec8.js.LICENSE.txt */ [...] If i curl a node which doesn't host the service : * TCP_NODELAY set * Connected to service.foo.bar port 80 (#0) > GET /app/polyfills.d92dcdb0a986e964fec8.js HTTP/1.1 > Host: service.foo.bar > User-Agent: curl/7.68.0 > Accept: */* > * Mark bundle as not supporting multiuse < HTTP/1.1 200 OK < Server: nginx/1.21.6 < Date: Mon, 13 Jun 2022 15:38:25 GMT < Content-Type: application/javascript < Content-Length: 257750 < Connection: keep-alive < cache-control: public, immutable, max-age=604800 < expires: Mon, 20 Jun 2022 15:38:25 GMT < permissions-policy: accelerometer=(), ambient-light-sensor=(), autoplay=(), camera=(), encrypted-media=(), fullscreen=(), geolocation=(), gyroscope=(), magnetometer=(), microphone=(), midi=(), payment=(), picture-in-picture=(), sync-xhr=(self "https://haveibeenpwned.com" "https://2fa.directory"), usb=(), vr=() < x-content-type-options: nosniff < x-frame-options: SAMEORIGIN < referrer-policy: same-origin < x-xss-protection: 0 < * transfer closed with 257750 bytes remaining to read * Closing connection 0 curl: (18) transfer closed with 257750 bytes remaining to read nginx log say : nginx.0.scembp2e9iqp#s3 | 2022/06/13 15:38:36 [warn] 23#23: *114 an upstream response is buffered to a temporary file /var/cache/nginx/proxy_temp/5/00/0000000005 while reading upstream, client: #ip, server: service.foo.bar, request: "GET /app/polyfills.d92dcdb0a986e964fec8.js HTTP/1.1", upstream: "http://10.0.4.56:80/app/polyfills.d92dcdb0a986e964fec8.js", host: "service.foo.bar" My nodes are connected each others over wireguard, this is my routing table : default via #ip dev ens3 #ip dev ens3 scope link 10.252.1.0/24 dev wg0 proto kernel scope link src 10.252.1.1 172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1 linkdown 172.19.0.0/16 dev docker_gwbridge proto kernel scope link src 172.19.0.1 Here is my wireguard configuration : [Interface] Address = 10.252.1.1/24 ListenPort = 51820 PrivateKey = *** [Peer] PublicKey = *** AllowedIPs = 10.252.1.2/32 Endpoint = #s2 [Peer] PublicKey = *** AllowedIPs = 10.252.1.3/32 Endpoint = #s3 This is my firewall configuration : *filter :INPUT ACCEPT [0:0] :FORWARD DROP [0:0] :OUTPUT ACCEPT [0:0] :FILTERS - [0:0] :DOCKER-USER - [0:0] -F INPUT -F DOCKER-USER -F FILTERS -A INPUT -i lo -j ACCEPT -A INPUT -j FILTERS -A DOCKER-USER -i ens3 -j FILTERS -A FILTERS -m state --state ESTABLISHED,RELATED -j ACCEPT -A FILTERS -p icmp --icmp-type echo-request -j ACCEPT -A FILTERS -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT -A FILTERS -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT -A FILTERS -m state --state NEW -m tcp -p tcp --dport 443 -j ACCEPT -A FILTERS -p udp --dport 51820 -j ACCEPT -A FILTERS -s 10.252.1.0/24 -j ACCEPT -A FILTERS -j REJECT --reject-with icmp-port-unreachable COMMIT Any ideas ? Am i missing something ?
How do I run a next application on port 80?
I'm trying to run a nextjs app on port 80 on arch linux. I'm getting a permission error even though I'm using sudo. It seems like no other app is using port 80? This is package.json: "scripts": { "dev": "next dev -p 80", "build": "next build", "start": "next start -p 80", "lint": "next lint" }, Now when i run sudo npm run dev I get this error message: Error: listen EACCES: permission denied 0.0.0.0:80 at Server.setupListenHandle [as _listen2] (node:net:1313:21) at listenInCluster (node:net:1378:12) at Server.listen (node:net:1465:7) at /home/pera/Desktop/projects/agency/frontend/node_modules/next/dist/server/lib/start-server.js:45:16 at new Promise (<anonymous>) at Object.startServer (/home/pera/Desktop/projects/agency/frontend/node_modules/next/dist/server/lib/start-server.js:19:12) at nextDev (/home/pera/Desktop/projects/agency/frontend/node_modules/next/dist/cli/next-dev.js:116:23) at /home/pera/Desktop/projects/agency/frontend/node_modules/next/dist/bin/next:130:34 { code: 'EACCES', errno: -13, syscall: 'listen', address: '0.0.0.0', port: 80 } Now when i run sudo netstat -tulpn tcp 0 0 127.0.0.1:6463 0.0.0.0:* LISTEN 17894/Discord --typ tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 585/mongod tcp 0 0 127.0.0.1:27060 0.0.0.0:* LISTEN 1350/steam tcp 0 0 127.0.0.53:53 0.0.0.0:* LISTEN 317/systemd-resolve tcp 0 0 0.0.0.0:27036 0.0.0.0:* LISTEN 1350/steam tcp 0 0 127.0.0.1:57343 0.0.0.0:* LISTEN 1350/steam tcp 0 0 127.0.0.1:9050 0.0.0.0:* LISTEN 438/tor tcp 0 0 0.0.0.0:5355 0.0.0.0:* LISTEN 317/systemd-resolve tcp 0 0 127.0.0.54:53 0.0.0.0:* LISTEN 317/systemd-resolve tcp6 0 0 :::5355 :::* LISTEN 317/systemd-resolve udp 0 0 0.0.0.0:35486 0.0.0.0:* 17894/Discord --typ udp 0 0 0.0.0.0:60196 0.0.0.0:* 17894/Discord --typ udp 0 0 0.0.0.0:44070 0.0.0.0:* 17894/Discord --typ udp 0 0 127.0.0.54:53 0.0.0.0:* 317/systemd-resolve udp 0 0 127.0.0.53:53 0.0.0.0:* 317/systemd-resolve udp 0 0 192.168.0.10:68 0.0.0.0:* 277/systemd-network udp 0 0 0.0.0.0:5355 0.0.0.0:* 317/systemd-resolve udp 0 0 0.0.0.0:27036 0.0.0.0:* 1350/steam udp6 0 0 :::5355 :::* 317/systemd-resolve This is the output. No app is open on port 80?
Google cloud engine external access issue
I'm new to Google cloud platform and I didn't understand why I cannot reach a node.js instance running on a new VM. Node is running on port 8084 through app.listen('8084', "0.0.0.0") Firewall rules are the following: gcloud compute firewall-rules list NAME NETWORK DIRECTION PRIORITY ALLOW DENY DISABLED default-allow-http default INGRESS 1000 tcp:80 False default-allow-https default INGRESS 1000 tcp:443 False default-allow-icmp default INGRESS 65534 icmp False default-allow-internal default INGRESS 65534 tcp:0-65535,udp:0-65535,icmp False default-allow-rdp default INGRESS 65534 tcp:3389 False default-allow-ssh default INGRESS 65534 tcp:22 False node-8084 default INGRESS 999 tcp:8084 False netstat: netstat -na | grep LISTEN tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:8084 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:34339 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:8998 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:65001 0.0.0.0:* LISTEN tcp6 0 0 :::970 :::* LISTEN tcp6 0 0 :::980 :::* LISTEN tcp6 0 0 :::22 :::* LISTEN tcp6 0 0 :::922 :::* LISTEN I can reach the VM via SSH (port 22) but not through http://35.206.91.238:8084/medical on Chrome. 35.206.91.238 is the external IP showed by google cloud console. Node.js presents no errors (and no requests). main code is var app = express(); app.get('/medical', function(request, response){ if( request.query.q ) run(request.query.q, function(results, queries) { parseResult(q, results, queries, response) } ); }) app.listen('8084', '0.0.0.0') console.log('Server started on port 8084'); exports = module.exports = app; Wireshark traffic is only Only SYN is passing, chrome says "35.206.91.238 refused to connect." Node is executed manually from shell and doesn't report any error after "Server started on port 8084". Any idea
Node.js server only listening on ipv6
I am running a node.js server on port 5403. I can telent to the private ip on this port but cannot telnet to the public ip on the same port. I assume the cause of this is because node.js is only listening on ipv6. This is the result of netstat -tpln (Not all processes could be identified, non-owned process info will not be shown, you would have to be root to see it all.) Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN - tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN - tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN - tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN - tcp6 0 0 :::5611 :::* LISTEN 25715/node tcp6 0 0 :::22 :::* LISTEN - tcp6 0 0 ::1:631 :::* LISTEN - tcp6 0 0 :::5403 :::* LISTEN 25709/node How do I make the node server listen on ipv4
You need to specify an IPV4 address when you call the listen(), I had the same issue with the http module. If I use this: var http = require('http'); var server = http.createServer(function(request, response) { ... }); server.listen(13882, function() { }); It only listen on IPV6, as you can see from netstat output: $ netstat -lntp Proto Recv-Q Send-Q Local Address Foreign Address State tcp6 0 0 :::13882 :::* LISTEN However, if I specify an IPV4 address like this: var http = require('http'); var server = http.createServer(function(request, response) { ... }); server.listen(13882, "0.0.0.0", function() { }); netstat will report the server as listening on IPV4: $ netstat -lntp Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 0 0.0.0.0:13882 0 0.0.0.0:13882 LISTEN I'm using Ubuntu 16.04 and npm 5.3.0. HTH
Can't connect to MongoDB from NodeJS on Ubuntu. Same code on OSX works fine
I have mongodb installed locally on my osx laptop and on a remote ubuntu server. Both have mongodb running and I can verify this using the http diagnostics on port 28017. I'm running the same code on both computers. On osx everything works fine, but on Ubuntu I can't make a connection to the database through NodeJS. I keep getting this error: Error: failed to connect to [localhost:27017]] message: listen EADDRNOTAVAIL stack: Error: listen EADDRNOTAVAIL at errnoException (net.js:769:11) at Server._listen2 (net.js:892:19) at listen (net.js:936:10) at Server.listen (net.js:993:9) at asyncCallback (dns.js:67:16) at Object.onanswer [as oncomplete] (dns.js:120:9) What I don't understand is that I can connect on Ubuntu locally via the mongo commandline interface. I can also connect to the database on Ubuntu via the mongo command on my OSX computer. So nothing seems to be wrong with the installation of MongoDB itself. Can anyone think of a reason why I can't connect via NodeJS? I have tried using the mongodb and mongoose packages. Both give me the same error. Here are the 2 ways I tried: var mongo = require("mongodb"); var host = "localhost"; var port = mongo.Connection.DEFAULT_PORT; var db = new mongo.Db('node-mongo-examples', new mongo.Server(host, port, {}), {}); db.open(function(err, db){ if(err){ log.error('MongoDB connection error:', err); }else{ log.info("OPEN MONGO CONNECTION"); } }); And the with mongoose: var mongoose = require('mongoose'); var db = mongoose.createConnection('localhost', 'node-mongo-examples'); db.on('error', function(err){ log.error('MongoDB connection error:', err); }); db.once('open', function () { log.debug("OPEN MONGO CONNECTION"); }); In the logs I see nothing special, and nothing happens either ***** SERVER RESTARTED ***** Wed Sep 26 18:00:18 [initandlisten] MongoDB starting : pid=13377 port=27017 dbpath=/var/lib/mongodb 64-bit host=octo-dev Wed Sep 26 18:00:18 [initandlisten] db version v2.2.0, pdfile version 4.5 Wed Sep 26 18:00:18 [initandlisten] git version: f5e83eae9cfbec7fb7a071321928f00d1b0c5207 Wed Sep 26 18:00:18 [initandlisten] build info: Linux ip-10-2-29-40 2.6.21.7-2.ec2.v1.2.fc8xen #1 SMP Fri Nov 20 17:48:28 EST 2009 x86_64 BOOST_LIB_VERSION=1_49 Wed Sep 26 18:00:18 [initandlisten] options: { config: "/etc/mongodb.conf", dbpath: "/var/lib/mongodb", logappend: "true", logpath: "/var/log/mongodb/mongodb.log" } Wed Sep 26 18:00:18 [initandlisten] journal dir=/var/lib/mongodb/journal Wed Sep 26 18:00:18 [initandlisten] recover : no journal files present, no recovery needed Wed Sep 26 18:00:18 [websvr] admin web console waiting for connections on port 28017 Wed Sep 26 18:00:18 [initandlisten] waiting for connections on port 27017 ..... except when I connect through the mongo commandline interface: Wed Sep 26 18:30:40 [initandlisten] connection accepted from 127.0.0.1:38229 #3 (1 connection now open) I ran out of things to try. Any suggestions for troubleshooting this? Some extra info sudo netstat -tulpn Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 13377/mongod tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN 885/mysqld tcp 0 0 0.0.0.0:1935 0.0.0.0:* LISTEN 1102/java tcp 0 0 0.0.0.0:9999 0.0.0.0:* LISTEN 1102/java tcp 0 0 192.87.219.76:10000 0.0.0.0:* LISTEN 31171/webserver tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 1387/java tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1076/apache2 tcp 0 0 0.0.0.0:28017 0.0.0.0:* LISTEN 13377/mongod tcp 0 0 0.0.0.0:48466 0.0.0.0:* LISTEN 12418/java tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 3507/sshd tcp 0 0 127.0.0.1:9016 0.0.0.0:* LISTEN 12418/java tcp 0 0 0.0.0.0:5080 0.0.0.0:* LISTEN 1102/java tcp 0 0 0.0.0.0:25 0.0.0.0:* LISTEN 1216/master tcp 0 0 0.0.0.0:41018 0.0.0.0:* LISTEN 12418/java tcp 0 0 0.0.0.0:8443 0.0.0.0:* LISTEN 1102/java tcp 0 0 0.0.0.0:9090 0.0.0.0:* LISTEN 12418/java tcp 0 0 0.0.0.0:29090 0.0.0.0:* LISTEN 12418/java tcp 0 0 127.0.0.1:8100 0.0.0.0:* LISTEN 8535/soffice.bin tcp 0 0 127.0.0.1:8005 0.0.0.0:* LISTEN 1387/java tcp 0 0 0.0.0.0:389 0.0.0.0:* LISTEN 887/slapd tcp 0 0 0.0.0.0:33736 0.0.0.0:* LISTEN 1102/java tcp6 0 0 :::22 :::* LISTEN 3507/sshd tcp6 0 0 :::389 :::* LISTEN 887/slapd udp 0 0 192.87.219.76:123 0.0.0.0:* 721/ntpd udp 0 0 127.0.0.1:123 0.0.0.0:* 721/ntpd udp 0 0 0.0.0.0:123 0.0.0.0:* 721/ntpd udp 0 0 0.0.0.0:5353 0.0.0.0:* 797/avahi-daemon: r udp 0 0 0.0.0.0:55248 0.0.0.0:* 797/avahi-daemon: r udp6 0 0 :::123 :::* 721/ntpd udp6 0 0 :::35920 :::* 797/avahi-daemon: r udp6 0 0 :::5353 :::* 797/avahi-daemon: r
Make sure port 27017 is opened to the web server in Ubuntu.
I found the problem!! The system had a messed up /etc/hosts file. Something with localhost configuration that was unusual. Correcting this file solved everything :D
Try firing the mongo command from your terminal.It will show an error if theres some problem with mongodb.