Asp.Net Core 2 SignalR Ubuntu Xamarin Android - nginx

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

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.

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

nginx gives 400 bad request for urls with whitespace

I have nginx proxying requests to a node.js web app in the backend on a Ubuntu box. For requests to api.example.com with whitespace in the url get a 400 bad request error. Think the problem line of code is:
proxy_pass http://127.0.0.1:3002$uri$is_args$query_string;
however not sure how to rewrite it.
This is how the site configuration at /etc/nginx/sites-available/default looks:
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://127.0.0.1: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;
}
}
server {
listen 80;
server_name api.example.com;
location / {
proxy_pass http://127.0.0.1:3002$uri$is_args$query_string;
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 multiple apps on nginx

I have been working to get a couple asp.net core webapps running on EC2 ubuntu instance using nginx and supervisor. I am successful in running one app at a time and by simply swapping my port in my nginx setting and reloading I can swap between the running .netcore apps running on 5000 and 5001. I cannot seem to figure out the nginx settings to make them both work at a path, ie: hostname/app1, hostname/app2.
Here is my Nginx Config. Could anyone point to something I have done wrong? My supervisor is running both apps I can verify that by looking at the logs and also changing the port in the default location "/".
server {
listen 80 default_server;
listen [::]:80 default_server;
# 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;
# }
location /app1 {
rewrite ^/app1(.*) /$1 break;
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;
}
location /app2{
rewrite ^/app2(.*) /$1 break;
proxy_pass http://localhost:5001;
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 do not have a default route simple because I don't have anything to put there yet.
Looks like the solution was trailing slashes on the location and proxypass
server {
listen 80 default_server;
listen [::]:80 default_server;
# 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;
# }
location /app1/ {
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;
}
location /app2/ {
proxy_pass http://localhost:5001/;
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;
}
}

nginx as a reverse proxy

I'm trying to use nginx as a reverse proxy to a couple of web applications deployed within docker containers. I can only expose port 80 from the docker server, and want to allow access to shipyard and rabbitMQ management web app.
Ideally, users could access the services via:
http[:]//10.10.10.1/shipyard/
http[:]//10.10.10.1/rabbitmq/
After quite a bit of research, trial and error this is my nginx config:
upstream rabbitmq {
server 127.0.0.1:8888;
}
upstream shipyard {
server 127.0.0.1:8080;
}
server {
listen 80;
server_name 10.10.10.1;
location /rabbitmq/ {
proxy_http_version 1.1;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_pass http://rabbitmq/;
proxy_redirect default;
}
location /shipyard/ {
proxy_http_version 1.1;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_pass http://shipyard/;
proxy_redirect default;
}
}
When I access either path the apps I run into a couple of problems, which I believe maybe related:
shipyard:
Tries to load files from http[:]//10.10.10.1/api/containers, when it should be http[:]//10.10.10.1/shipyard/api/containers
rabbitmq:
Everything appears to work, until the server attempts to make a restful call:
+++ PUT: http[:]//10.10.10.1/rabbitmq/api/queues///test2
The server response with 405 (Not Allowed). At first I thought the /// was the problem, that is how the rabbitMQ management application works.

Resources