why doesn't nginx load balance not work? how fixed it? - nginx

the nginx config below the server
28081 always return 502, i config the proxy_next_upstream, but the request does not passed to next node (28082)
nginx version:1.19.3
upstream boot-server {
server 172.17.112.90:28081 weight=9;
server 172.17.112.90:28082 weight=1;
}
server {
listen 80 ;
server_name some.domain.com;
root /home/
location /v3/ {
proxy_pass http://boot-server;
proxy_next_upstream http_502 non_idempotent;
proxy_read_timeout 7200;
proxy_connect_timeout 5;
proxy_set_header Host $Host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Content-Type "application/json";
}
}

Related

How to add public url to nginx reverse proxy with a access token

I am using a public URL and adding it to my Nginx reverse proxy. I have come across a bad request error when I run my nginx.conf configurations file. I have an access token that also needs to be added
Below is my nginx.conf file.
Any recommendations ?
worker_processes 1;
events {
worker_connections 1024;
}
http {
server {
listen 80;
server_name localhost 127.0.0.1;
client_max_body_size 0;
set $allowOriginSite *;
proxy_pass_request_headers on;
proxy_pass_header Set-Cookie;
# External settings, do not remove
#ENV_ACCESS_LOG
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_redirect off;
proxy_buffering 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_pass_header Set-Cookie;
proxy_set_header X-Forwarded-Proto $scheme;
location /test/ {
proxy_pass https://a***.***.com;
}
}
}
403 ERROR
The request could not be satisfied.
I have fixed the issue by using the Nginx rewrite modules. I have posted a link below.
http://nginx.org/en/docs/http/ngx_http_rewrite_module.html
location /test/ {
rewrite ^/test(.*) https://URL$1 break;

How to get real client ip when using upstreams on one server?

I have a vps running nginx, with ngx_stream_ssl_preread_module I have made SSL and Non-SSL protocols work on the same port.
When I checked the access.log, I found a lot of lines starting with 127.0.0.1. Obviously this is not a real client IP.
I tried to modify my nginx.conf, such as proxy_set_header, real_ip_header, set_real_ip_from 127.0.0.1, etc.,they have no effect.
This is my origianl stream configuration in nginx.conf.
stream {
server {
listen 443;
ssl_preread on;
proxy_pass $upstream;
}
map $ssl_preread_protocol $upstream {
default shadowsocks;
"TLSv1.1" https;
"TLSv1.2" https;
"TLSv1.3" https;
}
upstream shadowsocks {
server 127.0.0.1:7890;
}
upstream https {
server 127.0.0.1:8888;
}
}
I would try setting the proxy headers as follows:
server {
listen 443 ssl default_server;
ssl_preread on;
proxy_redirect off;
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Host $remote_addr;
proxy_pass $upstream;
}
}

ERR_TOO_MANY_REDIRECTS Nginx

I'm trying to redirect my particular domain to Tomcat where multipe Application is running, but I'm getting "ERR_TOO_MANY_REDIRECTS" ERROR in the browser
My configuration has below
server {
listen 80;
server_name www.mydomain.com;
location / {
proxy_pass http://localhost:7070/AppName;
proxy_read_timeout 600s;
client_max_body_size 200m;
}
}
Recently I configured my Odoo app to forward all requests via Nginx.
You need to add something like this to your Nginx config:
upstream tomcat {
server 127.0.0.1:8080;
}
server {
listen 80;
server_name www.mydomain.com;
location / {
proxy_pass http://tomcat;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
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;
}
proxy_read_timeout 600s;
client_max_body_size 200m;
}
}
If this doesn't work, for reference, you may want to check this article: https://www.rosehosting.com/blog/install-odoo-on-a-debian-8-vps-with-nginx-as-a-reverse-proxy/
I hope you'll find this useful.
It is common to set the proxy_redirect directive in the same way as the proxy_pass directive. see for example configure-nginx-with-proxy-pass.
location ~ ^/stash {
proxy_pass http://IP:7990;
proxy_redirect http://IP:7990/ /stash;
}
but I got the ERR_TOO_MANY_REDIRECTS error with this configuration... so i changed it for "proxy_redirect off;" as suggested here, and it solved my problem!
here is the configuration for my gitlab server:
server {
listen 80;
server_name reverseproxy.mydomain.org;
location /gitlab/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host-Real-IP $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://172.xx.xx.xxx:10080;
#proxy_redirect http://172.xx.xx.xxx:10080/ /gitlab/;
proxy_redirect off;
}
}
NB: i also needed to remove the directive "proxy_set_header Host $host;" for my gitlab server, powered by docker-gitlab.

nginx proxy requests for a specific path

Is it possible to pass requests for a specific path to a different upstream server?
Here is my nginx site configuration:
upstream example.org {
server 127.0.0.1:8070;
keepalive 8;
}
server {
listen 0.0.0.0:80;
server_name example.org www.example.org;
access_log /var/log/nginx/example.org.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://example.org;
proxy_redirect off;
}
}
Currently, requests to this site are redirected to a Node.js instance running on port 8070.
I would like requests to this site that have a path starting with /services to be redirected to another Node.js instance running on port 8080.
Is this possible? And of course -- how so?
Yes, just add another location block:
upstream example.org {
server 127.0.0.1:8070;
keepalive 8;
}
upstream other.example.org {
server 127.0.0.1:8080;
keepalive 8;
}
server {
listen 0.0.0.0:80;
server_name example.org www.example.org;
access_log /var/log/nginx/example.org.log;
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_redirect off;
location / {
proxy_pass http://example.org;
}
location /services {
proxy_pass http://other.example.org;
}
}
Note: I extracted all shared proxy directives into the server block so that they are not repeated in each location block. If they would differ between different locations, you would have to move them again into the location blocks...

nginx url rewrite for reverse proxy

I have an nginx on port 80 and a tomcat on port 8080 configured as upstream.
The war application in tomcat listen to /pwm.
I would like to configure nginx to a reverse proxy for tomcat and rewrite the url "/" to "/pwm".
example:
user types "web.noc.local" in browser and nginx rewrites the url to web.noc.local/pwm and redirects to tomcat on port 8080.
my nginx config:
upstream pwm_server {
server 127.0.0.1:8080 fail_timeout=0;
}
server {
listen 80;
server_name web.noc.local;
access_log /var/log/nginx/log/web.noc.local.access.log main;
error_log /var/log/nginx/log/web.noc.local.error.log;
location / {
if ($is_args != "") {
rewrite "^$" /pwm break;
expires 7d;
proxy_pass http://pwm_server;
}
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_max_temp_file_size 0;
proxy_buffering off;
proxy_connect_timeout 30;
proxy_send_timeout 30;
proxy_read_timeout 30;
proxy_pass http://pwm_server;
}
}
now when I open the url the, nothing happens, only a blank screen.
thx for help.
Ok, I found a solution for me:
location / {
rewrite ^ http://web.noc.local/pwm/ last;
}
location /pwm {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_max_temp_file_size 0;
proxy_buffering off;
proxy_connect_timeout 30;
proxy_send_timeout 30;
proxy_read_timeout 30;
proxy_pass http://pwm_server;
}

Resources