I'm new to Nginx, I'm trying to do proxy_pass for a specific format of URL.
For example "http://example.com/sync/test" is the URL accessed by the user, internally the URL has to do proxy_passs to apache server like, "http://localhost:8000/test" need to exclude sync, but now it's` redirecting has "http://localhost:8000/sync/test".
Is there any option to split and get parameters after sync,
Suggestions or solution is appreciated
Rewrite the url by removing /sync/ from the url
location / {
rewrite ^/sync/(.*) /$1 break;
proxy_pass http://localhost:8000;
}
Related
Currently i'm using nginx server and using nodejs server as reverse proxy.
I want to make the nginx server proxy_pass different server by location.
So, lets say my domain is subdomain.domain.com.
When loading subdomain.domain.com/, this should serve static files.
When loading subdomain.domain.com/example1/req1/req2, this should route to 127.0.0.1:3000/req1/req2.
When loading subdomain.domain.com/example2/req1/req2, this should route to 127.0.0.1:8080/req1/req2.
But my configuration routes subdomain.domain.com/example1/req1/req2 to 127.0.0.1:3000/example1/req1/req2 resulting error. (nodejs returns Cannot GET /example1)
How should I write this nginx conf file properly?
Try use rewrite directive in location block.
Something like:
location /example1/ {
rewrite ^/example1/(.*)$ /$1 break;
proxy_pass http://xxxxxx;
}
You can check documents related to rewrite module at http://nginx.org/en/docs/http/ngx_http_rewrite_module.html
You need to add below code snippet to your nginx.conf.
server subdomain.domain.com;
location / {
rewrite ^/example1/(.*)$ /$1 break;
proxy_pass http://127.0.0.1:3000;
}
}
Can Nginx rewrite a URL then use it as a proxy pass?
For example when I visit http://localhost/downloads/example-com/pc.html it should proxy to http://example.com/pc.html
Basically, I want to use my domain to download a file from another website that is why I am doing this
Yes you can rewrite url and proxy_pass to rewritten url.
But as per what you want to achieve, this simple solution should also work :
location ~ /example-com/(.*)$ {
proxy_pass http://example.com/$1;
}
if you want you achieve it by rewriting only then you can try following one :
location ~ /example-com/ {
rewrite /example-com/(.*) /$1 break;
proxy_pass http://example.com;
}
UPDATE :
pass another website url as query param like below :
http://yourdomain/downloads/data?data_url=example.com/pc.html
and change your nginx configuration as below.
location ~ /downloads/data {
set $data_url $arg_data_url;
proxy_pass http://$data_url;
}
after this configuration change you might get resolver issue as we are passing hostname in proxy_pass at runtime. To solve resolver issue you can follow this link : https://runkiss.blogspot.com/2020/01/using-nginx-authrequest-to-proxy-to.html
We are currently getting web requests with www.domain.com//index.php which are resolving, but causing issues with google. How can we rewrite the request to catch these and redirect to www.domain.com/index.php
Thanks
By default ngingx merges double-slashes and urls like www.domain.com//index.php works well.
You can turn off merging and make rewrite rule with redirection:
merge_slashes off;
rewrite (.*)//(.*) $1/$2 permanent;
I have this location element:
location ~* ^/publicapp {
proxy_pass https://myserver.domain.local;
}
The server myserver.domain.local hosts a web application located under /myapp.
I want to make it publicly available via https://www.mywebsite.com/publicapp. How do I tell nginx to translate /myapp to /publicapp?
Please keep in mind that I use ~* to allow case-insensitivity. Thus, I cannot use a URI with proxy_pass.
Kind regards,
Kevin
Try this:
location ~* /publicapp/ {
rewrite ^/publicapp/(.*)$ /myapp/$1 break;
proxy_pass https://myserver.domain.local;
}
This will rewrite your path and use new one at the .local server.
It works using
rewrite ^/publicapp/(.*) /myapp/$1 break;
At least it does with my very simple application.
Now I have to figure out how to do proper link translation (sorry for using ISA Server/TMG terms, don't know if it's the same in nginx).
Thanks to pythagor :-)
edit:
Works only if I keep a trailing slash after the url in the browser (https://www.mywebsite.com/publicapp/).
another edit:
To make sure URLs end with a slash:
rewrite ^([^.]*[^/])$ $1/ permanent;
Taken from: here (first answer)
It seems Nginx it always un-encodes urls when used with a regular expression. I have a rewrite rule:
location /api/ {
rewrite /api/(.*)$ $1 break;
proxy_pass http://127.0.0.1:8000/$1;
}
I would like to remove the api from the usl but keep the rest of the path. Part of the path is an email address someone#somewhere.com. I am passing someone%40somewhere.com but Nginx is turning it back with the # sign.
The correct answer seem to be
location /api/ {
rewrite ^ $request_uri;
rewrite ^/api/(.*) $1 break;
return 400;
proxy_pass http://127.0.0.1:8000/$uri;
}
See Nginx pass_proxy subdirectory without url decoding for full answer and original author.
(I realize this question is older than the one I referenced but I found this in google search and may not be the last one, so ...)
That is how Nginx handles urls. You can bypass it by changing your web application to escape the "%" character as "%25" and pass someone%2540somewhere.com.
This will be unescaped as someone%40somewhere.com.