I am having issues redirecting my port 80 to 4000 where my nodejs application is running on my Digital Ocean Ubuntu Droplet. Here is my /etc/nginx/sites-available/default file:
server {
listen 80;
server_name my_site.com;
location / {
proxy_pass http://MY_IP_ADDRESS:4000;
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;
}
}
When ever I head over to my droplet's IP on port 4000 the application loads successfully but not when I try on port 80.
You are doing a proxy_pass of port :8080 but you said your application is running on port 4000.
You need to tell nginx the correct port to proxy, (4000 instead of 8080)
Then you need to restart the nginx service sudo service nginx restart
Related
I have a node.js app and I am running nginx as a reverse proxy server. I want to access the same app through multiple ways.
Let the ip address of machine be 12.16.12.113.
Then I want the same app to be accessible through:
12.16.12.113:3000
test.example.com
I have 2 Configuration files for nginx in sites-available directory:
file1:
server {
listen 80;
server_name test.example.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;
}
}
file2:
server {
listen 3000;
server_name default_server;
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;
}
}
The problem is that if I link both the files in sites-enabled directory, then only the app is accessible only through 12.16.12.113:3000. If I remove file2, then it is accessible through test.example.com.
But I want both the methods to work.
Edit 1:
Based on suggestion from this answer, I did the following test:
It appears that nginx is not listening to port 80. Why is this happening?
The are few ways to achieve that.
You can make your Node app listen on 12.16.12.113:3000 instead of on localhost:3000 and then you will need nginx only for port 80.
You can make your Node app listen on localhost:3000 (but only on localhost) and then proxy 12.16.12.113:3000 and port 80 by nginx.
You can make your Node app listen on localhost:4000 or 0.0.0.0:4000 on any interface and use nginx to proxy requests to port 80 and 3000.
But you will not be able to make them both listen on the same port and the same interface without problems.
This is the only advice that could be given considering the fact that you didn't include any code example of how your Node app is binding to the ports, which would be the only relevant information here.
I am having troubles setting up NodeJs to work together with Nginx.
I have added forward of the subdomain to an IP address of my server. (when I ping subdomain it gives me the server IP address).
This is my config in /etc/nginx/sites-enabled/default
server {
listen 80;
listen [::]:80 default_server ipv6only=on;
server_name chat.mydomainname.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;
}
}
My NodeJs server is running on port 3000 and I can access it via browser successfully on url http://MYIPADDRESS:3000
I noticed when I restart nginx server (service nginx restart) that I don't get any output that I usually should get.
I have tried to check what is on port 80 and does something blocks nginx from running. I killed those processes (which were all nginx) and I started it again, but no luck.
I am using Ubuntu 16.04 on Digital Ocean. (I have added subdomain on DigitalOcean also)
Can someone see some obvious mistake here?
Thanks!
I want to use nginx on different port. If i am running nginx on default port(80) and trying to use by the url (100.100.7.60) this is working fine.
server {
listen 80;
server_name 100.100.7.60;
location / {
proxy_pass http://100.100.7.60: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;
}
}
But when i am changing nginx port 80 to 8080 or to any port. If now i am trying by (100.100.7.60). I am unable to run node.js application. Now if i want to run application my url should be like (100.100.7.60:8080).
server {
listen 8080;
server_name 100.100.7.60;
location / {
proxy_pass http://100.100.7.60: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;
}
}
Can anyone explain me what is problem? I want to change port number. But i do not want to add (:8080) in url. I want to make same url after changing port number
If you want to listen on a different port, you cannot exclude the port number from the URL. When you enter 100.100.7.60 into a browser, the protocol is HTTP and the port is 80 by default.
There is a (very bad) workaround to this:
server {
listen 80;
server_name 100.100.7.60;
return 301 http://100.100.7.60:8080;
}
This will redirect ALL traffic on port 80 to port 8080, which is (I assume) not what you want.
Instead, you should configure your DNS to point to this server using a subdomain eg myapp.example.com and then listen on 80 port with that server name to serve your app.
I have a server with two two Single Page Application (using a framework) running using node http-server:
website1 running on port 80: IP_ADDRESS:80
website2 running on port 8080: IP_ADDRESS:8080
Currently the workflow is using this two commands to deploy the two sites
pm2 start /usr/bin/http-server -f --name website1 -- -p 80 -d false
pm2 start /usr/bin/http-server -f --name website2 -- -p 8080 -d false
At the end, our sites run on domain like this:
subdomain.mysite.com for website1
subdomain.mysite.com:8080 for website2.
This is not desired, we wanted like this:
subdomain.mysite.com for website1
subdomain2.mysite.com for website2
I tried to install nginx, with the follow reverse proxy configurations:
server {
listen 80;
server_name subdomain2.mysite.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;
}
}
Hoping that now, if I type subdomain2.mysite.com, it will bring me to the website2 on port 8080, but it didn't, it brings me to website1 on port 80 instead. In fact, I cant be sure if this reverse proxy is working at all.
I am sure I configured something wrongly, what could be the problem?
p/s: I also wonder if I am doing it entirely wrong - if I am using nginx for reverse proxy, should I stop using http-server directly?
Node and Nginx can't both listen on port 80 at the same time. If you want to reverse proxy both you'll need to use a different port for the first Node app and add another virtual host to your nginx file for it.
For example, changing the first app's port to 8000 would look like:
pm2 start /usr/bin/http-server -f --name website1 -- -p 8000 -d false
pm2 start /usr/bin/http-server -f --name website2 -- -p 8080 -d false
With the following Nginx configuration:
server {
listen 80;
server_name subdomain2.mysite.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;
}
}
server {
listen 80;
server_name subdomain1.mysite.com;
location / {
proxy_pass http://127.0.0.1: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;
}
}
You probably also want to add the proxy_redirect off; flag to each block.
I am using a DigitalOcean VPS hosting a meteor app. I don't have a domain name yet, so just use the plain IP address. When I set below config and use myipaddress:3000 and myipaddress:8080, both of them worked well; but if I change the 8080 to 80, only myipaddress:3000 works. Using only myipaddress or myipaddress:80 will show "Welcome to nginx on Debian!" message. (I use Ubuntu 14.04 on the VPS).
server {
listen 8080;
server_name default;
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;
}
}
Can not figure out why can't use port 80.
---- Solved this problem --------
I commented out the "listen 80 default_server" in the /etc/nginx/sites-enabled/default" file, then my config at "/etc/nginx/conf.d/mysite.conf" works on port 80.
You probably still have the default.conf still in the directory that nginx is using to serve up the sites. either that or check in nginx.conf. Somewhere there is a server setup already using 80 that is being served first.