How to simulate upstream with Nginx for dynamic subdomains - nginx

I would proxy a request like the upstream solution.
server_name ~^(?<subdomain>.+)\.example\.com$;
root /dev/null;
location / {
error_page 502 #nextserver;
resolver 127.0.0.1:53 valid=300s;
proxy_pass "https://$subdomain";
}
location #nextserver {
error_page 502 #error;
resolver 127.0.0.1:53 valid=300s;
proxy_pass "https://$subdomain-blahblah";
}
location #error {
return 502 'Service is not available';
}
As you can see I would check https://$subdomain and if it doesn't exist or it down then checks https://$subdomain-blahblah.
it works fine but the problem happen when the second server is down, then Nginx doesn't provide Service is not available message.
So the scenario is like
check Server A -> down
check Server B -> down
Return custom error
I couldn't use upstream because the name of servers is dynamic.

I would suggest you to solve this problem differently. Check out this sample. It shows how you can approach the problem:
http{
listen 80;
root /path/to/static/files;
upstream_server subdomain1{
server 192.168.1.100:8000;
server 192.168.1.101:8000;
}
upstream_server subdomain2{
server 192.168.1.102:8000;
server 192.168.1.103:8000;
}
location / {
server_name subdomain1.example.com;
proxy_pass http://subdomain1;
include /etc/nginx/proxy_pass;
}
location / {
server_name subdomain2.example.com;
proxy_pass http://subdomain2;
include /etc/nginx/proxy_pass;
}
}

Related

Nginx redirecting traffic to a reserve server

I have an Nginx server. Which distributes traffic to multiple servers, below is the config:
location /rc/temp/ {
proxy_pass https://rc1-test.jer.com;
}
location /rc/ {
if ($testip) {
proxy_pass http://192.168.0.100;
}
proxy_pass http://192.168.0.103;
}
location / {
if ($is) {
proxy_pass https://rc6-test.jer.com;
}
proxy_pass https://rc8-test.jer.com;
}
I want to know if it is possible to add some if ...else so that there would be an opportunity, for example, if some one with the server is unavailable, then the traffic is redirected to the reserve. For example, for location /rc/temp/ if https://rc1-test.jer.com is not available; then direct traffic to a clone of this server https://rc2-test.jer.com;. Thanks.
Try error_page directive:
location /rc/temp/ {
proxy_pass https://rc1-test.jer.com;
error_page 500 #fallback
}
location #fallback {
proxy_pass https://rc2-test.jer.com;
}
You may change the 500 code in the error_page directive with some other error code if you need to.

Nginx configuration doesn't work properly on Tomcat

Hi I am facing issue when configuring nginx as proxy server to redirect request to my tomcat server. I have 3 tomcat server running on different machine & different port like this
192.168.51.115:8115
192.168.51.120:8120
192.168.51.130:8130
Now I want to config nginx to pass request to my three server sequentially like this
www.example.com/app1
www.example.com/app2
www.example.com/app3
Real IP: 123.123.123.123
This is my configuration under - site-enabled
server {
listen 80;
server_name example.com www.example.com;
location /app1 {
proxy_pass "http://192.168.51.115:8115";
}
location /app2 {
proxy_pass "http://192.168.51.120:8120";
}
location /app3 {
proxy_pass http://192.168.51.130:8130;
}
}
Note: When i put location directive placing just / then it works but doesn't work on /* like app1,app2 or app3
Can you try using ^~ as modifier in your location block ?
like
server {
listen 80;
server_name example.com www.example.com;
location ^~ /app1 {
proxy_pass "http://192.168.51.115:8115";
}
location ^~ /app2 {
proxy_pass "http://192.168.51.120:8120";
}
location ^~ /app3 {
proxy_pass http://192.168.51.130:8130;
}
}

nginx server rewrite configuration

