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);
Related
I am trying to run a react app with Node.js backend on the Nginx server.
Here's my server block in the nginx.conf file:
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
listen [::]:80;
server_name _;
root /usr/share/nginx/folder-name/frontend/build;
index index.html index.htm;
location / {
proxy_pass http://localhost:5000;
}
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
The build folder contains the compiled react js code(using npm run build)
Node.js backend is running on port 5000 using pm2/forever.
After restarting the Nginx Server, the react UI appears on the server IP but the UI is distorted.
Also, I am not able to access my backend APIs on MyIP/api/endpoint.
What am i doing wrong.? This nginx configuration was built from SO and other online resources so there's a huge probabilty that it could be wrong. Please help!
Thanks in advance!
The issue is you are setting the API proxy for the root (/). The correct one should look like this:
server {
listen 80;
listen [::]:80;
server_name _;
root /usr/share/nginx/folder-name/frontend/build;
index index.html index.htm;
location /api {
proxy_pass http://localhost:5000;
}
location / {
try_files $uri $uri/ =404;
}
}
If you don't have /api path in your Node.js, you will need a rewrite rule for it like this:
location /api {
rewrite ^/api/?(.*) /$1 break;
proxy_pass http://localhost:5000;
proxy_redirect off;
proxy_set_header Host $host;
}
I had experience that.Please check my image file
This configuration is running successfully on aws.
Your mistakes is proxy area. Please change like that.
location /api/ {
proxy_pass http://127.0.0.1:5000/api/
}
If you want, I can HELP you.
Yes, you can host both API and static files (build files of your front-end) on the same domain or host. Below, you can find a server block for a sample API hosted on port 3000 and static HTML files at a root location being served on port 80.
server {
listen 80;
server_name localhost;
location / {
root /var/www/html;
index index.html;
error_page 404 index.html;
}
location /api/ {
proxy_pass http://localhost:3000/;
}
}
You can access the front-end at http://localhost/<blah...> and the API at http://localhost/api/<blah...> (please note how /api is handled in the URL here and the server block above). Replace localhost with your domain name.
What am i doing wrong.?
One issue is with your proxy_pass directive. You are missing trailing slash /
...
location / {
proxy_pass http://localhost:5000/;
}
...
First, try this and share your result.
I have an nginx server with a reverse proxy pointing to my react app. The page loads fine and the links to different paths work fine when pressing through the page. However when I manually type out the url with path, I get a 404 error. Its the same path as via clicking links but I get a 404.
Here's my nginx conf file:
server{
listen 443 ssl;
listen [::]:443 ssl;
server_name sub.*;
location / {
proxy_pass http://127.0.0.1:8080;
}
// I tried adding an explicit path:
location /about{
proxy_pass http://127.0.0.1:8080/about;
}
//
// SSL Cert stuff here...
//
}
I have a similar issue with my nodejs on reverse proxy where the main url works fine but anything with a path returns a 502, but thats a separate question.
I found the solution. I had to update my nginx config to this:
server{
listen 443 ssl;
listen [::]:443 ssl;
server_name sub.*;
location / {
proxy_pass http://127.0.0.1:8080;
}
// I have to route each of my paths to my servers root
location /about{
proxy_pass http://127.0.0.1:8080;
}
location /contact{
proxy_pass http://127.0.0.1:8080;
}
// ...etc
//
// SSL Cert stuff here...
//
}
However this solution is a pain so I will be pursuing a different hosting strategy altogether.
When I setup simple nginx location to catch all traffic to root and proxy-pass to nodeJS on port 3000, when I request http://example.com/something
When I enter mydomain.net I got result from nodeJS, but when I tried example.com/something, I'm getting 502 Bad Gateway.
server {
listen xxx.xxx.xxx.xxx:80;
server_name example.com www.example.com;
location / {
proxy_pass http://xxx.xxx.xxx.xxx:3000/;
}
}
I expect nodeJS to handle rest of url (after /) as this working without nginx, when nodeJS is alone on port 80.
aste the following code in the file and make sure to change example.com to your domain (or IP), and 1337 to your Node.js application port:
server {
listen 80;
server_name example.com;
location / {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_pass "http://127.0.0.1:3000";
}
}
than
sudo nginx -t
sudo service nginx restart
I figure out that nginx code was ok, my SSL configuration seems to make problem here and after checked error log of nginx, started again and get successfully all locations to NodeJS. Thanks all.
How to configure multiple virtual hosted nodejs apps on same machine with different port to nginx conf? I tried location tag with different proxy_pass but it throws no css js error on one location and on another it’s get method error..
Please help me out. thanks!!
You can do it simple like this for instance:
server {
listen 80;
server_name yourdomain.com;
location / {
proxy_pass http://localhost:3000;
}
}
server {
listen 80;
server_name yourotherdomain.com;
location / {
proxy_pass http://localhost:3001;
}
}
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?