Trying to configure nginx to have 2 proxy passes for the same location.
Example:
domain.com to go to the homepage proxy
domain.com/user to go to user profile proxy
What i need to do:
What I tryed:
location = / { proxy_pass http://home_page; }
location ~ ^/(.+) { proxy_pass http://user_profile_page; }
If I go to domain.com/ and domain.com/username it goes to the same proxy.
I have to mention that the config has also the /auth location block with exact match "="
that has domain.com/auth, domain.com/auth/login. This location block works as expected
location ^~ /auth { proxy_pass http://auth_page; }
Related
I want to handle 2 cases: test.example.com and test.example.com/ABC.
If the entered url is the base domain (test.example.com), I want to proxy_pass a given endpoint (Let's say example.com/home).
If test.example.com/ABC is given, I want to proxy_pass to example.com/confirm/ABC
test.example.com = https://example.com/home
test.example.com/ABC = https://example.com/confirm/ABC
I made the (1) work like so:
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name test.example.com;
location / {
proxy_pass https://example.com/home;
}
}
But I couldn't figure out how to say "If $request_uri exists, proxy_pass to different endpoint". I tried:
location / {
if ($request_uri) {
proxy_pass https://example.com/confirm/$request_uri;
}
proxy_pass https://example.com/home;
}
How can I achieve this?
$request_uri always exists and is never empty. The URI for the root of your domain is a single / (even if it's not displayed in the browser's address bar).
The location block which matches that URI is location = /. See this document for details.
For example:
location = / {
proxy_pass https://example.com/home;
}
location / {
proxy_pass https://example.com/confirm/;
}
The first location block only matches the root URI /, and the second location block matches any other URI. The remainder of the URI is automatically appended to /confirm/. See this document for details.
I have this url
https://example.com/image/test.jpg
And i would like to proxy with Nginx the image at this url
https://cdn0.tnwcdn.com/wp-content/blogs.dir/1/files/2018/02/google-pacman-796x419.jpg
without the url changing i.e. staying the same as this https://example.com/image/test.jpg
What would you put in the location block to get this to happen?
You can use the proxy_pass parameter in your nginx config:
server {
# ...
server_name example.com;
# ...
location /image/test.jpg {
proxy_pass https://cdn0.tnwcdn.com/wp-content/blogs.dir/1/files/2018/02/google-pacman-796x419.jpg;
}
}
Only need to proxy pass remaining url when match to location
location /blog { proxy_pass http://example.com }
i.e if somebody requests /blog/page1/temp.html they are getting proxy passed to example.com/blog/page1/temp.html
I want to change it to example.com/page1/temp.html
I want to change example.com/blog/page1/temp.html to example.com/page1/temp.html
Specify uri into proxy pass directive (/ after hostname in this case):
location /blog/ {
proxy_pass http://example.com/;
}
Or use rewrite like this:
location /blog {
rewrite /blog/([^/]+) $1;
proxy_pass http://example.com
}
So I currently have a site at http://www.example.com and I would like that all requests from
http://www.example.com/api/v2/<anything>
to be proxied to
http://api.v2.com/<anything>
Note that http://api.v2.com does not have a base path.
My config:
server {
listen 80;
server_name www.example.com;
location /api/v2/ {
proxy_pass http://api.v2.com;
}
location / {
index index.html;
root /var/www/example.com/htdocs;
}
}
This way, however, the requests are being proxies to http://api.v2.com/api/v2/<anything> keeping the original path, and not http://api.v2.com/<anything>.
I've already noticed that:
location /api/v2 {
proxy_pass http://api.v2.com/api;
works as desired - http://api.v2.com/api/v2/<anything> proxies to http://api.v2.com/api/<anything>.
Is there a way to achieve the first behavior (that is , http://api.v2.com/api/v2/<anything> to http://api.v2.com/<anything>)? Do I have to use a rewrite? I know it it related to normalized URIs and that is expected, just would like to know if there's a way to do it.
If you want to remove the proxy path from resulting url to the server, either you should have the uri part in the proxy url, or you should specify the rewrite part.
In this specific case, it should be like as follows,
location /api/v2/ {
rewrite /api/v2/(.*) /$1 break;
proxy_pass http://api.v2.com/;
}
for more information visit nginx documentation https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass
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.
server {
listen 80;
server_name www.example.com;
location /api/v2/ {
proxy_pass http://api.v2.com/;
}
location / {
index index.html;
root /var/www/example.com/htdocs;
}
}
Source: http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass
I need to proxy a couple of urls to different hosts. Actually, I'm using the same host with different port to test my nginx configuration. This is my virtual host definition:
server {
listen 8081;
server_name domain.com;
location /Plasmid/ {
proxy_pass http://localhost:8000/Plasmid/;
}
location /_community/ {
proxy_pass http://localhost:8082/comments_api/ ;
}
location / {
# rewrite cq_user_authenticated===(.*)/(.*)/iuuid=(.*)/commenti.html$ /Plasmid/comments/legacy/$3/$1 ;
# rewrite querystring===(.*)$ /Plasmid/comments/legacy/$1 ;
# rewrite cq_user_authenticated===([^&]*)&/.*uuid=([^/]*) /comments_api/legacy/$2 ;
# rewrite userdetails(.*) /Plasmid/comments/user_details ;
root html;
index index.html index.htm;
}
}
Of course my hosts file has mapping for the domain.com
When I call the url: http://domain.com:8081/Plasmid/default/page/12 I get an http 404
If I remove the second location from my configuration:
location /_community/ {
proxy_pass http://localhost:8082/comments_api/ ;
}
I get the resource I want, but some part are missed since the are hosted on a different platform:
[error] 1033#0: *1 open() "/usr/local/Cellar/nginx/1.2.6/html/_community/content
How can I resolve this issue?
Do a little change:
location ^~ /Plasmid/ {
proxy_pass http://localhost:8000/Plasmid/;
}
location ^~ /_comunity/ {
proxy_pass http://localhost:8082/comments_api/;
Why is that? Because ^~ means starts with and when you request for page:
http://domain.com:8081/Plasmid/default/page/12
it fit to that rule. In your configuration you are using no mark and something like this:
location /anylocation
and it looks like your nginx prefer rule
location / {
than
location /Plasmid
and
location /_comunity
because it's using root directive and searching for _community/content in html folder (as you get in error message).
In other words ^~ has greater priority than no mark. One thing that could also help is to add break directive after each proxy_pass directive;