I have ngnix configured to load balance four tomcat applications. I have a question about the rewrite/redirect rules for my server configuration section.
How do I make requests for http://zavlb.rand.int.co1 go to http://zavlb.rand.int.co1/zi/za/fpages/aaa/FPAGE_AAA_00_00.xhtml?
upstream zavlb {
ip_hash;
# server applnx1.za.rand.int.co1:8080 weight=2;
server applnx01.za.rand.int.co1:8080 weight=2;
server applnx02.za.rand.int.co1:8080 weight=2;
server applnx03.za.rand.int.co1:8080 weight=2;
server applnx04.za.rand.int.co1:8080 weight=2;
}
server {
listen 80;
server_name zavlb.rand.int.co1;
client_max_body_size 5m;
location / {
proxy_pass http://zavlb;
proxy_set_header Host $host;
}
# redirect server error pages to the static page 404/index.html
#
error_page 404 500 502 503 504 404/index.html;
location = 404/index.html {
root /home/za/www;
access_log /var/log/nginx/access.log combined;
}
}
I apologize if i did not understood this correctly, but it seems to be you a URL redirect? In that case, you can use nginx rewrite module as described here: http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#rewrite. Ex:
rewrite / http://zavlb.rand.int.co1/zi/za/fpages/aaa/FPAGE_AAA_00_00.xhtml last;
However, if you want to serve that file from the backend server (proxy) when requests are hitting your root, you can use nginx try_files, like:
location / {
try_files $uri $uri/ /index.xhtml /zi/za/fpages/aaa/FPAGE_AAA_00_00.xhtml =404;
}
Hope this helps.

Why is my nginx "rewrite" directive causing a redirection loop?

Given the following http block, nginx performs as expected. That is, it will rewrite a URL such as http://localhost/3ba48599-8be8-4326-8bd0-1ac6591c2041/ to http://localhost/modif/3ba48599-8be8-4326-8bd0-1ac6591c2041/ and pass it to the uwsgi server.
http {
upstream frontend {
server frontend:8000;
}
server {
listen 8000;
server_name localhost;
root /www/;
location ~* "^/([0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12})/?$" {
include uwsgi_params;
set $uuid $1;
if ($cookie_admin) {
# if cookie exists, rewrite /<uuid> to /modif/<uuid> and pass to uwsgi
rewrite / /modif/$uuid break;
uwsgi_pass frontend;
}
content_by_lua_block {
ngx.say("Ping! You got here because you have no cookies!")
}
}
}
}
However, when I add another location block in the manner displayed blow, things fall appart and I get ERR_TOO_MANY_REDIRECTS.
http {
# access_log /dev/stdout; # so we can `docker log` it.
upstream frontend {
server frontend:8000;
}
server {
listen 8000;
server_name localhost;
root /www/;
location / { # THIS MAKES EVERYTHING FALL APART :(
uwsgi_pass frontend;
include uwsgi_params;
}
location ~* "^/([0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12})/?$" {
include uwsgi_params;
set $uuid $1;
if ($cookie_admin) {
# if cookie exists, rewrite /<uuid> to /modif/<uuid> and pass to uwsgi
rewrite / /modif/$uuid break;
uwsgi_pass frontend;
}
content_by_lua_block {
ngx.say("Ping! You got here because you have no cookies!")
}
}
}
}
What's going on here, exactly? How can I fix this?
I see your Nginx is listening on port 8000, but your upstream server is at 'frontend', also on port 8000. If frontend resolves to the same server that Nginx is running on, then you've got a loop of proxy requests happening.

Nginx proxy_next_upstream doesn't work

I want nginx to search my local host for the file first and on a 404 error it should search server 1.1.1.1.
I am able to fetch the file that is located on local host, but not able to get from server 1.1.1.1.
server {
listen 80;
server_name localhost;
access_log /var/log/nginx/access.log main;
location /products/ {
proxy_next_upstream http_404;
root /var/foo;
}
}
server {
listen 80;
server_name 1.1.1.1;
location /products/ {
#########
}
}
I guess proxy_next_upstream is not switching to the server.
Any help on this would be appreciated.
The proxy_next_upstream directive is a configuration directive to control re-request from a group of upstream servers by a proxy_pass if request to one of them fails. It doesn't make sense without proxy_pass and an upstream block defined. You may use it if you proxy to multiple upstream servers like this:
upstream backends {
server 192.2.0.1;
server 192.2.0.2;
...
}
server {
...
location / {
proxy_pass http://backends;
proxy_next_upstream error timeout http_404;
}
}
If you want nginx to search for a file on disk, and if it's not found - proxy request to another server, configure it e.g. using try_files fallback instead:
location / {
root /path/to/root;
try_files $uri #fallback;
}
location #fallback {
proxy_pass http://...
}
See http://nginx.org/r/try_files for more info about the try_files directive.

Resources