I have django and flask applications running on the same machine through different ports:
Django runs on server:8088
Flask runs on server:666
In NGINX.conf I have the following code:
location / {
proxy_pass http://127.0.0.1:8088;
}
location ^/server2 {
proxy_pass http://127.0.0.1:666;
}
Django has been running for over a year successfully with this set up, where as flask is a new addition. Any time I try to access one of the Flask urls I either get a "this url does not exist on this server" error, or on occasion a 500 error (when i've been fiddling).
If I write location information for a specific flask url like this:
location /server2/splash {
proxy_pass http://127.0.0.1:666/splash;
}
It works, but I obviously don't want to write individual location information for each and every URL in the flask application.
I've gone through many of the existing Nginx location posts on stackoverflow but I've not been able to get it working. Any ideas?
Thanks!
EDIT
this is an example of what I'm trying to achieve, but rather than an individual mapping for each URL, I want a single mapping that covers all URLs:
location /server2{
proxy_pass http://127.0.0.1:666/splash;
}
location /server2/split {
proxy_pass http://127.0.0.1:666/split;
}
location /server2/export {
proxy_pass http://127.0.0.1:666/export;
}
location /server2/import {
proxy_pass http://127.0.0.1:666/import;
}
Why do you use the ^ sign? Just remove it I think it will work:
location /server2 {
proxy_pass http://127.0.0.1:666;
}
Note that when you use location /server2 the server2 is still being passed to your flask application.
In this case Nginx is doing the following:
server.com/server2 => http://127.0.0.1:666/server2
server.com/server2/splash => http://127.0.0.1:666/server2/splash
In this case location is not doing a rewrite. Always check /var/log/nginx (or wherever your logs are located) to check the requests done by the browser and what Nginx looks for after the rules for your site are processed.
What you probably want is to set an upstream directive:
upstream flask_server {
server 127.0.0.1:666;
}
server {
...
location /server2 {
proxy_pass http://flask_server;
}
}
Related
I currently have the following configuration to proxy requests off a single domain to multiple backends:
server {
listen 80;
location /team/app1/location/region/ {
proxy_pass https://this.is.the.backend.app.example/path1/healthcheck;
}
location /team/app2/location/region/ {
proxy_pass https://this.is.the.backend.app.example/path2/healthcheck;
}
location /team/app3/location/region/ {
proxy_pass https://this.is.the.backend.app.example/path3/healthcheck;
}
}
The paths are pretty arbitrary, essentially I just want to be able to proxy from:
https://proxydomain.com/team/app1/location1/region
To:
https://this.is.the.backend.app.example/path3/healthcheck
So
/team/app1/location1/region
Would need to be stripped from the request and just proxy the request to the intended backend. I assume the path is being appended in someway as I just get 404s...
I can pass to a domain without trailing path like so - but when I try and proxy to a domain with trailing paths it gets complicated:
server {
listen 80;
location /one {
proxy_pass http://x.x.x.x/;
}
location /two {
proxy_pass http://x.x.x.x/;
}
}
I have tried the following configuration too - to try and rewrite the url:
rewrite ^/team/app3/location/region/(.*)$ $1 break;
Hopefully it makes sense what I am trying to achieve - any guidance would be greatly appreciated.
Thanks
You were on the right track. The rewrite will look like:
location /team/app3/location/region/ {
rewrite ^/team/app1/location/region/(.*)$ /path3/$1 break;
proxy_pass https://this.is.the.backend.app.example/;
}
You should add the new path path3 in the rewrite rule. With this NGINX will rewrite the $uri and append it to the proxy_pass. In case app3 and path3 are a fixed pattern you can tune the location block as well as the rewrite to simplify your configuration. But I would generally start with the approach mentioned above.
AND we keep in mind. Regex matching in locations will consume CPU. So sometimes it is better to have them fixed / static. Especially if you are using them in health checks and query them every 5s or so.
Thanks for that - definitely helped getting me down the correct track:
In the end the working config was:
location /team/app3/location/region {
rewrite ^/team/app3/location/region(.*) /path3/healthcheck$1 break;
proxy_pass https://this.is.the.backend.app.example;
}
Which correctly proxied through to:
https://this.is.the.backend.app.example/path3/healthcheck
Hi Earlier I was matching location prefix uri matching to get the redirection to website www.example.com/abc/ from www.example.com/bbb/abc(requested uri).
server {
location /bbb/ {
proxy_pass http://www.example.com/;
}
}
Earlier I was requesting on www.example.com/bbb/abc and getting redirected to www.example.com/abc
as I required
However now the problem is I've resolver and I'm also using variable. This is done to ensure that nginx starts even if one of the upstream services are down and it won't show the error of
[nginx] host upstream not found
server {
location /bbb/ {
resolver 127.0.0.11 valid=some secs;
set $example www.example.com
proxy_pass http://$example/;
}
}
Now that I'm using variable, I think url location prefix match is not working as I am sending request on www.example.com/bbb/abc , and it shows the nginx 404 not found.
Can anyone guide me how redirect properly with variable and resolver included.
I'm having trouble making nginx proxy an url with variable to a service within kubernetes.
Url looks like this:
http://localhost/user?username=Dave
I expect this url to take me to a subpage /user, which will read ?username=Dave and then fetch data from a database. However this takes me to the home page of the application(/ instead of /user) and does not read the variable even though url includes /user?username=Dave.
My current nginx config file looks like this:
server {
listen 0.0.0.0:80;
server_name localhost;
location / {
proxy_pass http://${FLASK_APP}:8080/;
}
location /user {
proxy_pass http://${GO_APP}:8000/;
}
}
I have read that location /user will match the url I'm passing. What is wrong with it? Or do I need to add something to proxy_pass http://${GO_APP}:8000/; or location /user?
As noted in the comments, the issue arises because you are using a variable in the proxy_pass target. As also noted in the comments, this question is related. As the answer referencing the docs states:
A special case is using variables in the proxy_pass statement: The
requested URL is not used and you are fully responsible to construct
the target URL yourself.
This means that you either need to use a static proxy_pass target, such as
// note that I added the forward slash
location /user/ {
proxy_pass http://destination:8000/;
}
Or as an alternative, I believe you can do it this way also
location /user/ {
proxy_pass http://${GO_APP}:8000/user$is_args$args;
}
I have a docker container which serves a web application on:
my.server.domain:8080
When I request that URL with a browser, it is automatically redirected to the login page:
http://my.server.domain:8080/login
I'm trying to proxy the app so that I can avoid to use the port number.
Two possible ways to achieve that are:
1) http://my.server.domain/appname
2) http://appname.my.server.domain
Either way, will work for me.
But I'm struggling with making the right NGINX configuration.
I tried with:
location /appname {
proxy_pass http://my.server.domain:8080/;
}
But when I load http://my.server.domain/appname it gets redirected to http://my.server.domain/login which doesn't exist.
If I use / it works as expected:
location / {
proxy_pass http://my.server.domain:8080/;
}
But that's not what I need.
I can add more directives like:
location /login {
proxy_pass http://my.server.domain:8080/login;
}
But then it will fail on the next redirect and so on ..
I try to setup nginx such that it proxies requests to multiple locations. E.g. /location1 and /location2 should both be proxied to http://localhost:8080. I just can't figure out how to configure this without using multiple location blocks. I already tried:
location /(location1|location2) {
proxy_pass http://localhost:8080/
}
which will only give 404s. And I've also tried:
location ~ /(location1|location2) {
proxy_pass http://localhost:8080/
}
Which will thrown an error that regular expressions are not allowed with proxy pass.
Is it possible to configure this proxy without having to create multiple location blocks?
Apparently is missing a slash and a ';'. Try this:
location ~ (/location1|/location2) {
proxy_pass http://localhost:8080;
}