I'm currently running into some configuration problems with NGINX where I keep getting a 502 error instead of NGINX falling back onto a different directory if either the server is down or the directory doesn't exist.
I'm running a Node.js application on port :3000, have SSL set up, and have all HTTP requests redirect to HTTPS. Given the scenario where my node.js application is offline, I wish to send the client to the default NGINX root directory /usr/share/nginx/html index index.htm index.html if possible.
I'm trying to have the nodejs application on port 3000 be shown on / but in the case that the server is down, to fallback on NGINX default directory and display the index.html in there instead. Can anyone help or guide me through this process?
Thank you
Edit: I've tried jfriend00 said in the comments, but now my proxy_pass doesn't seem to work. It would now default to the 500.html regardless whether my server is running or not. I've attached my nginx.conf file, would appreciate any help.
events {
worker_connections 1024;
}
http {
upstream nodejs {
server <<INTERNAL-PRIVATE-IP>>:3000; #3000 is the default port
}
...
server {
listen 80;
server_name <<PUBLIC-IP>>;
return 301 $scheme://<<DOMAIN>>$request_uri;
}
server {
listen 443;
ssl on;
server_name <<DOMAIN>>.com www.<<DOMAIN>>.com;
...
location / {
proxy_pass http://nodejs;
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 ;
proxy_set_header X-Forwarded-Proto https;
}
error_page 501 502 503 /500.html;
location = /500.html {
root /usr/share/nginx/html;
}
}
}
Adding the error_page works as I did above and it successfully kicks back. Thanks #jfriend00.
If you're deploying it to a live server, you might want to check this out since I had a hard time trying to figure out why my proxy_pass and my NGINX configuration wasn't working on CentOS deployed on EC2. It had nothing to do with the error_page.
Related
I'm currently in need of some help with this 404 issue I've been stuck on for days now. My system is running CentOS7 (CPanel, VPS) and it uses engintron for the nginx reverse proxy and pm2 to keep my next.js application running.
default.conf
server {
#listen 80 default_server;
listen 80;
server_name my.domain.name domain.name;
location / {
# reverse proxy for next server
proxy_pass http://127.0.0.1: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;
}
location /_next/static/ {
alias path/to/nextjs/static/folder;
expires 365d;
access_log off;
}
# deny all; # DO NOT REMOVE OR CHANGE THIS LINE - Used when Engintron is disabled to block Nginx from becoming an open proxy
# Set the port for HTTP proxying
set $PROXY_TO_PORT 8080;
include common_http.conf;
}
All the other files I haven't touched, so it's safe to rule them out as they're the default ones that come with engintron.
Connecting to my.domain.name, gives me this error in the console.
"/etc/nginx/html/index.html" is not found (2: No such file or directory), client: ::ffff:my.ip.address, server: my.domain.name, request: "GET / HTTP/2.0", host: "my.domain.name"
My best guess on why this problem occurs is because my domain/server can't receive the files or doesn't have permission through the proxy at all as getting the data at 127.0.0.1:3000 via curl http://127.0.0.1/3000 returns my site's code in full.
Any ideas on what could be causing this? Have an amazing rest of your day :)
Engintron can make proxying to Node apps easy. Have a look at the docs (engintron.com/docs) as well as the "custom rules" files in WHM > Plugins > Engintron.
As long as you load your Node site in your public_html folder for the given account (Apache cannot render it either way, so it's not an issue) and _next/static/ is inside that public_html folder (for symmetrical proxying), then the Node.js proxy rule you set in Engintron's custom rules (again see the docs) will be sufficient for your needs.
I would also recommend you revert any changes you did in Engintron's Nginx config files.
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.
im learning reverse proxy w/ nginx for the first time, and the following isnt working for me
im trying to reroute requests from http://localhost to an api server i have running at http://localhost:8080
server {
listen 80;
location / {
proxy_pass http://localhost:8080;
}
}
when i hit http://localhost, I simply get shown the welcome to nginx splash screen.
if i hit http://localhost:8080, i see my api
I have a node express service running at :8080, which i can hit manually, but shouldn't http://localhost be proxied there too?
When I setup a nginx domain that forwards requests to a node server, it looks like this, for the server_name, you can use localhost as a parameter for accessing it via localhost. You can also pass default_server to make this the default server config.
Note: Only one active config can contain default_server otherwise Nginx will throw errors.
Note: When using default_server, Nginx will catch localhost in that server config. Otherwise you need to specify localhost in the list of server_name's (separated by a space).
server {
# Setup the domain name(s)
server_name example.com;
listen 80 default_server;
# If you would like to gzip your stuff
gzip on;
gzip_min_length 1;
gzip_types *;
# Setup the proxy
# This will forward all requests to the server
# and then it will relay the servers response back to the client
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_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache_bypass $http_upgrade;
}
}
Found out that adding this to my nginx.conf fixes the issue:
listen [::]:80;
For some reason listen 80; doesn't catch my http://localhost requests.
I am new to nginx and I'm struggling to get my configuration for a reverse proxy working. I have a node app running on localhost:3010 and I'm trying to serve pages through nginx from this app at the subdomain dev.[sitename].org. Let's just say dev.example.org for readability. Here are the contents of a file I created in sites-available called example.org (is that the correct name for this file?):
server {
server_name www.example.org example.org;
}
upstream app_dev.example.org {
server 127.0.0.1:3010;
}
server {
listen 0.0.0.0:80;
server_name dev.example.org;
access_log /var/log/nginx/dev.example.access.log;
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_set_header X-NginX-Proxy true;
proxy_pass http://app_dev.example.org/;
proxy_redirect off;
}
}
This is mostly based off this related question: Node.js + Nginx - What now? however when I try to open dev.example.org in my browser, Chrome reports that it can't find the page. I can ping dev.example.org and get an IP address, so the server seems to be available, but my nginx configuration incorrect. I created the symlink in sites-enabled and restarted nginx, in case you thought I might have forgotten those steps.
So my thought now is that I'm not referring to the subdomain correctly somewhere, or maybe my file in sites-available is named wrong. Any push in the right direction would be appreciated.
Just to be sure the problem is on nginx try these steps:
Set a test server at port 3030, serving the system doc folder or anything else.
server {
listen 3030
location / {
root /usr/share/doc/;
autoindex on;
}
}
upstream simple_test {
server 127.0.0.1:3030
}
Then use simple_test below as well:
proxy_pass http://simple_test/;
If you see the /usr/share/doc dir listing when you access dev.example.org then your issue is on the node side.
Turned out something was blocking port 80! fixed that and the config as posted above worked
I'm using this buildpack to serve static files on Heroku with a node + nginx setup. While static assets are served properly, trying to serve content through node results in a 502 Bad Gateway. Node on its own works fine and so does nginx. The problem is when the two need to work together which I guess is because I haven't configured the nginx upstream settings right.
Here's my nginx conf:
worker_processes 1;
error_log /app/nginx/logs/error.log;
daemon off;
events {
worker_connections 1024;
}
http {
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
gzip on;
upstream node_conf {
server 127.0.0.1:<%= ENV['PORT'] %>;
keepalive 64;
}
server {
listen <%= ENV['PORT'] %>;
server_name localhost;
location / {
root html;
index index.html index.htm;
proxy_redirect off;
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_set_header X-NginX-Proxy true;
proxy_set_header Connection "";
proxy_http_version 1.1;
proxy_pass http://node_conf;
}
location ~* ^.+\.(jpg|gif|png|ico|css|js|html|htm)$ {
root /app;
access_log off;
expires max;
}
location /static {
root /app;
index index.html index.htm;
}
}
}
.
My _static.cfg:
SERVER_TYPE="nginx"
BUILD_WEB_ASSETS="true"
.
My node server:
var app = require( 'express ')()
app.get( '/', function(req, res) { res.send( 'This is from Node.' ) })
app.listen( process.env.PORT )
.
I also have a sample html file in /static to test if nginx works:
<html>This is from nginx.</html>
.
With this config, appname.herokuapp.com should display "This is from Node." but instead I get the 502.
appname.herokuapp.com/static displays "This is from nginx" as it should, so no problems with nginx and static content.
I have tried every combination of values for upstream in nginx server settings but none have worked. What else can I try to make nginx proxy requests to node?
Here's my Heroku Procfile in case it helps: web: bin/start_nginx
I am not really familiar with Heroku, and pretty new to Nginx, but I'll give it a shot: To me it looks like the Nginx-config is saying that Nginx and the node.js app are using the same port (<%= ENV['PORT'] %>).
What you want is Nginx to listen to incoming connections (usually port 80), and have it forward them to the node.js app. Here is an example Nginx config:
# the IP(s) on which your node server is running. I chose port 4000.
upstream xxx.xxx.xxx.xxx { #Your IP adress as seen from the internet
server 127.0.0.1:4000; #Your local node.js process
}
# the nginx server instance
server {
listen 0.0.0.0:80; #Have Nginx listen for all incoming connections on port 80
server_name my-site;
access_log /var/log/nginx/my-site.log;
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_set_header X-NginX-Proxy true;
proxy_pass http://xxx.xxx.xxx.xxx/; #Your IP adress as seen from the internet
proxy_redirect off;
}
}
This config is working for me on a webserver I am hosting in my living-room. Good luck!
Here's a README with information to get nginx and Node.js working together from a project I created a while back. Also included is an example nginx.conf.
As an overview, basically you're just creating sockets with Node then setting nginx to pipe those upstream. I commonly use this when I want to run multiple Node processes and have nginx stand in front of it.
It also includes working with socket.io out of the box, so you can use those to see how to configure your Node instance as well.