What is the problem with this configuration nginx as the reverse agent of Node.js?

I have a personal domain name running on a VPS. I want to set nginx as a reverse proxy for node.js applications, but it doesn’t work. Anyone can take a look My configuration and tell me what I did wrong?

We assume that my domain name is example.com. Basically, I want to do this so that when I go to node.example.com, it proxy node.js application. I also Blog.example.com and www.example.com are set up in nginx.

This is the nginx configuration of my reverse proxy (blog.example.com, www.example.com settings are omitted) :

server {
listen 80;
server_name node.example.com;

access_log /srv/www/example .com/logs/node-access.log;
error_log /srv/www/example.com/logs/node-error.log;

location / {
proxy_pass http ://example.com:3000/;
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;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffers 32 4k;
}
}

This is my node.js application:

var http = require('http');
http.createServer(function (req, res ) {
res.writeHead(200, {'Content-Type':'text/plain'});
res.end('Hello World ');
}). listen(3000, "example.com");

I restarted the nginx server and ran the node.js application. However, if I go to node.example.com, it It will say “node.example.com does not exist or is not available”.

I am not sure what is wrong with my configuration. I have also tried various combinations.

These are mine Tried configuration:

proxy_pass in nginx | ​​hostname in node.js app
http:// localhost:3000/ | ---.listen(3000, "localhost")
http:// 127.0.0.1:3000/ | ---.listen(3000, "127.0.0.1")
http:// node.example.com:3000/ | ---.listen(3000, "node.example.com")

I also tried the following nginx configuration:

upstream nodeapp {
server 127.0.0.1:3000;
}

server {
...
location / {
proxy_pass http:/ / nodeapp;
...
}
...
}

It doesn’t work either. What am I doing wrong ? I have searched the Internet for hours and tried various methods, but none of them seem to work.

If anyone can help me, I really appreciate it.

Thank you!

In the nginx configuration (proxy_pass), you must delete (http://) and (your Hostname) space in the URL:

You wrote:

proxy_pass http:// nodeapp;

You must write :

proxy_pass http://nodeapp;

I tried it on my server and added a space after http://.. and restarted nginx but nginx failed!
So, I think this may be your nginx problem!
Try to delete this space, I hope to cooperate with you!

Good luck!

I have a personal domain name running on a VPS. I want to set nginx as a reverse proxy for node.js applications, but it doesn’t work. Anyone Can you look at my configuration and tell me what I did wrong?

We assume that my domain name is example.com. Basically, I want to do this so that when I go to node.example.com, it proxy node.js application. I also Blog.example.com and www.example.com are set up in nginx.

This is the nginx configuration of my reverse proxy (blog.example.com, www.example.com settings are omitted) :

server {
listen 80;
server_name node.example.com;

access_log /srv/www/example .com/logs/node-access.log;
error_log /srv/www/example.com/logs/node-error.log;

location / {
proxy_pass http ://example.com:3000/;
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;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffers 32 4k;
}
}

This is My node.js application:

var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type':'text/plain'});
res.end('Hello World ');
}).listen(3000, " example.com");

I restarted the nginx server and ran the node.js application. However, if I go to node.example.com, it will say “node. example.com does not exist or is not available”.

I am not sure what is wrong with my configuration. I have also tried various combinations.

These are the configurations I have tried:

proxy_pass in nginx | ​​hostname in node.js app
http:// localhost:3000/ | ---.listen(3000, "localhost")< br />http:// 127.0.0.1:3000/ | ---.listen(3000, "127.0.0.1")
http:// node.example.com:3000/ | ---.listen (3000, "node.example.com")

I also tried the following nginx configuration:

upstream nodeapp {
server 127.0.0.1:3000;
}

server {
...
location / {
proxy_pass http:// nodeapp;
...
}
...
}

It doesn’t work either. What am I doing wrong? I have searched the Internet for hours and tried various methods, but none of them seem to work.

If anyone can help me, I really appreciate it.

Thank you!

In the nginx configuration (proxy_pass), you must delete the space in the URL between (http://) and (your hostname):

You wrote:

proxy_pass http:// nodeapp;

You must write:

proxy_pass http:/ /nodeapp;

I tried on my server and added a space after http://.. and restarted nginx but nginx failed!
So, I think this may be your nginx problem!
Try to delete this space, I hope to cooperate with you!

Good luck!

Leave a Comment

Your email address will not be published.