How to remove first part of route in nginx? - nginx

I have nginx configured on server like below
localhost ~ elastic{
proxy_pass http://localhost:5000;
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;
}
With this I go on http://nginx_host/elastic/something I reach http://localhost:5000/elastic/something.
But I want to change it such that by going on http://nginx_host/elastic/something I reach http://localhost:5000/something.
I have many other services running on this server using same configuration as above, therefore I cannot use a generic method to remove the first part of the path, it needs to be specifically to remove elastic from start. How can I do this?

You can modify the config like this:
localhost /elastic/ { # note the trailing slash here
proxy_pass http://localhost:5000/; # note the trailing slash here
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;
}
The trailing slashes will ensure that /elastic/ are removed from the url while proxying the request. So, http://nginx_host/elastic/something will now go to http://localhost:5000/something.

Related

nginx proxy_pass not working with variable

I am trying to configure proxy pass in Nginx with query argument, variable is valid, but proxy pass not working.
Working
location / {
proxy_pass http://192.168.1.115:1111/;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
}
Not working
location / {
proxy_pass http://192.168.1.115:$arg_port/;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';`
}

nginx redirect rule, example.com/gaming/postName to g.example.com/postName

I am trying to make 301 redirect rule because I moved my website domain.
My blog was running on
example.com/gaming
and posts are like this
example.com/gaming/postName
I want to rewrite it with a rule to be
G.example.com/postName
basically changing example.com/gaming -> g.example.com
There are many solutions for this problem and here's the one which I use.
server {
listen 80;
server_name example.com;
location /gaming {
proxy_pass https://YOUR_APP_IP:3000;
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;
}
location / {
proxy_pass https://YOUR_APP_IP:3000;
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;
}
}
Here, keep YOUR_APP_IP as 127.0.0.1 if you're running on the same machine. else use the IP of the machine where your application is running.
This will route all the /gaming routes to default route.

NGINX as reverse proxy to serve paths directly via url

I am looking to configure nginx to serve following requirement
We are serving individual websites at www.company.com/mywebsites and want to use nginx as reverse proxy to serve the same website on custom URL e.g www.mywebsites.com
I have configured dns of mywebsites.com to point to nginx server and following code block serves the home page of www.company.com
server {
server_name www.mywebsites.com;
location / {
proxy_pass http://127.0.0.1:9002;
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;
}
}
however i am struggling to get the part of redirecting www.mywebsites.com to the www.company.com/mywebsites path. I have tried added proxy_redirect but that didn't help and also trying to add path in the proxy_pass doesn't work either as it then tries to fetch all of the components using www.company.com/mywebsites/required/componenets.css which results in 404 as the correct path is www.company.com/required/componenets.css
You might need to do this change:
server {
server_name www.mywebsites.com;
location / {
rewrite /mywebsites/(.*) /$1 break;
proxy_pass http://127.0.0.1:9002;
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_redirect off;
}
}
However, I truly recommend that this issue to be fixed from the site code itself (I don't know what the site is developed with), as this might have unknown side effects on some URLs.

Nginx proxying not working, returning 400 error

So I have my sites available file set up as follows:
location / {
proxy_pass http://localhost:3000/;
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;
}
location /legacy/ {
proxy_pass https://my.domain.com/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host https://my.domain.com/;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache_bypass $http_upgrade;
}
However, whenever I go to my.domain.com/leagcy/ I get a 400 error directly back and no redirection or proxying taking place. Any ideas as to what I have done wrong?
You probably use regular expressions in server_name and you're sending requests without setting Host header to any value.
$host variable contains in this order of precedence: host name from the request line, or host name from the 'Host' request header field, or the server name matching a request
Which causes invalid Host header to be send to upstream.

Nginx redirect with parts of URL

I have a redirect problem with nginx and I really don't know how to do it.
The problem is that I need to redirect all the traffic to a website INCLUDING THE PATH
BASE_URL: https://example.com/auth/
For example if the request is: https://example.com/auth /some/path/
I need it to redirect to: https://newdomain.com /some/path/
In other words, I need to concatenate the part of / some / path / to the new URL.
Here is the part of my code that I have tried
location /auth/* {
proxy_pass https://newdomain.com/$request_uri;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
}
Thanks.
the issue seems to be the URL handling. You need a small regexp and get the result.
try:
location ~ ^/auth/(.*)$ {
proxy_pass https://newdomain.com/$1;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
}
You can find examples there https://www.liaohuqiu.net/posts/nginx-proxy-pass/

Resources