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

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.

Related

how to proxy_pass api path to port with nginx

I want to proxy_pass the root location (/) to the port 3000 and the /api location to the port 5000, is completely possible, right?
my nginx config file:
server {
listen 80;
server_name mywebsite.com;
location /api {
proxy_pass http://localhost:5000;
}
location / {
proxy_pass http://localhost:3000;
}
}
if I do the api request locally i can get the expected output:
myuser#myserver [conf.d]# curl localhost:5000
Hello, World!myuser#myserver [conf.d]#
but with an api client don't, and proxy_pass from the root path to the port 3000 works fine in the browser and the api client
Notes:
im not forgetting reload nginx with sudo systemctl reload nginx
the firewall is allowing traffic in both ports, im using ufw
the server OS is centos 7
I think you are using React and nodejs. I use below config.
server {
listen 80;
server_name mywebsite.com;
location / {
# My react
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 /api{
# This is my nodejs API
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;
}
}

Can not proxy assets requests to file storage with nginx

I am trying to proxy requests to digital-ocean space storage, example: GET http://example.com/download/image_id.jpg ---> https://bucket-name.fra1.cdn.digitaloceanspaces.com/images/image_id.jpg, but I get
<Error>
<Code>NoSuchBucket</Code>
<BucketName>example.com</BucketName>
<RequestId>tx000000000000018d441fd-005d582ff2-1b7a15-fra1a</RequestId>
<HostId>1b7a15-fra1a-fra1</HostId>
</Error>
Here is my full configuration:
server {
listen 80;
listen [::]:80;
root /var/www/example.com/html;
index index.html index.htm index.nginx-debian.html;
server_name example.com www.example.com;
location / {
proxy_pass http://localhost:8080;
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 /download/ {
proxy_pass https://bucket_name.fra1.digitaloceanspaces.com/images$request_uri;
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;
resolver 8.8.8.8;
}
}
I have already tried different options with/without trailing slash, but after reading docs carefully, I think now it is optimal, configuration,
also I tried to debug where I am proxied replacing
proxy_pass https://bucket_name.fra1.digitaloceanspaces.com/images$request_uri;
with
proxy_pass https://google.com/images$request_uri;
and it gave me what I expected.
According to docs, what I want to do looks like this:
location /download/ {
proxy_pass https://bucket_name.fra1.digitaloceanspaces.com/images;
...
So that download part is replaced with images, but, it didn't work.
At the moment (after debugging with google) I am almost confident that url after proxie_pass is equal to https://bucket_name.fra1.digitaloceanspaces.com/images/image_id.jpg, so maybe the key problem is in wrong approach to "spaces"
P.S. I'm open to advices about better way to log/debug proxy_pass value .
So... proxy_set_header Host $host;was breaking everything. Removing it fixed proxing.

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 redirect for menu items in application

So have an Nginx server called proxy1 which is supposed to redirect to two applications (app1 and app2).
For the first proxy_pass I use:
server {
listen 80;
server_name proxy1;
location /app1 {
proxy_pass http://host1:9090/admin;
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;
}
}
This directs me to the app1 admin page which has some url options
When I hover over them i see they are:
http://proxy1/admin/metrics?pretty=true
http://proxy1/admin/ping
http://proxy1/admin/threads
http://proxy1/admin/healthcheck?pretty=true
How can I configure Nginx to redirect these menu urls to the correct app (app1)?

NGINX redirecting to root for escaped_fragment URI

I have a very weird situation where NGINX (used as proxy for node app) is redirecting all ?_escaped_fragment_= to root (/) when using https://domain.com?_escaped_fragment_=/app/someurl BUT when I use https://dev.domain.com?_escaped_fragment_=/app/someurl all works fine.
Just want to make it clear that prerenderer works well and I have tested it both directly on machine as well as using dev subdomain.
I can give the original URL for those that would like to check things live.
Thanks a million guys :)
server {
server_name domain.com www.domain.com;
listen 80;
return 301 https://domain.com$request_uri;
}
server {
server_name domain.com;
listen 443 ssl;
ssl_certificate /etc/nginx/ssl/***.crt;
ssl_certificate_key /etc/nginx/ssl/***.key;
location / {
proxy_pass http://x.x.x.x:4567;
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 {
server_name dev.domain.com;
location / {
proxy_pass http://x.x.x.x:4567;
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;
}
}
For those of you who had a similar experience maybe also with apache please note that it's not NGINX but the app where you need to specify to the prerender middleware .set('protocol', 'https'));
protocol
Option to hard-set the protocol. Useful for sites that are available
on both http and https.
app.use(require('prerender-node').set('protocol', 'https'));

Resources