Nginx proxy_pass trailing slash does not translat location headers - nginx

I have a jellyfin server I want to proxy_pass to but when i go to 192.168.1.34/jellyfin/, I'm redirected to 192.168.1.34/web/index.html instead of 192.168.1.34/jellyfin/web/index.html. All I found online is that the trailing slash should take care of this problem but it does not work for me. I can see in the network tab that the redirect is triggered by a Location /web/index.html Header.
server {
listen 80;
server_name 192.168.1.34;
location /jellyfin/ {
proxy_pass http://192.168.1.65:8096/;
}
location /jellyfin/socket/ {
proxy_pass http://192.168.1.65:8096/socket/;
include proxy_params;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
Is there a way to make this work? (I'm only using ipv4) Thanks.

Related

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.

Nginx reverse proxy to both nextjs and react-admin

I have an existing app written in nextjs because I need SSR. Since I don't need SSR on the admin side I would like to use react-admin. I don't intent to integrate the two but instead have them run as separate processes/services on separate ports and have nginx do the proxy routing. I am having difficulty in configuring react-admin.
nextjs running on 127.0.0.1:3000
react-admin running on 127.0.0.1:3001
Configuration for nginx reverse proxy location:
server {
server_name www.mydomainname.com;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
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;
proxy_redirect off;
}
location /admin {
proxy_pass http://127.0.0.1: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;
proxy_redirect off;
}
# 301 Redirect URLs with trailing /'s
#as per https://webmasters.googleblog.com/2010/04/to-slash-or-not-to-slash.html
rewrite ^/(.*)/$ /$1 permanent;
# 301 redirect from custom redirect file
if ( $redirect_uri ) {
return 301 $redirect_uri;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/mydomiainname.com/fullchain.pem # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/mydomainname.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
I believe the nginx config to be correct. When navigating to /admin react-admin is responding with a blank "React App" page instead of the default page seen from it's root, '/', path.
I have tried setting the 'homepage': "/admin" in the react-app package.json file with no joy.
How do i configure react-app to serve pages by default to /admin instead of /?
The problem is likely that the path proxied to react-admin is /admin instead of just /. To avoid this, you want to add a trailing slash / to the end of your proxy_pass URL:
location /admin {
proxy_pass http://127.0.0.1:3001/;
...
}
This is explained in the proxy_pass section of the Nginx docs, though admittedly the language is a bit esoteric:
If the proxy_pass directive is specified with a URI, then when a request is passed to the server, the part of a normalized request URI matching the location is replaced by a URI specified in the directive:
location /name/ {
proxy_pass http://127.0.0.1/remote/;
}
If proxy_pass is specified without a URI, the request URI is passed to the server in the same form as sent by a client when the original request is processed, or the full normalized request URI is passed when processing the changed URI:
location /some/path/ {
proxy_pass http://127.0.0.1;
}

proxy pass configuration in nginx

I am a front end developer and tried my hands in nginx configuration last time which is working fine. The below is the configuration:
server {
listen 80;
server_name localhost;
access_log /var/log/nginx/localhost.access.log;
location / {
#By default route to node.js running on localhost:9000 port
proxy_pass http://localhost: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;
}
#currently only one server but will have to redirect to n hosts based on a parameter
location /hosts.json {
proxy_pass http://app-host.net:3000;
}
#currently only one server but will have to redirect to n hosts based on a parameter
location /hosts/ {
proxy_pass http://app-host.net:3000;
}
}
Now, I need to redirect to 4 different servers based on a parameter. ie if the city is Bangalore, I need to redirect to bangalore.corp.net:3000 and if the city is NewYork, then I need to redirect to newyork.corp.net:3000 and so on.
Here is somewhat I am expecting:
location /app1/hosts/ {
proxy_pass http://app1-host.net:3000;
}
#But the proxy pass should point to http://app1-host.net:3000/hosts and not http://app1-host.net:3000/app1/hosts
How can we handle such proxy pass in the nginx configuration file. Please let me know.
You have a URL of the form /app1/hosts/foo which should map to http://app1-host.net:3000/hosts/foo. The can be achieved by appending a URI in the proxy_pass directive, which will act like an alias.
location /app1/hosts/ {
proxy_pass http://app1-host.net:3000/hosts/;
}
See this document for details.

nginx http auth request module will only return default error pages

I am using nginx as a single point of entry to my entire solution.
my goal is to send some url to be authenticated before continuing.
I have found a module named: ngx_http_auth_request_module which suits right in place to solve my problem.
http://nginx.org/en/docs/http/ngx_http_auth_request_module.html
i am compiling my nginx like this: ./configure --with-http_auth_request_module
my code looks like this:
http {
server {
listen 8080 default_server;
server_name _;
location /api {
auth_request /auth;
proxy_pass http://localhost:8000;
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 = /auth {
proxy_pass http://localhost:8000/auth/user;
proxy_pass_request_body off;
proxy_set_header Content-Length "";
proxy_set_header X-Original-URI $request_uri;
}
}
}
my problem is that if my server returns and error, like 401, then a default page gets shown. what i want is to be able to return the json that was returned from my authentication service. this is useless without this. what is the point of showing just a generic 401 page? i want to return my proxied json response.
please help.
auth_request is just for authentication. This little hack should work for you
error_page 401 /auth;
After auth error it'll go to /auth location again, this time as ordinary request.

nginx subdomain to directory , too many redirect , why?

this is my config:
server {
listen 80;
server_name ~^(?<sb>.+)\.a\.b\.c\.com$;
access_log /data/logs/nginx/tas.access.log main;
location / {
proxy_intercept_errors on;
proxy_pass http://b.c/a/$sb/;
proxy_set_header Host $host;
proxy_redirect off;
}
}
and browser report to many redirects.
If, as you say, you want to proxy to localhost:8082, you need to say so in the proxy_pass line:
server {
listen 80;
server_name ~^(?<sb>.+)\.a\.b\.c\.com$;
access_log /data/logs/nginx/tas.access.log main;
location / {
proxy_intercept_errors on;
proxy_pass http://localhost:8082/a/$sb/;
proxy_set_header Host $host;
proxy_redirect off;
}
}
Without all of the information, it's hard to guess what's going on. Based on the comments, my guess is that you are using virtual hosting so that the upstream site is also served by the same nginx. So this line is the problem:
proxy_set_header Host $host;
The nginx variable $host is pointing to the current Host header (which matches the server_name). So if you set the same host header for the upstream again, then nginx will find the same location block above because nginx relies on the Host header to find the proper server. Thus the redirect loop.
Set
proxy_set_header Host your_upstream_server_name
will fix it then.

Resources