what's the difference beteween these proxy_pass example?
location /proxyApi/ {
proxy_pass http://xxx.xxx.xxx.xxx:xxxx/;
}
location /proxyApi {
proxy_pass http://xxx.xxx.xxx.xxx:xxxx;
}
location /proxyApi/ {
proxy_pass http://xxx.xxx.xxx.xxx:xxxx;
}
location /proxyApi {
proxy_pass http://xxx.xxx.xxx.xxx:xxxx/;
}
In the 1st and 4th example, you have provided an optional URI to the end of the proxy_pass statement. This causes the proxy_pass to change the URI in a similar way to the alias directive by a direct substitution of the text in the location statement. Caveats are that the URI has not already been changed within the location block (e.g. with a rewrite...break), and that the location is a prefix location (as in your question) and the proxy_pass contains no variables (as in your question).
The URI /proxyApi/foo/bar will pass upstream to http://xxx.xxx.xxx.xxx:xxxx/foo/bar in case 1, and http://xxx.xxx.xxx.xxx:xxxx//foo/bar in case 4. Notice the error with the double // in case 4.
Cases 2 and 3 simply pass the URI upstream without modification. Both pass the URI /proxyApi/foo/bar to http://xxx.xxx.xxx.xxx:xxxx/proxyApi/foo/bar.
The other difference is URIs such as /proxyApi and /proxyApifoo, which will only match the location blocks in case 2 and 4.
See this document for details.
Related
I need to get the host address from the request and redirect to another host with the rewritten address, but in all possible ways I failed
Here's an example (one of the attempts):
location / {
proxy_pass http://...;
...
}
location /service/(.*)$/ {
rewrite /service/$1/(.*)$ /$2 break;
proxy_pass http://$1.localhost:8080/;
...
}
Works only if hardcoded:
location /service/hostaname/ {
rewrite /service/hostaname/(.*)$ /$1 break;
proxy_pass http://hostaname.localhost:8080/;
...
}
But this is not a suitable solution for me
Help meeeeeee, thanks!
Your location syntax is invalid, as it is missing the regular expression operator. The rewrite statement contains an invalid regular expression.
You do not need the rewrite statement as you can extract the parts of the URI from the location statement.
For example:
location ~ ^/service/([^/]+)/(.*)$ {
proxy_pass http://$1.localhost:8080/$2;
}
You may need to define a resolver statement as the proxy_pass contains variables. See this document for details.
I have a lot of code about location, which is very complicated and difficult
location /addressJson/0{
proxy_pass http://wangzc.wang:1337/0;
}
location /addressJson/1{
proxy_pass http://wangzc.wang:1337/1;
}
location /addressJson/2{
proxy_pass http://wangzc.wang:1337/2;
}
location /addressJson/3{
proxy_pass http://wangzc.wang:1337/3;
}
location /addressJson/4{
proxy_pass http://wangzc.wang:1337/4;
}
location /addressJson/5{
proxy_pass http://wangzc.wang:1337/5;
}
location /addressJson/6{
proxy_pass http://wangzc.wang:1337/6;
}
location /addressJson/7{
proxy_pass http://wangzc.wang:1337/7;
}
How can I abbreviate?
You do not need to use a regular expression location to match any URI that begins with the same text. Use a prefix location. The location that matches all of the locations in your question is:
location /addressJson/ { ... }
The optional uri part of the proxy_pass value performs an aliasing function, which will substitute the prefix (specified in the prefix location) with the uri part in the proxy_pass value. See this document for details.
For example:
location /addressJson/ {
proxy_pass http://example.com:1337/;
}
If the original requested URI is /addressJson/foo then the /addressJson/ part is removed and the / prepended before sending the revised URL as http://example.com:1337/foo.
Note that the location value should end with / in order that the text substitution works correctly.
I am trying to use variable to set the hostname in a proxy_pass, but once I try that, the path after the location is ignored.
If I try and get localhost:8001/dirA/x/y/z.html. The following returns the file from http://server1:8888/dirB/dirC/x/y/z.html. This is what I expect to happen.
location ^~ /dirA/ {
proxy_pass http://server1:8888/dirB/dirC/;
But if I try the following config which is just using a variable for hostname... and try and get localhost:8001/dirA/x/y/z.html
location ^~ /dirA/ {
set $endpoint server1;
proxy_pass http://$endpoint:8888/dirB/dirC/;
I get http://server1:8888/dirB/dirC/index.html returned instead.
That's just how proxy_pass works. If you use a variable in the value, you need to provide the entire URI. See this document for details;
You could use a regular expression location. For example:
location ~ ^/dirA/(.*)$ {
set $endpoint server1;
proxy_pass http://$endpoint:8888/dirB/dirC/$1;
}
Note that the order of regular expression locations is significant. See this document for details.
Alternatively, a rewrite...break should also work.
location ^~ /dirA/ {
set $endpoint server1;
rewrite ^/dirA/(.*)$ /dirB/dirC/$1 break;
proxy_pass http://$endpoint:8888;
}
I have been struggling with setting up Nginx for our use case.
When I set up Nginx with the following config:
location /dep-core {
proxy_pass http://node-server:7070/;
}
and call the server with following endpoint:
<END-POINT>/dep-core/api/login
the call is redirected to
<ADDRESS-AFTER-RESOLUTION>//api/login
with two leading //s.
and when I remove the trailing / in proxy_pass after 7070:
location /dep-core {
proxy_pass http://node-server:7070;
}
the call is redirected to
<ADDRESS-AFTER-RESOLUTION>/dep-core/api/login
with leading /dep-core appended.
I want my call to redirect my call to:
<ADDRESS-AFTER-RESOLUTION>/api/login
What would be the standard way to achieve this??
For correct translation from /dep-core/foo to /foo, the location directive requires a trailing /.
For example:
location /dep-core/ {
proxy_pass http://node-server:7070/;
}
See this document for details.
To translate /dep-core to /, you can use a rewrite...break with a clever regular expression in the second block of your question. But a simple solution is to add an exact match location for that single edge case.
For example:
location = /dep-core {
rewrite ^ $uri/ last;
}
location /dep-core/ {
proxy_pass http://node-server:7070/;
}
I need to set proxy_pass for the below url pattern.
location ~ ^/hosts/bu/(.*)/app/(.*)$ {
proxy_pass http://appserver.cnma.com:3000/hosts/bu/$1/app/$2;
}
When I try with the URL it does not pass the second parameter correctly. Please let me know where I am going wrong.
Orig docs say:
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;
}
So there is enough:
proxy_pass http://appserver.cnma.com:3000$request_uri;
you can use named capture in nginx while using regular expressions
location ~ ^/hosts/bu/(.*)/app/(.*)$ {
proxy_pass http://appserver.cnma.com:3000/hosts/bu/$1/app/$2;
}
You can modify block to below like this
location ~ ^/hosts/bu/(?<loc1>.+)/app/(?<loc2>.+)$ {
proxy_pass http://appserver.cnma.com:3000/hosts/bu/$1/app/$loc2;
}