Nginx redirect to URL - nginx

Using my Nginx install I was forwarding my requests to www.example.com:8080/twinkle/index.jsp
location / {
proxy_set_header Host www.example.com;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://localhost:8080/twinkle/;
}
I am deploying another application that is going to primarily be used to download and running on port 81
www.example.com:81/blacksheep/mary/mary.wav
www.example.com:81/blacksheep/tom/tom.wav
location /blacksheep/(.*) {
proxy_pass http://localhost:81/media/(.*) ;
}
However this takes me back to www.example.com/twinkle/blacksheep/xxxxx
Not sure what I am missing.. (I am an amateur in this space, filling up temporarily )
thanks.

I tried the following after Anatoly's comment & it worked.
location /blacksheep {
rewrite ^(/blacksheep)(.*)$ /blacksheep$2 break;
proxy_pass http://localhost:81;
}

Related

Nginx: create a location to redirect to another location

I'm trying to redirect a location to another location, but for some reason it doesn't work. My nginx service is running under a Docker container on 8080:80 port.
I have /portal that shows my web application correctly and I would like nginx redirects from / to /portal. I've tried some approaches using location / or location = /, but with no success.
The /portal can be accessed normally by
http://localhost:8080/portal
but for some reason when I access
http://localhost:8080/
the browser redirects to
https://localhost/portal
instead of redirecting to:
http://localhost:8080/portal
Locations:
upstream webportal {
server portal_container:8080;
}
location / {
return 301 /portal;
}
location /portal {
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 http://webportal;
}
I was able to fix the problem by adding absolute_redirect off; to my location:
location / {
absolute_redirect off;
return 301 /portal;
}

Let Nginx pass current location

