grafana / grafana

The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
https://grafana.com
GNU Affero General Public License v3.0
66.34k stars 12.35k forks source link

Grafana doesn't obey root_url settings when redirecting after a successfull login. #2066

Closed zerthimon closed 9 years ago

zerthimon commented 9 years ago

It redirects to a '/' instead of what is configured in the 'root_url'

torkelo commented 9 years ago

@zerthimon are you sure you have configured nginx or apache reverse proxy correctly? Are you using version 2.0.2? a lot of poeple have put grafana behind reverse proxy and added sub url to root_url so its strange that no one has reported it before

zerthimon commented 9 years ago

@torkelo Yep, I'm using grafana 2.0.2

My Nginx is configured this way:

    location /grafana/ {
        proxy_set_header Host $host;
        rewrite ^/grafana/(.*) /$1 break;
        proxy_pass http://localhost:3000;
    }

And the root_url is:

root_url = %(protocol)s://%(domain)s:%(http_port)s/grafana/

I'm seeing this issue after signing out and logging into grafana. Everything else works fine (i just have to fix the uri in browser from '/' to '/grafana/' after logging in).

Here is the access log (see the "GET /" in the last line):

zzz.zzz.zzz.zzz - graphite [28/May/2015:16:22:28 +0300] "GET /grafana/login HTTP/1.1" 200 997 "https:/xxx.xxx.xxx/grafana/" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0"
zzz.zzz.zzz.zzz - graphite [28/May/2015:16:22:44 +0300] "POST /grafana/login HTTP/1.1" 200 41 "https://xxx.xxx.xxx/grafana/login" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0"
zzz.zzz.zzz.zzz - graphite [28/May/2015:16:22:44 +0300] "GET / HTTP/1.1" 200 542 "https://xxx.xxx.xxx/grafana/login" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0"
torkelo commented 9 years ago

@zerthimon not sure about the rewrite rule, this works for me:

