I am using Nginx as a load balancer. It is able to route the request to the designated host address, but not the port as mentioned in the upstream block. It always sends it to the default port.
I have tried a number of approaches modifing proxy_set_header Host, proxy_set_header X-Forwarded-For, proxy_set_header X-Real-IP, proxy_set_header X-Forwarded-Proto, proxy_set_header Referer etc. But nothing seems to work.
I have the Nginx server installed in Azure and trying to access VM's in the same virtual network.
Expected Behavior:
http://localhost/sites -----> http://zzz:9081/sites (should take IP:Port into acount)
Actual Behavior:
http://localhost/sites -----> http://zzz/sites (takes IP, but the port is 80/ default listen port).
upstream alb {
ip_hash;
server zzz:9081;
server zzz:9080;
}
server {
listen 80;
server_name localhost;
# root /usr/share/nginx/html;
location /sites {
proxy_pass http://alb/sites;
proxy_set_header X-Real-IP $remote_addr;
}
}
Any help will be greatly appreciated.
Related
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!
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 have a Digitalocean VPS and 3 Nodejs (ExpressJs) application. Now I started all of them in same IP and different port like this:
- 95.5.5.8:65001
- 95.5.5.8:65002
- 95.5.5.8:65003
But now I want when anybody writes app1.com, the first app 95.5.5.8:65000 shows to him like this:
app1.com -> load server in port 65001
app2.com -> load server in port 65002
app3.com -> load server in port 65003
All of the applications are in same VPS by the same IP, How can I do this, please?
You can implement this feature by following steps:
In domain setting page, map app1.com, app2.com and app3.com to 95.5.5.8.
In Nginx, configure 3 proxies for app1.com, app2.com and app3.com in directory /etc/nginx/conf.d/. Here is the proxy file /etc/nginx/conf.d/app1.conf for app1.com:
server {
listen 80;
server_name app1.com;
access_log /var/log/nginx/app1_access.log;
charset utf-8;
location / {
proxy_pass http://95.5.5.8:65001;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded_For $proxy_add_x_forwarded_for;
}
}
Restart Nginx.
I have a node.js app and I am running nginx as a reverse proxy server. I want to access the same app through multiple ways.
Let the ip address of machine be 12.16.12.113.
Then I want the same app to be accessible through:
12.16.12.113:3000
test.example.com
I have 2 Configuration files for nginx in sites-available directory:
file1:
server {
listen 80;
server_name test.example.com;
location / {
proxy_pass http://localhost: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;
}
}
file2:
server {
listen 3000;
server_name default_server;
location / {
proxy_pass http://localhost: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;
}
}
The problem is that if I link both the files in sites-enabled directory, then only the app is accessible only through 12.16.12.113:3000. If I remove file2, then it is accessible through test.example.com.
But I want both the methods to work.
Edit 1:
Based on suggestion from this answer, I did the following test:
It appears that nginx is not listening to port 80. Why is this happening?
The are few ways to achieve that.
You can make your Node app listen on 12.16.12.113:3000 instead of on localhost:3000 and then you will need nginx only for port 80.
You can make your Node app listen on localhost:3000 (but only on localhost) and then proxy 12.16.12.113:3000 and port 80 by nginx.
You can make your Node app listen on localhost:4000 or 0.0.0.0:4000 on any interface and use nginx to proxy requests to port 80 and 3000.
But you will not be able to make them both listen on the same port and the same interface without problems.
This is the only advice that could be given considering the fact that you didn't include any code example of how your Node app is binding to the ports, which would be the only relevant information here.
I am having an issue with this configuration. My AWS ELB accepts TCP connections on port 80 and forwards them using proxy protocol to an nginx instance listening on port 8080. This nginx node is supposed to use ip_hash module to stick the user to a specific node.
This is working perfectly fine, but only 2 out of the 4 nodes are being used instead of being load balanced among all of them, here is my nginx config
upstream socket_nodes {
ip_hash;
server a.server.com:2000;
server a.server.com:2001;
server a.server.com:2002;
server a.server.com:2003;
}
# Accept connections via the load balancer
server {
listen 8080 proxy_protocol;
set_real_ip_from 0.0.0.0/32;
real_ip_header proxy_protocol;
charset utf-8;
location / {
proxy_pass http://socket_nodes;
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-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
Unlike "round robin" loading balancing, ip_hash means that for any given IP address, NGINX will always forward to the same application instance.