nginx error 502 bad gateway error with proxy - linux

I recently uninstalled apache2 for Nginx
I'm trying to listen on 88, 808 and 888 for my sites and redirect different subdomains for each (and another domain to another server).
The other domain is going to another computer on my local network and the different ports goes to different servers on the same computer (most are not served by nginx)
I did a netstat and Nginx is listening to the different ports.
The problem is that Nginx is giving bad gateway for all proxied requests and timeout for the direct ip access.
proxy conf : --> otherdomain.fr = eror 502 bad gateway
# HTTP
server {
# Listen on ipv4
listen 80;
#listen [::]:80;
server_name ~.*.otherdomain.fr;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass "http://192.168.1.17";
}
}
server{
listen 80;
server_name nextcloud.domain.me;
location / {
proxy_set_header Host $host;
proxy_pass "http://127.0.0.1:888";
proxy_redirect off;
}
}
server{
listen 80;
server_name domain.me;
location / {
proxy_set_header Host $host;
proxy_pass "http://127.0.0.1:808";
proxy_redirect off;
}
}
ex for port 88: --> ip:88 = timeout
(obviously, it is enabled and the nginx user have access to the files)
server {
# Listen on ipv4
listen 88;
location / {
root /var/www/html/tests;
}
}
Question :
I'm obviously doing something wrong but I cant find out what, if you could give me a hand it would be incredible.
Thank you in advance!

Related

Can't link domain name to IP (Nothing displays)

I have bought a domain (http://qify.app) on google Domains
When opening Chromium / Firefox I don't have any thing coming out of it (ERR_CONNECTION_REFUSED).
My current setup:
An EC2 AWS machine running my nodeJS backend on port 3000 (localhost)
A nGinx reverse proxy to redirect all inbound port 80 to 3000 (the backend) current nginx config: at /etc/nginx/sites-enabled/default
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name localhost;
root /usr/share/nginx/html;
location / {
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;
}
}
Also I can curl 15.237.134.217 just as much as curl qify.app (and get the correct html)
<!DOCTYPE html><html>
...
</html>
Final nginx version (working for me, I needed two server blocks)
server {
listen 443 ssl http2 ipv6only=off;
server_name qify.app;
ssl_certificate /etc/letsencrypt/archive/qify.app/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/archive/qify.app/privkey.pem;
ssl_ciphers EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
keepalive_timeout 70;
location / {
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 X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
server { # Redirects all port 80 to 443 (with a 301 redirect)
listen [::]:80 http2 ipv6only=off;
server_name qify.app www.qify.app;
return 301 https://qify.app$request_uri;
}
The .app TLD has a baked-in HSTS policy to always use HTTPS on any .app domain. Both Chrome and Firefox, along with several other browsers, include .app in their preloaded HSTS policy list. This means that these browsers will always lead with https on port 443. See https://blog.google/technology/developers/introducing-app-more-secure-home-apps-web/ as a reference to this https requirement.
The nginx config file you showed indicates that it is only listening on port 80. This is why the curl http://qify.app works, since it uses port 80, and doesn't have the preloaded HSTS list that those web browsers do.
Generate a certificate for your domain, and configure nginx to listen on port 443, and your browsers will be able to access it that way.

Nginx node setup to custom directory

I am using nginx first time so need help.
My app is running in /root/project1/tools (this directory is having server.js)
How i can connect nginx to this directory. I searched lot and do not find direct ans. Think nginx will find my server.js by port number not by path. is that true?
I am using linux ubuntu 18
More over nginx is throwing error
2018/10/23 06:14:51 [alert] 3822#3822: *2025 socket() failed (24: Too
many open files) while connecting to upstream, client: 127.0.0.1,
server: nativeiconba$
/etc/nginx/sites-available/nativeiconbase.com
upstream app_yourdomain {
server 127.0.0.1:8080;
keepalive 8;
}
# the nginx server instance
server {
listen 80;
listen [::]:80;
server_name nativeiconbase.com www.nativeiconbase.com;
access_log /var/log/nginx/nativeiconbase.com.log;
# pass the request to the node.js server with the correct headers
# and much more can be added, see nginx config options
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://nativeiconbase/;
proxy_redirect off;
}
}
root /root/project1/src/;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name localhost;
/etc/nginx/sites-available/default
server {
listen 80 default_server;
listen [::]:80 default_server;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
root /root/project1/src/;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
location / {
# First attempt to serve request as file, then
proxy_pass http://10.139.32.25: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;
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
And my node app is running on port 8080. Any idea what can i do to setup nginx. any reference to resource will be helpful.
All you have to do is setup a Reverse Proxy Server in Nginx
Start your NodeJS Server on whatever port
node server.js
If you are using any process management tool like pm2 then
pm2 server.js
Now in nginx config what you have to do is proxying all request to local nodejs server so
upstream app_yourdomain {
server 127.0.0.1:8080;
keepalive 8;
}
# the nginx server instance
server {
listen 80;
listen [::]:80;
server_name nativeiconbase.com www.nativeiconbase.com;
access_log /var/log/nginx/nativeiconbase.com.log;
# pass the request to the node.js server with the correct headers
# and much more can be added, see nginx config options
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://localhost:8080;
proxy_redirect off;
}
}
I have just changed the line proxy_pass http://localhost:8080 in your code

nginx redirect from www to no-www not working

I have a node.js app running on my AWS instance and I'm using NginX to serve it up.
I am trying to redirect the www.websitename.com traffic to websitename.com. I have tried setting up a redirect in the etc/nginx/sites-available/websitename file and it looks like this:
upstream app_websitename {
server 127.0.0.1:8000;
keepalaive 8;
}
server {
listen 0.0.0.0:80;
server_name www.websitename.com;
return 301 http://websitename.com$request_uri;
}
server {
listen 0.0.0.0:80;
server_name websitename.com;
access_log /var/log/nginx/websitename.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_websitename/;
proxy_redirect off;
}
}
Any ideas are appreciated. I have been trying stuff all over the place ;)
replace this listen 0.0.0.0:80; With
listen 80 ;
listen [::]:80;
Did you also add websitename.com to your host file ?
It ended up being a problem with the way the company I bought the domain name from not saving my A-record properly. I spent a whole day wondering what I did wrong lol.

