nginx gives 400 bad request for urls with whitespace - nginx

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

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.

Why Nginx is redirecting my subdomain to the domain root?

I am not a server admin so I am still learning. I am trying to create a subdomain for my website but every time when I am opening the subdomain it is pointing to domain root folder. I am trying to understand what am I doing wrong.
I have created the domain and subdomain using AWS Route 53 using this https://dev.to/arswaw/create-a-subdomain-in-amazon-route53-in-2-minutes-3hf0
So my path is like this
- var
- www
- html
- webiste
- subdomains
- kiosk
Here is my website confing
server {
root /var/www/html/website;
server_name example.com www.example.com;
location / {
proxy_pass http://127.0.0.1:9000;
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;
}
}
Here is my subdomain config
server {
root /var/www/html/subdomains/kiosk;
listen 80;
server_name kiosk.example.com;
location / {
proxy_pass http://127.0.0.1:9000;
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;
}
}

Nginx serves root of one subdomain instead of root of another subdomain

I have two subdomains, app.mydomain.com and v1.api.mydomain.com, pointing to the same IP. I want app.mydomain.com to be proxied to localhost:5000 and v1.api.mydomain.com to be proxied to localhost:1337.
The following configuration does it (app.mydomain.com/... is served by localhost:5000 and v1.api.mydomain.com/... is served by localhost:1337) except for one thing: the root of v1.api.mydomain.com (with or without a trailing slash) isn't served by localhost:1337, it is served by localhost:5000 (app.mydomain.com) and therefore it doesn't display what I expect. How is this possible?
My first configuration file in sites-enabled:
server {
listen 80;
server_name app.mydomain.com;
location / {
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;
}
My second configuration file in site-enabled:
server {
listen 80;
server_name v1.api.mydomain.com;
location / {
proxy_pass http://localhost:1337/;
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;
}
}
To make sure that the problem wasn't a redirection of some sort by the server running on localhost:1337, I shut it down and the problem is still there, so it really is something with my nginx configuration. Thanks for helping.

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

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

Resources