NGINX location block looking for url containing a # - nginx

Good afternoon,
I currently have a problem trying to proxy_pass to a url that contains a # sign. I'm assuming it's causing me issues since it's the comment character
the url that works is http://ip:port/remotelogin/#/
and I would like to show up as https://proxy ip/remotelogin/#/
location /remotelogin/#/ {
proxy_http_version 1.1;
proxy_pass "http://ip:port/";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto http;
}

Just replace /remotelogin/#/ with /remotelogin/.

Related

Nginx proxy pass with regex is not working

Essentially, I would like to be able to proxy https://test.myurl.com/proxy/12345 to https://12345.myurl.com:8443, however, I would like 12345 to be arbitrary.
I have been able to get the proxy pass working with the following (i.e. statically assigned):
location /proxy/12345/ {
proxy_pass https://12345.myurl.com:8443/;
proxy_redirect off;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header Host "test.myurl.com";
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
}
However, making it arbitrary does not seem to work:
location ~* ^/proxy/([0-9]+)/ {
proxy_pass https://$1.myurl.com:8443/;
proxy_redirect off;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header Host "test.myurl.com";
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
}
While Nginx accepts my configuration, I get a 404 Not Found. I have looked at a number of other related questions and played around with the config to no avail.

Nginx changes POST to GET using proxy_pass

I want to use Nginx create a gateway to receive requests and pass them along to a network of microservices.
What I need Nginx to do is just act as a proxy server, taking the requests, passing them along to whatever service, and returning the response without any changes.
This is my configuration for my local setup:
server {
listen 8080;
location /api/register/ {
proxy_pass http://micro_auth_backend:8082;
}
location /api/location/ {
proxy_pass http://localhost:8088;
}
}
It works correctly for GET requests, but when doing a POST call, the server will always receive a GET response.
I have tried adding some more configs inside the location, such as this example below, but so far nothing has worked:
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
Any suggestions would be appreciated.
Thank you
Just removed the trailing slash on location:
location /api/register {
proxy_pass http://micro_auth_backend:8082;
}
Now it works.
you can add this code to your nginx.conf file. It works for me.
location /api/register/ {
proxy_pass http://localhost:8082;
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;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location /api/location/ {
proxy_pass http://localhost:8088;
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;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}

Variable in nginx proxy_pass causes MIME type issue

I've googled the topic at length all evening and can't seem to make sense of this. My situation is the following: I have a NAS on my network (hostname MYHOSTNAME) and I also have a nginx reverse proxy, on another machine. I want to use a variable for the proxy_pass section of my configuration file such that in case the NAS is offline when the proxy starts, it doesn't crash. Basically:
location /MYNAS {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_redirect off;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_pass http://MYHOSTNAME:1234/;
works just fine. But the following:
location /MYNAS {
set $VAR_HOSTNAME MYHOSTNAME;
resolver 192.168.x.x;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_redirect off;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_pass http://$VAR_HOSTNAME:1234/;
gives me trouble. The webpage loads an empty page, and in the browser console, I can see:
Refused to execute script from 'something' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.
mydynamicdns/:1 Refused to execute script from 'somethingelse' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.
desktop.js?v=1562143318:3 Uncaught TypeError: Cannot read property 'diskless' of undefined
at _S (desktop.js?v=xxx:x)
at aa.defaultCSSSelectors (desktop.js?v=xxx:x)
at desktop.js?v=xxx:x
at ext-all.js?v=xxx:x
at b (ext-all.js?v=xxx:x)
I'm at a loss for new ideas of how to make this work... Thanks in advance guys, any help would be greatly appreciated.
I don't think I can quite explain the technical details of why, but by turning on debugging in nginx and comparing with the usecase that works, I eventually figured that I needed to nullify the /MYNAS/ piece. The key is the rewrite line below:
location /MY_NAS/ {
set $MY_NAS http://MYNAS:1234;
resolver 192.168.x.x;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_redirect off;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
rewrite /MY_NAS/(.*) /$1 break;
proxy_pass $MY_NAS;
}
Hopefully that will help someone else one day and save you the headache.

Allow access service / proxy_pass only from a certain URL in nginx

I have a website running at https://mywebsite.com. I also install another application (Minio) on the same server running at port 9000.
NOTE: When access Minio via myServerIP:9000, it automatically goes to myServerIP:9000/minio/. If I haven't logged in yet, then I will be redirected to myServerIP:9000/minio/login. If I go to myServerIP:9000/minio/anyRandomPath, I will also be redirected to the login page or another default page (if I have already logged in)
Now I want to access Minio via address https://mywebsite.com/files, so I setup my nginx as below
location /files/ {
proxy_pass http://127.0.0.1:9000/minio/;
proxy_set_header URI_REQUEST_ORIGIN $request_uri;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
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 X-Forwarded-Ssl on;
proxy_set_header X-Forwarded-Proto $scheme;
}
This doesn't work (it shows a blank page with title "Minio login"). When I check the browser's console, it says there's an error message Uncaught SyntaxError: Unexpected token < with a link to https://mywebsite.com/minio/index_bundle_xxx.js. So I try adding another location block in my nginx.conf:
location /minio/ {
proxy_pass http://127.0.0.1:9000/minio/;
proxy_set_header URI_REQUEST_ORIGIN $request_uri;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
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 X-Forwarded-Ssl on;
proxy_set_header X-Forwarded-Proto $scheme;
After that, it works as expected. But the problem is that now I can access Minio via both /minio and /files
Now how can I block direct access to Minio via https://mywebsite.com/minio?
I tried some workaround like adding below location blocks:
location =/minio/ {
return 404;
}
location =/minio/login/ {
return 404;
}
But this is clearly a bad way and also not efficient, since I can just go to mywebsite.com/minio/typeAnythingHere to be redirected to the login page.

How in Nginx to do rewrite without changing URL in address bar

I've been trying to do rewrite in Nginx:
domain.com/one/two -> onetwo.domain.com. The URL that user sees should not be changed in address bar.
This code does not work correctly - it changes URL in address bar
rewrite ^/one/two/ http://onetwo.domain.com/ last;
What solution must be there?
Thanks.
This isn't possible, because you're changing hostnames. Browser security is tied to it, as is webserver configuration.
You can rewrite URLs within same hostname, but changing hostnames requires redirect or using a frame.
Use proxy pass:
location /one/two/ {
proxy_pass http://onetwo.domain.com/;
include proxy.conf;
}
Where proxy.conf is where you keep your proxy settings, stuff like:
proxy_ignore_client_abort off;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_connect_timeout 120;
proxy_send_timeout 900;
proxy_read_timeout 900;
proxy_headers_hash_max_size 1024;
proxy_headers_hash_bucket_size 128;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header REQUEST_SCHEME $scheme;
proxy_set_header X-Forwarded-Ssl on;
proxy_set_header X-Forwarded-Protocol $scheme;
proxy_set_header X-scheme $scheme;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header 'SERVER_PORT' $server_port;

Resources