NGINX: Allow multiple ports to be available for https + redirect all http to https

I'm trying to deploy an NGINX server that hosts two node.js Express apps over https.
My main site (the one to be served on port 80) is an Express app running on port 8001. (i.e. https://example.com loads this app)
I'm also running another Express app on port 8002 that I want to be available publicly on port 8080. (i.e. https://example.com:8080 loads this app)
Here is my /etc/nginx/sites-available/default file:
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
return 301 https://$server_name$request_uri;
}
server {
# SSL configuration
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name example.com www.example.com;
include snippets/ssl-example.com.conf;
include snippets/ssl-params.conf;
# Pass requests for / to localhost:8001:
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost:8001/;
proxy_ssl_session_reuse off;
proxy_set_header Host $http_host;
proxy_cache_bypass $http_upgrade;
proxy_redirect off;
}
location ~ /.well-known {
allow all;
}
}
server {
listen 8080 ssl;
server_name example.com www.example.com;
return 301 https://$server_name$request_uri;
include snippets/ssl-example.com.conf;
include snippets/ssl-params.conf;
# pass requests to port 8002 where our other node server is running
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost:8002/;
proxy_ssl_session_reuse off;
proxy_set_header Host $http_host;
proxy_cache_bypass $http_upgrade;
proxy_redirect off;
}
}
If it's of any additional assistance, I've been following the DigitalOcean guides for configuring https and NGINX
here
and
here.
Remove return 301 https://$server_name$request_uri; from 3rd server block.

nginx reverse proxy to IIS

Have a Nginx box on the DMZ that is to proxy trough to a IIS box on the LAN.
I can get to the home page OK via the Nginx box however after navigating further it seems to re-direct rather than proxy.
Is there additional config needed either on the IIS or Nginx side?
server {
listen 80;
listen 443;
server_name internal.lan.com;
location / {
proxy_pass http://internal.lan.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect off;
}
}

Resources