I am trying to setup an nginx server in front of my nodejs server(with express).
This is my server config for nginx :
server {
listen 8080;
server_name localhost:8080;
root /usr/share/nginx/appHtml;
autoindex on;
location / {
proxy_pass http://localhost:3000;
}
}
My node server verifies the user and redirects the request to http://localhost:3000/u/user. Something like :
app.get('/',function(req,res){
//verify user
res.redirect('/u/user');
})
I want my window location to read localhost:8080/u/user instead of just localhost:8080 after this redirect as is the case when i am running my nodejs server without nginx.
Similarly i want the same behaviour for all my server redirects.
How do i achieve this?
Related
I deploy first time my app in a VPS with a new domain.
Till now my project axios url was 'http://localhost:5000/posts'
My node server is running in 5000 port and i use proxy in react to listen to5000 and not 3000.
So, how do i transfer thoses urls to my machine ? do i have to use my new server IP or example.com domain to axios url ?
I am able to understand that you want to have react and nginx application running on same server under same IP and you have react application running on 3000 port and nodejs on 5000.
You can have following configuration in your default.conf
server {
root /var/www/html;
server_name _;
listen 3000;
# this is the react application endpoints
location / {
try_files $uri $uri/ /index.html;
}
}
In your react application, use the api endpoint as http://127.0.0.1:5000/posts.
So now, react application will be dealt by NGINX and api by nodejs listening on 5000.
Node js part:
app.listen(4000,"localhost");
Nginx default:
server{
server_name "mydomain";
location /{
proxy_pass http://localhost:4000;
}
}
When i set it with localhost it's working.
When i run the node script on another server and set nginx configuration like this:
server{
server_name "mydomain";
location /{
proxy_pass http://192.168.1.30:4000;
}
}
This is not working. I'm getting 502 Bad Gateway error when i try to connect "mydomain".
I've solved the problem with changing the node script.
app.listen(4000,"localhost"); to app.listen(4000);
I'm having a little bit of trouble rerouting my server:80 to my node js app running in port 9999.
I have my nginx setup like so:
server {
listen 80;
server_name mydomain.com;
location / {
proxy_pass http://principal-domain.com:9999/;
}
location ~/\.ht {
deny all;
}
}
But obviously only / is being rerouted. If I make a request to /api I get a 502 Bad Gateway.
Anyone knows how to fix this? Apply the rerouting for all routes in mydomain.com?
I am using Nginx to point a subdomain to a different port that a node.js server is listening to.
It works fine for http, but now I need to switch over to https.
This is what I have right now in sites-available/default:
server {
listen 80;
listen 443;
server_name sub.example.com;
location / {
proxy_pass http://example.com:2222;
}
}
Now that I am switching my node server over to https do I need to change the proxy_pass to https://example.com:2222?
Now that I am switching my node server over to https do I need to change the proxy_pass to https://example.com:2222?
Short answer is no. It doesn't need to be same protocol for proxy as for incoming request
But you may require another directive
proxy_set_header X-Forwarded-Proto https;
I found this in my searches.
Turns out it's best to handle the SSL certification with nginx and leave node running a http server.
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.