I have nginx reverse-proxy to my site on IIS and here is my nginx config:
UPDATE
upstream backend {
server 43.128.77.101;
}
server {
server_name domain.subdomain.com;
location /products {
if ($query_string ~ Jeans){
return 301 /get-all-products/?filter=jeans;
}
if ($query_string ~ Shirts){
return 301 /get-all-products/?filter=shirts;
}
if ($query_string ~ Hats){
return 301 /get-all-products/?filter=hats;
}
}
location / {
proxy_pass http://backend;
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;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
It redirects from /products page to certain URLs by query string. But for page /products-available it fails with error 404. Nginx error log contains error:
"/usr/share/nginx/html/products-available" failed (2: No such file or directory)
The page /products-available doesn't need any redirections. I want it to pass on backend IIS server as it is. How can I tell nginx to pass it through? What am I doing wrong?
Thank you.
This would be because you are only defining the behavior of Nginx for a given path (/products).
If you want to define a default behavior for Nginx requests that don't match the /products path (like /products-available) you can add the following after your current location section to proxy any other path request to a different application/port.
location / {
proxy_pass http://127.0.0.1:3000;
}
You can see more information on sending a request to a different application in https://docs.nginx.com/nginx/admin-guide/web-server/reverse-proxy/#passing-a-request-to-a-proxied-server

nginx proxy_pass to dynamic url

I have multiple backend servers and I want to proxy all servers using a single nginx server instance. I don't want to change nginx.conf whenever I add a new backend server.
For example: Server 1 : 192.168.10.1:8080, Server2: 192.168.10.2:8080, etc
Nginx is running on example.com. I want to access Server1 by using example.com?ip=192.168.10.1, example.com?ip=192.168.10.2 etc
I tried this configuration, but it is giving 500 error page.
location / {
proxy_pass http://$arg_ip:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
Is there something I am missing? Is there any other way of achieving this?
server {
server_name dynamic_host;
listern 8080;
#resolver 8.8.8.8;
#seems you don't need resolver because you use ip address
location / {
if ( $arg_address != "" ) {
proxy_pass $arg_address;
#proxy_pass $arg_address$uri
#proxy_pass $arg_address$request_uri
}
}
}
the difference between the three proxy_pass
$proxy_address
example.com?address=http://192.168.10.2:8080/ goes to
http://192.168.10.2:8080/
$proxy_address$uri
example.com/test/path?address=http://192.168.10.2:8080/ goes to
http://192.168.10.2:8080/test/path
$proxy_address$request_uri
example.com/test/path?address=http://192.168.10.2:8080/&param=value goes to
http://192.168.10.2:8080/test/path?address=http://192.168.10.2:8080/&param=value
you can change the param address to ip, in this case, don't forget to change $arg_address to $arg_ip.
reference:
http://nginx.org/en/docs/http/ngx_http_core_module.html#variables

nginx keep port number when 301 redirecting

I'm trying to collapse a second brand of a web app into the first brand and use 301 redirects to redirect any lingering traffic. The server is running in a Vagrant box forwarding on port 8001. I would like to have:
Instead of https://local-dev-url:8001/foo/(anything) 301 to https://local-dev-url:8001/(anything)
Instead of https://local-dev-url:8001/adminfoo/(anything) 301 to https://local-dev-url:8001/admin/(anything).
Here's what I have:
location ~ /foo/?(.*)$ {
return 301 $1/;
}
location ~ /adminfoo/?(.*)$ {
return 301 admin/$1/;
}
location / {
proxy_set_header Host $http_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-Port 443;
proxy_set_header Authorization $http_authorization;
proxy_set_header X-Scheme $scheme;
proxy_pass http://127.0.0.1:5000;
proxy_redirect http:// $scheme://;
}
location /admin/ {
alias /hostonly/path/to/admin/stuff/;
}
However, instead of redirecting https://local-dev-url:8001/foo/ to https://local-dev-url:8001/ it is 301ing to https://local-dev-url// instead. (No port number, extra slash.) I've seen answers that hard-code the URL of the redirect, but since I work with a lot of other devs and we all have unique local dev URLs, the only consistent part is the :8001 port number.
Is there a way to configure the 301 to work as desired?
If nginx is not listening on port 8001, it cannot know which port to use in the redirect. You will need to specify it explicitly:
location ~ /foo(.*)$ {
return 301 $scheme://$http_host$1;
}
location ~ /adminfoo(.*)$ {
return 301 $scheme://$http_host/admin$1;
}
The $http_host variable consists of the hostname and port from the original request. See this document for details.
For me, editing the following lines in proxy.conf to look like this, did the trick:
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Host $http_host;

Dynamic routing with NGINX

I am an amateur of NGINX, I want to setup NGINX as a Reverse Proxy for my web server.
I would like to know that the NGINX these things as listed below:
When a browser send request with URL: http://nginxproxy.com/client/1.2.3.4/, this request should be passed to the client with IP 1.2.3.4 http://1.2.3.4/, the browser should still show the URL nginxproxy/client/1.2.3.4/
And the same for:
nginxproxy.com/client/2.3.4.5 --> //2.3.4.5
nginxproxy.com/client/2.3.4.6 --> //2.3.4.6
All the others requests that doesn't mach the pattern should come to my default server myserver.
Can I do this by using NGINX?
After researching, I tried with the below configuration:
But unlucky, It doesn't work. The address was changed to http:/1.2.3.4 on browser's address bar, instead of http:/nginxproxy.com/client/1.2.3.4 as expected.
server {
listen 80;
location ~ ^/client {
rewrite ^/client/?(.*) /$2 break;
proxy_pass $scheme://$1;
}
location / {
proxy_pass http://myserver.com;
}
}
Any help is much appreciated.
Doing some more research and based on #Cole input, here is my answer:
location ~ ^/client/(?<site>[^/]+)/? {
rewrite ^.*\/client\/(?<site>[^\/]+)\/?(.*) /$2 break; #passing all the remaining request URIs after <site> group to client server
proxy_pass $scheme://$site;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host/client/$site; #this help to keep the address as it is on the browser's address bar
proxy_set_header X-Forwarded-Proto $scheme;
}
location / {
proxy_pass $scheme://myserver.com
}
server {
listen 80;
location /client/ {
rewrite ^/client/(?<site>[^/]+)/? $scheme://$site;
}
location / {
proxy_pass $scheme://myserver.com;
}
}

Resources