location /grafana2/ { proxy_pass http://localhost:3000 }

zerthimon commented 9 years ago

@torkelo What do you have in the root_url ? When I have:

    location /grafana/ {
        proxy_pass http://localhost:3000;
    }

I'm getting only this:

{{alert.title}}

and a bunch of 404's in /var/log/grafana/grafana.log

2015/05/28 17:36:03 [I] ESC[1;31mCompleted /grafana/ 404 Not Found in 7.841934msESC[0m
2015/05/28 17:36:03 [I] ESC[1;31mCompleted /grafana/css/grafana.dark.min.5aa0b879.css 404 Not Found in 1.044431msESC[0m
2015/05/28 17:36:03 [I] ESC[1;31mCompleted /grafana/app/app.6e379bdb.js 404 Not Found in 1.025579msESC[0m
2015/05/28 17:36:12 [I] ESC[1;31mCompleted /grafana/ 404 Not Found in 3.022672msESC[0m
2015/05/28 17:36:12 [I] ESC[1;31mCompleted /grafana/app/app.6e379bdb.js 404 Not Found in 3.327384msESC[0m
2015/05/28 17:36:12 [I] ESC[1;31mCompleted /grafana/css/grafana.dark.min.5aa0b879.css 404 Not Found in 3.290403msESC[0m
torkelo commented 9 years ago

root url is default value but with granfana/ added to the end

torkelo commented 9 years ago

Just tested this and login works fine.

nginx:

location /grafana/ {
    proxy_pass                 http://127.0.0.1:3000/;

grafana ini:

[server]
root_url = %(protocol)s://%(domain)s:%(http_port)s/grafana
zerthimon commented 9 years ago

The trailing slash in http://127.0.0.1:3000/; made it work for me.

But the redirect after login still sends my browser to "/"

torkelo commented 9 years ago

strange, must be something wrong with your nginx version?

torkelo commented 9 years ago

@zerthimon did you change root_url that ends in grafana/ and did you restart the grafana backend?

zerthimon commented 9 years ago

@torkelo here is the current config

# The full public facing url
root_url = %(protocol)s://%(domain)s:%(http_port)s/grafana/
    location /grafana/ {
        proxy_pass http://localhost:3000/;
    }
zerthimon commented 9 years ago

@torkelo restarted the grafana backend and nginx ... I'm gonna analyze the traffic and report back

zerthimon commented 9 years ago

Checked with other browsers - no problems found. Cleaned cache and cookies and that solved the problem. Mozilla must have cached the wrong redirect, when I played with configuration.

@torkelo Thanks for assist, grafana is great!

torkelo commented 9 years ago

@zerthimon ah, great that you solved it

artkrz commented 9 years ago

/etc/init.d/grafana-server restart solved this issue for me

areebahmushtaq commented 9 years ago

@torkelo I am new to nginx and grafana both. I installed nginx and grafana. Grafana works great with its default settings but when I tried doing the above, unfortunately i then dint get access to grafana, it says {{alert.title}}. And if i revert back the changes, grafana works. Can you please guide me where to write this location directive location /grafana/ { proxy_pass http://localhost:3000/; } inside a new server block in nginx.conf and outside http block? or can you please show me the nginx.conf file. Am i missing something else as well?

torkelo commented 9 years ago

@areebahmushtaq

this should work in grafana.ini

root_url = http://your_nginx_and_port/grafana/

Your nginx confiug

    location /grafana/ {
        proxy_pass http://localhost:3000/;
    }
`` 
torkelo commented 9 years ago

for the nginx config, it should go inside a server block

Oneirag commented 8 years ago

Hi all I have the same problem with the login page. I also have the nginx configuration as recommended:

location /grafana/ {
        proxy_pass http://localhost:3000/;
    }

And in grafana.ini: root_url = http://mydomainname/grafana/

I don't see any issue on that configuration so I checked the js of the login page of grafana. After loging the response of the grafana server is "{"message":"Logged in","redirectUrl":"/"}" So that "redirectUrl" parameter makes the web browser redirect to the root of my server instead to the location /grafana/ After this, I surf back to the /grafana/ location of the server and I'm logged in due to the session cookie so everything works fine Did I miss any other configuration parameter that affects this redirectUrl of the response of the login page? Thanks in advance for your help

torkelo commented 8 years ago

The redirectUrl i handled in the frontend and before reloading that page the app sub url is appended. https://github.com/grafana/grafana/blob/master/public/app/core/controllers/login_ctrl.js#L84

torkelo commented 8 years ago

or wait, that does not look right. hmm.. might be a bug there

torkelo commented 8 years ago

Seems to work for me, you should not get redirectUrl unless you have a redirect cookie (tried accessing a page before logging in and you will get one).

So try clearing any cookies you have

karanp24 commented 8 years ago

Login works fine but when you try to add datasource the URL is not directed.

torkelo commented 8 years ago

The url is not directed? What do you mean?

karanp24 commented 8 years ago

So I configured the root url and nginx config as in the grafana documentation. And that works fine for login, admin, dashboard, alerting options in grafana. But, when I try to add datasource from UI the page does not show up there and same thing happens for when installing plugins. Following are the latest logs of grafana when I tried to add datasource. Also my nginx config is : location /grafana/ { proxy_pass http://grafana:3000/; ##{I have the services in the same overlay network} LOGS-------- t=2016-12-28T03:09:02+0000 lvl=info msg="Initializing HTTP Server" logger=server address=0.0.0.0:3000 protocol=http subUrl=/grafana t=2016-12-28T03:09:05+0000 lvl=info msg="Request Completed" logger=context userId=0 orgId=0 uname= method=GET path=/datasources status=302 remote_addr=10.0.0.18 time_ms=0s size=37

torkelo commented 8 years ago

@karanp24 very strange, cannot replicate and no similar reports :(

karanp24 commented 8 years ago

@torkelo thanks for the followup. I resolved the issue it was related with nginx. I have /kibana suburl in nginx as well and thats the reason nginx was confused and the kibana urls were getting higher preference. so the updated nginx conf is : location ^~ /grafana/ { proxy_pass http://grafana:3000/;

ugirirajan commented 8 years ago

@torkelo Where you able to do proxy settings in ha proxy? I am using ha proxy instead nginx.

lambdaq commented 8 years ago

Hi, I have a similar problem with static files:

Nginx conf:

location /grafana {
    proxy_pass http://internal_server_ip:3000
}

in grafana.ini:

root_url = %(protocol)s://%(domain)s:%(http_port)s/grafana

But open in browser will report these 404 errors

(index):11 GET /grafana/public/css/fonts.min.css (index):14 GET /grafana/public/css/grafana.dark.min.c7b017c5.css (index):79 GET /grafana/public/app/boot.85c49108.js

patrickleet commented 8 years ago

@lambdaq ever make any progress with that - getting the same errors with haproxy

bergquist commented 8 years ago

@lambdaq that location settings seems starnge. I would expect it to be /grafana rather then / grafana ?

http://docs.grafana.org/installation/behind_proxy

lambdaq commented 8 years ago

@bergquist Sorry It's a typo. It should be /grafana. I corrected it.

lambdaq commented 8 years ago

@patrickleet I ended up adding a rewrite like this:

location /grafana {
    rewrite ^/grafana/(.*) /$1 break;
    proxy_pass http://internal_server_ip:3000
}

It's working OK.

dpetersen commented 8 years ago

I've spent more time today fighting this than I'd care to admit. All I can say is that Grafana seems extremely touchy mounted at a subpath. I ended up falling back to proxying Grafana without the subpath; it just wouldn't work.

I think my proxy server (bitly/oauth2_proxy) is forcing a trailing slash, and Grafana refuses to deal with it, but I can't be sure. At a subpath, all I can get is a 404 page that is clearly from Grafana.

torkelo commented 8 years ago

@dpetersen strange, never heart of an issue before with putting grafana behind nginx, apache or oauth2 proxy in subpath, that is used by a lot of users.

patrickleet commented 8 years ago

The assets were not served at the subpath, although the application was, therefore the assets could not be found. Not sure about nginx but I experienced this in haproxy. The trick to get it working was to rewrite the subpath to /

Here's relevant config

frontend services
    #...

    acl url_metrics_grafana3000 path_beg /grafana/ path_beg /grafana/public path_beg /grafana/api
    use_backend metrics_grafana-be3000 if url_metrics_grafana3000

backend metrics_grafana-be3000
    mode http
    http-request set-path %[path,regsub(/grafana/,/)]
    server metrics_grafana metrics_grafana:3000
s4s0l commented 7 years ago

@patrickleet how does your root_url looks like? Also can you go to metrics_grafana:3000 directly?

Update: root_url = %(protocol)s://%(domain)s:%(http_port)s/grafana/ seems to work with @patrickleet haproxy example

gsethu commented 7 years ago

I started Grafana as below.

docker run -d -p 3000:3000 --name=grafana -e "GF_SERVER_ROOT_URL=http://server-ip/grafana" -e "GF_SECURITY_ADMIN_PASSWORD=secret" grafana/grafana

Accessing Grafana via http://server-ip:3000/grafana throws

{{alert.title}} error.

Am I missing anything in the configuration or setup ?

From the log message:

msg="Initializing HTTP Server" logger=server address=0.0.0.0:3000 protocol=http subUrl=/grafana

torkelo commented 7 years ago

You specifed http://grafana as root url, so that is what Grafana expects as the url used on browser.

For putting grafana under suburl /grafana you need a reverse proxy like nginx

gsethu commented 7 years ago

Thanks for your reply. I tried adding the host behind AWS ALB (path based routing) and added /grafana as the path. But still the same issue exists. Is that anything need to be configured for accessing Grafana using AWS ALB i.e AWS-ALB-Name/grafana throws the above error.

But if I start with default root_url (no sub_urls), I am able to access it via AWS ALB with the corresponding port number i.e. AWS-ALB-Name:3000 works fine.

torkelo commented 7 years ago

Not sure how AWS ALB subpath reverse proxy works, I know that it works with apache and nginx.

On Mon, Mar 6, 2017 at 6:26 AM, gsethu notifications@github.com wrote:

Thanks for your reply. I tried adding the host behind AWS ALB (path based routing) and added /grafana as the path. But still the same issue exists. Is that anything need to be configured for accessing Grafana using AWS ALB i.e AWS-ALB-Name/grafana throws the above error.

But if I start with default root_url (no sub_urls), I am able to access it via AWS ALB with the corresponding port number i.e. AWS-ALB-Name:3000 works fine.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/grafana/grafana/issues/2066#issuecomment-284306119, or mute the thread https://github.com/notifications/unsubscribe-auth/AAAq9_biz347EXMFgSQX2BlDzNG_3tnHks5ri5kJgaJpZM4EthQT .

linusguan commented 7 years ago

@gsethu @torkelo The ALB path based routing does not do path manipulation so the request Grafana got is http://someip:3000/grafana. I think there has to be a reverse proxy in the middle to strip out /grafana because there is no way to configure Grafana to serve a sub path directly.

dereulenspiegel commented 7 years ago

@dpetersen Sorry for digging up this issue although it is closed. But I am also trying to run grafana behind oauth2_proxy from bitly. Other applications are working quite well like this, but with grafana all I get are 404s. Did you ever got the combination of oauth2_proxy and grafana working?

dpetersen commented 7 years ago

@dereulenspiegel I only got it working at the root, not at a subpath, and we settled for that. If you're OK with running it at the root and are having problems, shoot me an email at the address in my Github profile, and I can dig up my configs for you.

emmnx commented 7 years ago

Deleting everything cached and cookies did the trick.

aceslup commented 7 years ago

this configuration,nginx is work。but,direct access will be reported,like this:http://grafana_server:3000/grafana/。return {{alert.title}}。Is it the same?

emmnx commented 7 years ago

@aceslup You're still having problems with the paths. But it could be that the problem is on your client side (browser), even when everything works OK on the server side. Make you sure that you're always using a fresh browser profile, or delete cache and cookies.

fogfish commented 7 years ago

There are no easy way to run grafana behind AWS ALB. You need to setup nginx + grafana. It would be nice if subUrl is configurable directly within grafana.

iggyzap commented 7 years ago

Why is this ticket closed ?

JulianGindi commented 7 years ago

I'm running into this issue as well. I'm testing without a load-balancer/proxy set up by running Grafana directly on port 80 as Root (for testing only). I run into the same redirect issue with /login. Not sure how to debug this further. I have taken the suggestions into account from this thread.

bbbenji commented 7 years ago

I had the same problem. Commenting out the following in my NGINX config resolved the issue.

location ~* \.(?:css(\.map)?|js(\.map)?|jpe?g|png|gif|ico|cur|heic|webp|tiff?|mp3|m4a|aac|ogg|midi?|wav|mp4|mov|webm|mpe?g|avi|ogv|flv|wmv)$ {
  expires 7d;
  access_log off;
}

location ~* \.(?:svgz?|ttf|ttc|otf|eot|woff|woff2)$ {
  add_header Access-Control-Allow-Origin "*";
  expires 7d;
  access_log off;
}