Nginx routing to several RESTful endpoints - rest

I would like to use Nginx as the HTTP proxy server.
On the backend, we have 3 different applications written in Java exposing a RESTful API each. Every application has their own prefix on the API.
For instance:
APP 1 - URI prefix: /api/admin/**
APP 2 - URI prefix: /api/customer/**
APP 3 - URI prefix: /api/support/**
On the frontend, we have a SPA page making requests to those URI's.
Is there a way to tell Nginx to route the HTTP request depending on the URI prefix?
Thanks in advance!

I am pretty sure you can use nginx proxy forwarding to re-route according to each of your several uri prefixes. I have used proxy forwarding with nginx. Your example I have adapted from a page that gives information specifically on uri prefixes (I have preserved the /foo entry from that page here for your comparison):
https://serverfault.com/questions/379675/nginx-reverse-proxy-url-rewrite
See also (noting difference of proxy_pass from proxy_redirect),
http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass
With your code, the locations would be something like:
server {
listen 80;
server_name www.example.com;
location /api/admin {
proxy_pass http://localhost:3200/;
}
location /api/customer {
proxy_pass http://localhost:3200/;
}
location /api/support {
proxy_pass http://localhost:3200/;
}
location /foo {
proxy_pass http://localhost:3200/;
}
}
As the link I have provided mentions, note the forward slash at the end of each location directive that enables the wild carding after the prefix. And of course the urls to which each of the paths are redirected need not all be the same--localhost:3200--as they are in this example.

Related

Redirecting from "www" to root domain in nginx

I have a fairly simple task, but it doesn't seem to be working and I can't figure out the reason for it. I have a digitalocean droplet and a domain that points to the digitalocean dns. I have set up an "A Record" with "#" and "www" for the domain that both point to my droplet. In my nginx config I have set up one server block for the redirect which contains:
server {
server_name www.example.com;
return 301 $scheme://example.com$request_uri;
}
Now when I do:
curl -I http://www.example.com
I get a http response saying "301 permanently moved" with the correct location. (When I use https I don't get the "permanently moved" but that's a different story).
However when I open the URL in my browser nothing happens and I just get the default nginx website.
What could be the reason for this behavior?
Its correct, you just need a nginx server listening on example.com :
server {
server_name example.com;
location / {
# do something
}
}

Redirection with nginx

Using this tutorial
http://rainbow-six3.com/plesknginx/ i am trying to redirect my name.com to ip:8080 , after that setup if i acces through name.con i get an redirection loop... is there any other service to redirect? Had a lot of problems with nginx.
I want to redirect name.com to an tomcat7 application wich uses ip:8080 to get executed. Tomcat is already an headache...
Neither this helped me:
Tomcat base URL redirection
It outputs webpage not available on link: http://name.com/index.jsp
Mind the words. 'Redirecting' != 'Proying'. You redirect through 301 or 302 to a publicly directly accessible resource, while you proxy to a backend, not supposed to be directly accessed.
The minimal configuration to proxy any name.com/<whatever> request to <ip>:8080/<whatever> is:
server {
listen 80;
server_name name.com;
location / {
proxy_pass $scheme://<ip>:8080;
}
}

Redirect IP to host name in NGINX

I want to redirect each IP address of my web site to the host name of that web site using rewrite directive and than access the web site using proxy_pass directive in NGINX like this
proxy_pass http://host/name ;
Using NGINX as a proxy works for but i couldn't change my script to rewrite addresses and proxy my request at the same time. I tried to use Rewrite directive but i can't find the right syntax for that.
Using rewrite directive to change host will cause a redirection. It means client needs to post another request with new host, and then, you can proxy_pass this request. In this case, the URL in client (for example, browser) will change, like 'http://*.*.*.*:port/uri?request_string' -> 'http://host/uri?request_string'.
Usually, we use rewrite directive to change the URI of the request which will be proxy_passed. If you want to change the host, using proxy_set_header. An example:
location ~* "^/maishenme/(knowse|knowdetail|iget|ilist|initem|i?compare)(.*)?$" {
rewrite "^/maishenme/(.*)?$" /$1 break;
proxy_pass http://***.xxx.com;
proxy_set_header Host "internal.xxx.com";
break;
}
And in this case, from the client side, the url do not change, but for the backend server, you can print the host field and see it changed to "internal.xxx.com"

nginx load balancing keep redirect me wrong

hi i have nginx server and other webserver which i want to hide from public direct access. but when i try to access that server on root i send redirect response to client to url like this: server/test/spring/main. when i try to access it from nginx server i get the server url redirect instead nginx url.
example:
my-nginx.com
my-server.com
if i want to access myserver.com/test/spring/main from nginx server i guess i have to access my-nginx.com/test/spring/main but when i do that i get redirect to url myserver.com...
my config:
upstream my-server {
server my-server.com;
}
server {
listen 80;
server_name my-nginx;
location / {
proxy_pass http://my-server/;
}
}
the other think is when i access the root page on my-server.com i redirect the client to "https://my-server.com/test/spring/main".
Why i'm redirect from my-nginx.com url to my-server.com?
This configuration seems do not have big problem, and I also use proxy_pass to access other domain in our production environment, and do not redirect.
You should be care of the last '/' in "http://my-server/", usint "http://my-server" instead, or just using "http://my-server.com" with no upstream.
The behavior with '/' or with not '/' is a little different.

nginx redirect mask

I'm new to nginx and am trying to figure out an issue with redirection. I'm trying to redirect a website from a host running a web application to another domain. That part I've done but I'm looking to mask it. When it redirects, I don't want the user to know they've gone to another domain.
I've substituted the domain names for privacy of my client. But, they are on a Linode at test.com that's running a web application that's at sub.test.com. All I want is for any user visiting test.com to be redirected to a temporary site hosted on other.com but without exposing the domain.
Previously, someone had shown me how to do it but it was a long time ago and I no longer have the information to reference. Can someone help me out? I don't want to expose the domain of the testing environment.
server {
listen 80;
server_name www.test.com test.com www.test.net test.net;
rewrite ^ http://other.com/sub redirect;
location / {
root /srv/http/www.test.com;
index index.html;
}
}
You can't do that with redirect, cause redirect is actually like telling the browser where to fetch page from, so you can't obscure the real location.
I guess what you need is proxying, e.g. a client requests some document from your server (test.com), then your server fetches the document from some other server (other.com) and returns that document to a client as if it was generated on test.com - it that case a client won't be able to determine where actually the document is originating from.
This can be done with:
server_name test.com;
location / {
proxy_pass http://other.com;
}
if you want the redirect to not change the user-visible domain name then you need to change it from a 301 to a 302 by changing your rewrite as follows:
rewrite ^ http://other.com/sub redirect;