grafana behind a nginx reverse proxy - nginx

I try to run grafana and nginx as reverse proxy in a kubernetes cluster and I already found this answer but this seems not to work for me. At least I get the same {{alert.title}}-Message as Oles. That's why I would like ask again and maybe someone can give me a hint what I am doing wrong?
The configuration for the grafana deployment contains the following part:
env:
- name: GF_SERVER_DOMAIN
value: "k8s-4"
- name: GF_SERVER_ROOT_URL
value: "http://k8s-4/grafana"
and I don't modify the grafana.ini inside the container/pod.
Further I configure the nginx in the default.conf as following:
server {
listen 80;
server_name localhost k8s-4;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
location /grafana/ {
proxy_pass http://k8s-4:30080/grafana;
proxy_set_header X-Forwarded-Host k8s-4;
proxy_set_header X-Forwarded-Server k8s-4;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
But as I say above this leads to the alert.title Error. But if I set the context to the root Element and configured the tools as follows:
deployment:
env:
- name: GF_SERVER_DOMAIN
value: "k8s-4"
- name: GF_SERVER_ROOT_URL
value: "http://k8s-4"
nginx - default.conf
server {
listen 80;
server_name localhost k8s-4;
location / {
proxy_pass http://k8s-4:30080/grafana;
proxy_set_header X-Forwarded-Host k8s-4;
proxy_set_header X-Forwarded-Server k8s-4;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /grafana/ {
}
}
it works...
That's why I am quite sure this is a problem with the deployment of grafana but I don't see the error. Maybe someone here can give me a little hint?

Your first server setup is almost correct, you need to change the proxy_pass line to:
proxy_pass http://k8s-4:30080/;

Related

Ngixn rewrite and reverse proxy to docker container

I am running 3 services using docker compose on 3 ports: svc1:8081, svc2:8082, and svc3:8083 respectively. I have nginx installed on my host machine (not as a docker container), I want to reverse proxy all the requests to appropriate services so I am rewriting the url inside the nginx location block and performing a reverse proxy.
I am unable to get the results as something the files are not loading (mainly the static file to docker container)
My nginx config is as follows:
server {
listen 80;
server_name - ;
location /svc1/ {
rewrite ^/svc1(.*)$ $1 break;
proxy_pass http://localhost:8081;
}
location /svc2/ {
rewrite ^/svc2(.*)$ $1 break;
proxy_pass http://localhost:8082;
}
location /svc3/ {
rewrite ^/svc3(.*)$ $1 break;
proxy_pass http://localhost:8083;
}
}
I would be thankful if anyone can point if I am doing any wrong. Thanks in advance for your help.
Try this:
server {
listen 80;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Referer $http_referer;
# Additionally those headers if websocket/reload is used:
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
location /svc1/ {
proxy_pass http://localhost:8081/;
}
location /svc2/ {
proxy_pass http://localhost:8082/;
}
location /svc3/ {
proxy_pass http://localhost:8083/;
}
}
Don't forget to restart nginx after changing conf!

Nginx: create a location to redirect to another location

I'm trying to redirect a location to another location, but for some reason it doesn't work. My nginx service is running under a Docker container on 8080:80 port.
I have /portal that shows my web application correctly and I would like nginx redirects from / to /portal. I've tried some approaches using location / or location = /, but with no success.
The /portal can be accessed normally by
http://localhost:8080/portal
but for some reason when I access
http://localhost:8080/
the browser redirects to
https://localhost/portal
instead of redirecting to:
http://localhost:8080/portal
Locations:
upstream webportal {
server portal_container:8080;
}
location / {
return 301 /portal;
}
location /portal {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://webportal;
}
I was able to fix the problem by adding absolute_redirect off; to my location:
location / {
absolute_redirect off;
return 301 /portal;
}

How to rewrite or proxy url in nginx?

I am new in nginx. I want to rewrite
feature-1234.mydomain.com/xyz?foo=bar
to
docker-feature-1234:9000/xyz?foo=bar
with request parameters.
I use official nginx docker image. How should be my nginx.conf file?
EDIT: 'feature-1234' is a variable so:
feature-5678.mydomain.com
should serve as
docker-feature-5678:9000
By combining answers, i found the solution.
https://serverfault.com/questions/388552/nginx-sub-domain-proxy-pass
Docker Network Nginx Resolver
nginx: [emerg] "server" directive is not allowed here
nginx.conf:
events {
}
http {
server {
listen 80;
server_name ~(.*).test.go;
location / {
if ($host ~* ^([a-zA-Z0-9-]+)\.test\.go$) {
set $proxyhost docker-$1:9000;
}
resolver 127.0.0.11 ipv6=off;
proxy_pass http://$proxyhost;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
}
}
}
Thanks

NGINX basic authentication based on environment variable

I'm setting up a docker image with nginx-lua installed. The scenario is to have a basic authentication on staging, but not in production. My idea was to have an ENV variable with the name of the stage and check the value in the nginx.conf file.
The content of the docker-compose.yml file (for staging, and for production the STAGE env will be prod of course):
docs-router:
build: ./nginx
environment:
- API_BASE_URI=staging.example.com
- DOCS_STATIC_URI=docs-staging.example.com
- STAGE=staging
ports:
- "8089:8089"
- "8090:8090"
The content of the nginx.conf file:
...
env API_BASE_URI;
env DOCS_STATIC_URI;
env STAGE;
...
http {
server {
listen 8089 default_server;
charset utf-8;
resolver 8.8.8.8;
access_log off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
location ~ ^(/.*\.(?:apib|svg))?$ {
set_by_lua_block $api_base_uri { return os.getenv("API_BASE_URI") }
set_by_lua_block $stage { return os.getenv("STAGE") }
set $unprotected "prod";
if ($stage = $unprotected) {
auth_basic "Restricted Content";
auth_basic_user_file /etc/nginx/.htpasswd;
}
proxy_pass https://$api_base_uri$1;
proxy_set_header Host $api_base_uri;
}
...
}
}
But it's not working. Any idea, how can I achieve this?
I just find a solution with some help from Serverfault. It's not the best one because the URLs are in the nginx.conf file, but it solves my problem:
I just removed the variable form the docker-compose.yml file:
docs-router:
build: ./nginx
environment:
- API_BASE_URI=staging.example.com
- DOCS_STATIC_URI=docs-staging.example.com
ports:
- "8089:8089"
- "8090:8090"
And then I mapped the URLs in the nginx.conf file:
...
env API_BASE_URI;
env DOCS_STATIC_URI;
...
http {
##
# URL protection
##
map $http_host $auth_type {
default "off";
stage1.example.com "Restricted";
stage2.example.com "Restricted";
}
server {
listen 8089 default_server;
charset utf-8;
resolver 8.8.8.8;
access_log off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
location ~ ^(/.*\.(?:apib|svg))?$ {
set_by_lua_block $api_base_uri { return os.getenv("API_BASE_URI") }
auth_basic $auth_type;
auth_basic_user_file /etc/nginx/.htpasswd;
proxy_pass https://$api_base_uri$1;
proxy_set_header Host $api_base_uri;
}
...
}
}
If there is a better / nicer solution for this, please let me know.

Root directory shows 404 in nginx

I am new to nginx server. I tried to set a new url "/images/" for serving images. I edited the bi.site file in site-enabled folder.
server {
listen *:80;
access_log /var/log/myproject/access_log;
location / {
proxy_pass http://127.0.0.1:5000/;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /images/ {
root /www/myproject/files_storage;
}
}
And in /www/myproject/files_storage location i put a temp.txt file.
When i put http://www.main_url/images/temp.txt it shows 404 not found. What am i doing wrong ? Did i miss something important ?
this:
location /images/ {
root /www/myproject/files_storage;
}
results in /www/myproject/files_storage/images path, it would be obvious if you setup error_log. So use "alias" directive instead of "root"
http://nginx.org/en/docs/http/ngx_http_core_module.html#alias

Resources