nginx proxy_pass not working with variable - nginx

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';`
}

Related

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.

How to remove first part of route in 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.

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.

Proxy_pass to second address if first isn't available (502 for instance)

When I send query to example.com/nodeService and node service on port 3001 returns 502 error, I need to redirect me on 3002 port without client application knowing about error. Is there such functionality in nginx? Thanks.
location /nodeService/ { #If this one is not available (502 error in my case)
proxy_pass http://localhost:3001/;
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 /nodeService/ { # redirect me here!
proxy_pass http://localhost:3002/;
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;
}
By default, nginX will perform in-band (passive) health checking. If the response from a particular server fails with an error, nginX will mark this server as failed and will try to avoid selecting this server for a while.
The max_fails directive by default is 1, while fail_timeout by default is 10s. The servers will be tried in sequence until a healthy one is found. If none of them is healthy - nginX will return to the client the result from the last server.
http {
upstream myapp1 {
server http://localhost:3001/ max_fails=3 fail_timeout=5s;
server http://localhost:3002/ max_fails=2 fail_timeout=6s;
}
server {
listen 80;
location /nodeService/ {
proxy_pass http://myapp1;
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;
}
}
}

Asp.Net Core 2 SignalR Ubuntu Xamarin Android

I have deployed Asp.Net Core 2 website on ubuntu with Nginx as a reverse proxy. The website works but SignalR doesn't. Same build work locally in IIS-Express. Getting following error in logs,
Terminating Long Polling connection by sending 204 response.
Following nginx configuration I am using,
server {
listen 80;
location / {
proxy_pass http://localhost:5000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
I am accessing the server from Xamarin Android.
What can be the issue? Any help would be great. Thanks in advance.
You should change you config to add additional path which needs the Upgrade header
server {
listen 80;
location / {
proxy_pass http://localhost:5000;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location /socket/path/ {
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;
}
}

Resources