with nginx, how to redirect part of request to apache - redirect

Some solutions about redirection can be found. But what I want to do is redirecting part of requests to another server, specifically, only when a request url includes string "service". For example:
http://localhost/service/image-------------> http://localhost:8080/service/image
http://localhost/service/image/upload------> http://localhost:8080/service/image/upload
http://localhost/service/blog--------------> http://localhost:8080/service/blog
.....
.................................................................................................................................................................
but blow will still served by ngnix,cause no "service" included within url
http://localhost/wiki/....
http://localhost/video/....
How to do this?

You need to use location regex matching together with proxy_pass, example:
upstream apache {
server 127.0.0.1:8080;
}
# in your server block:
server{
# location matching is prioritized by accuracy and order of definition
location ~* ^/service {
proxy_pass http://apache;
proxy_redirect off;
}
}
^/service will match any request beginning with /service and forward it to Apache.
proxy_pass is transparent for the user, i.e. it will forward the request to Apache and return the output to the user.
For more info on location matching, checkout http://wiki.nginx.org/HttpCoreModule#location

If I understood you correctly:
location / {
if ($request_uri ~* "^/service/.*") {
rewrite ^ http://localhost:8080$request_uri permanent;
}
}
P.S. did not check

Related

Configure REST service waith parameter in nginx

I am configuring a REST service in nginx. The path is
http://localhost:8000/services/abc/api/portal/transporter?date=2019-07-15T00:00:00
Can some one help to configure the path. I am trying as
location ~ ^/services/abc/api/portal/transporter/[^/]+$ {
proxy_pass http://backendserver;
}
here transporter is a parameter. It is giving 404 error.
Query string isn't a subject to test for location or rewrite directives. Them both works with so-called normalized URI that doesn't include a query string part (normalized URI is /services/abc/api/portal/parameter in your example) that doesn't match ^/services/abc/api/portal/parameter/[^/]+$ regex due to the /[^/]+$ regex suffix. Why do not use just a regular location
location /services/abc/api/portal/parameter {
proxy_pass http://backendserver;
}
If you want to match only the /services/abc/api/portal/parameter URI (with any query string), you can use an exact match location:
location = /services/abc/api/portal/parameter {
proxy_pass http://backendserver;
}

Redirect/rewrite nginx location to .sock file without prefix

I have one server that has several APIs running on it. One of them is users-DB The following gets down to gunicorn just fine:
location /usersDB/ {
include proxy_params;
proxy_pass http://unix:/home/ubuntu/projects/UsersDB-api/app.sock;
}
Except when I try to access the usersDB API's /helloWorld route, and look in the logs at gunicorn.err I see:
GET /usersDB/helloWorld
I was hoping to see:
GET /helloWorld
Of course, gunicorn returns 404s and that is what I see in my browser. I've tried rewrite rules:
location /usersDB/ {
rewrite /usersDB/(.*) /$1 last;
include proxy_params;
proxy_pass http://unix:/home/ubuntu/projects/UsersDB-api/app.sock;
}
But the above results in the requests making their way to /var/www/htmlhelloWorld instead of app.sock.
I know that if you use a url for the proxy_pass you just add a trailing /, but I'm not sure what to do in the case of a sock file.
How do I get rid of the /usersDB/ suffix that is now included on all routes in nginx?
Use a separating :. For example:
proxy_pass http://unix:/home/ubuntu/projects/UsersDB-api/app.sock:/;
See this document for details.

Automatic trailing slash redirect leaks internal IP in the Location header

When doing curl -ILk http://192.168.0.4/app the Location header of the 301 redirect is http://my-srv.local/app/ which is a server-local address. I've put server_name_in_redirect on everywhere to get rid of it, but the result is the same.
Config:
server {
server_name my-nginx;
server_name_in_redirect on;
set $endpoint http://my-srv.local;
location / {
server_name_in_redirect on;
proxy_pass $endpoint;
}
}
Note: the nginx version that I'm using doesn't have absolute_redirect yet.
Reference: Information leak with automatic trailing slash redirect
How to make it point to the correct URL or prevent the leak at least?
So it's the my-srv.local who generates that response (that is pretty reasonable). With the nginx 1.11.8+ it will probably generate relative redirects. In the meantime, the possible fix is to overwrite that redirect:
server {
server_name my-nginx;
set $endpoint http://my-srv.local;
location / {
proxy_pass $endpoint;
proxy_redirect $endpoint http://$host;
}
}

how to remove trailing slash on post request nginx

I'm trying to remove trailing slash on http post method, when i try to re-write the URL using rewrite ^/(.*)/$ /$1 permanent; it doesn't work for me
The upstream should receive in this format /x/y if the Http POST is coming in these format
location /x/y/ ==> location /x/y
location /x/y ==> location /x/y
This is the nginx configuration
upstream backend {
server 127.0.0.1:8778;
# Number of idle keepalive connections per worker process.
keepalive 35;
}
location /x/y {
limit_except POST {
deny all;
}
proxy_pass http://backend;
proxy_buffering on;
include proxy.conf;
}
The problem here is when the upstream see the URI is in this format /x/y/ it rejected the request, what should be the correct rewrite rule for this so that if the http post comes in the format like /x/y or /x/y/ the upstream should always see /x/y
The permanent will cause the rewrite to generate a redirect with a 301 response. What you need is an internal adjustment of the URI before sending it upstream:
location /x/y {
rewrite ^/(.*)/$ /$1 break;
...
}
See this document for more.

Nginx config for single page app with HTML5 App Cache

I'm trying to build a single page app that utilizes HTML5 App Cache, which will cache a whole new version of the app for every distinct URL, thus I must redirect everyone to / and have my app route them afterward (this is the solution used on devdocs.io).
Here's my nginx config. I want all requests to send a file if it exists, redirect to my API at /auth and /api, and redirect all other requests to index.html. Why is the following configuration causing my browser to say that there is a redirect loop? If the user hits location block #2 and his route doesn't match a static file, he's sent to location block #3, which will redirect him to "/" which should hit location block #1 and serve index.html, correct? What is causing the redirect loop here? Is there a better way to accomplish this?
root /files/whatever/public;
index index.html;
# If the location is exactly "/", send index.html.
location = / {
try_files $uri /index.html;
}
location / {
try_files $uri #redirectToIndex;
}
# Set the cookie of the initialPath and redirect to "/".
location #redirectToIndex {
add_header Set-Cookie "initialPath=$request_uri; path=/";
return 302 $scheme://$host/;
}
# Proxy requests to "/auth" and "/api" to the server.
location ~* (^\/auth)|(^\/api) {
proxy_pass http://application_upstream;
proxy_redirect off;
}
That loop message suggests that /files/whatever/public/index.html doesn't exist, so the try_files in location / doesn't find $uri when it's equal to /index.html, so the try_files always internally redirects those requests to the # location which does the external redirect.
Unless you have a more complicated setup than you've outlined, I don't think you need to do so much. You shouldn't need external redirects (or even internal redirects) or server-side cookie sending for a one-file js app. The regex match for app and api wasn't quite right, either.
root /files/whatever/public;
index index.html;
location / {
try_files $uri /index.html =404;
}
# Proxy requests to "/auth" and "/api" to the server.
location ~ ^/(auth|api) {
proxy_pass http://application_upstream;
proxy_redirect off;
}