Getting NGINX to point to docker mattermost-preview - docker

I'm trying to follow this: https://forum.mattermost.org/t/recipe-embedding-mattermost-in-web-applications-using-an-iframe-unsupported-recipe/10233
I'm running a dockerized version of mattermost. I am running a dockerized container of nginx. I configured my nginx according to: https://docs.mattermost.com/install/install-ubuntu-1804.html#configuring-nginx-as-a-proxy-for-mattermost-server
I have the mattermost-preview container running on localhost:8065.
I followed the steps and restart the nginx instance and visit localhost:80 but still see the default nginx welcome page.
Here's my default.conf:
upstream backend {
server localhost:8065;
keepalive 32;
}
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=mattermost_cache:10m max_size=3g inactive=120m use_temp_path=off;
server {
listen 80 default_server;
server_name localhost;
return 301 https://$server_name$request_uri;
}
server {
listen 443 http2;
server_name localhost_two;
http2_push_preload on; # Enable HTTP/2 Server Push
ssl off;
location ~ /api/v[0-9]+/(users/)?websocket$ {
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
client_max_body_size 50M;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_hide_header Content-Security-Policy;
proxy_hide_header X-Frame-Options;
proxy_buffers 256 16k;
proxy_buffer_size 16k;
client_body_timeout 60;
send_timeout 300;
lingering_timeout 5;
proxy_connect_timeout 90;
proxy_send_timeout 300;
proxy_read_timeout 90s;
proxy_http_version 1.1;
proxy_pass http://backend;
}
location / {
client_max_body_size 50M;
proxy_set_header Connection "";
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_hide_header Content-Security-Policy;
proxy_hide_header X-Frame-Options;
proxy_buffers 256 16k;
proxy_buffer_size 16k;
proxy_read_timeout 600s;
proxy_cache mattermost_cache;
proxy_cache_revalidate on;
proxy_cache_min_uses 2;
proxy_cache_use_stale timeout;
proxy_cache_lock on;
proxy_http_version 1.1;
proxy_pass http://backend;
}
}
# This block is useful for debugging TLS v1.3. Please feel free to remove this
# and use the `$ssl_early_data` variable exposed by NGINX directly should you
# wish to do so.
map $ssl_early_data $tls1_3_early_data {
"~." $ssl_early_data;
default "";
}
My nginx.conf is the default one

Related

NGINX: redirect requests from installed nginx to nginx in docker

I have two nginx reverse proxy on the same machine:
Installed into Ubuntu (takes 80 and 443 ports)
nginx in docker container (takes 445 port and mapped to 443)
The installed nginx should redirect particular domain requests to nginx in docker.
Installed nginx config:
upstream target {
server 127.0.0.1:8891 fail_timeout=0;
}
upstream target_green {
server 127.0.0.1:445 fail_timeout=0;
}
server {
set $rootfolder "/var/www/root/";
set $link "target.domain.example";
listen 443;
server_name target.domain.example;
charset utf-8;
client_max_body_size 1G;
add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
ssl on;
ssl_certificate /etc/nginx/ssl/bundle.crt;
ssl_certificate_key /etc/nginx/ssl/private_key.key;
ssl_session_cache builtin:1000 shared:SSL:10m;
ssl_session_timeout 6m;
ssl_prefer_server_ciphers on;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH;
ssl_dhparam /etc/nginx/dhparam.pem;
access_log /var/www/app/logs/access.log;
error_log /var/www/app/logs/error.log;
gzip on;
location / {
proxy_connect_timeout 600;
proxy_send_timeout 600;
proxy_read_timeout 600;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://target;
}
location /ws/ {
proxy_pass http://target;
proxy_http_version 1.1;
proxy_read_timeout 86400;
proxy_redirect off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
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_set_header X-Forwarded-Host $server_name;
}
location /static {
gzip_vary on;
gzip on;
gzip_min_length 1100;
gzip_buffers 16 8k;
gzip_comp_level 3;
gzip_types text/plain application/xml application/x-javascript text/css;
root /var/www/root/static/;
}
location /media {
gzip_vary on;
gzip on;
gzip_min_length 1100;
gzip_buffers 16 8k;
gzip_comp_level 3;
gzip_types text/plain application/xml application/x-javascript text/css;
root /var/www/root/media/;
}
}
server {
listen 443 ssl;
server_name "target-green.domain.example";
charset utf-8;
client_max_body_size 1G;
add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
ssl on;
ssl_certificate /etc/nginx/ssl/bundle.crt;
ssl_certificate_key /etc/nginx/ssl/private_key.key;
ssl_session_cache builtin:1000 shared:SSL:10m;
ssl_session_timeout 6m;
ssl_prefer_server_ciphers on;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH;
ssl_dhparam /etc/nginx/dhparam.pem;
access_log /var/www/app/logs/access.log;
error_log /var/www/app/logs/error.log;
gzip on;
location / {
proxy_connect_timeout 600;
proxy_send_timeout 600;
proxy_read_timeout 600;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass https://target_green;
}
}
nginx-in-docker config:
server {
set $rootfolder "/var/www/app/";
set $app "http://app:8891";
set $ws "http:/app-ws:10000";
listen 443 ssl;
# Docker DNS
resolver 127.0.0.11;
server_name "target-green.domain.example";
charset utf-8;
client_max_body_size 1G;
add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
ssl_certificate /etc/nginx/ssl/ssl_certificate;
ssl_certificate_key /etc/nginx/ssl/ssl_certificate_key;
ssl_session_cache builtin:1000 shared:SSL:10m;
ssl_session_timeout 6m;
ssl_prefer_server_ciphers on;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH;
ssl_dhparam /etc/nginx/ssl_dhparam;
access_log /var/www/app/logs/access.log;
error_log /var/www/app/logs/error.log;
location / {
proxy_connect_timeout 159s;
proxy_send_timeout 600;
proxy_read_timeout 600;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass $app;
}
location /ws/ {
proxy_pass $ws;
proxy_http_version 1.1;
proxy_read_timeout 86400;
proxy_redirect off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
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_set_header X-Forwarded-Host $server_name;
}
location /static {
gzip_min_length 1100;
gzip_comp_level 3;
root $rootfolder/frontend/;
}
location /media {
gzip_min_length 1100;
gzip_comp_level 3;
root $rootfolder/;
}
}
Unfortunately, in the browser, I see target-green.domain.example redirected you too many times.

nginx multiple locations using proxy_path to different urls leads to 502 response

I made an nginx.conf file, my issue is that the all the proxy_pass redirects end up being 502 Bad Gateway messages.
Here is the nginx.conf:
http {
server {
listen 80;
server_tokens off;
location / {
client_max_body_size 0;
gzip off;
proxy_read_timeout 300;
proxy_connect_timeout 300;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://172.21.144.10;
}
}
server {
listen 443;
server_tokens off;
location / {
client_max_body_size 0;
gzip off;
proxy_read_timeout 300;
proxy_connect_timeout 300;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://172.21.144.10:443;
}
}
}
The url http://172.21.144.10 points to a gitlab-ce install in another docker container, it is accessible via the browser.
I've tried both proxy_pass with and without a trailing slash, so that's not the issue.

Can't get client's real IP when using Nginx in Docker

I have configured Nginx reverse proxy with Docker in my project.
part of my docker-compose file:
version: "3"
# optional ports are specified throughout for more advanced use cases.
services:
proxy-service:
container_name: budi-nginx-dev
restart: on-failure
image: nginx:latest
volumes:
- ./.generated-nginx.dev.conf:/etc/nginx/nginx.conf
ports:
- "${MAIN_PORT}:10000"
depends_on:
- minio-service
- couchdb-service
extra_hosts:
- "host.docker.internal:host-gateway"
I have add "X-Forwarded-For proxy_set_header" in "location /api/".
part of my nginx file:
user nginx;
error_log /var/log/nginx/error.log debug;
pid /var/run/nginx.pid;
worker_processes auto;
worker_rlimit_nofile 33282;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
proxy_set_header Host $host;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
map $http_upgrade $connection_upgrade {
default "upgrade";
}
server {
listen 10000 default_server;
server_name _;
client_max_body_size 1000m;
ignore_invalid_headers off;
proxy_buffering off;
location ~ ^/api/(system|admin|global)/ {
proxy_pass http://host.docker.internal:4002;
}
location /api/ {
proxy_read_timeout 120s;
proxy_connect_timeout 120s;
proxy_send_timeout 120s;
proxy_http_version 1.1;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://host.docker.internal:4001;
}
location /app_ {
proxy_pass http://host.docker.internal:4001;
}
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_connect_timeout 300;
proxy_http_version 1.1;
proxy_set_header Connection "";
chunked_transfer_encoding off;
proxy_pass http://minio-service:9000;
}
client_header_timeout 60;
client_body_timeout 60;
keepalive_timeout 60;
gzip off;
gzip_comp_level 4;
}
}
When I request api that start with "api/", I get "127.20.0.1" of "X-Forwarded-For" property in the request header. Obviously it is not the real client ip.
I would like to known how to get the real client ip in this case.

Nginx: Add context if one doesn't exist - in redirection

Requirement:
Redirect http://ruby.server.com to https://ruby.server.com/app1/ and don't append anything if the incoming URL has context (ex: http://ruby.server.com/app2/) in it
Setup
We have the following setup
Amazon Load Balancer
| http - |
| --> 80(ruby.server.com)
| https - |
nginx is running on ruby.server.com at port 80. There is no 443 in nginx.
server {
listen 80;
server_name ruby.server.com;
root /home/ubuntu/ruby/server/public/;
location / {
if ($http_x_forwarded_proto != 'https') {
rewrite ^ https://$host$request_uri? permanent;
}
client_max_body_size 100M;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:4009/;
root /home/ubuntu/ruby/server/public/;
}
if ($http_x_forwarded_proto != 'https') line is used for redirecting http://ruby.server.com to https://ruby.server.com
More context
We have 2 rails apps running inside the rails server, /public/app1, /public/app2. By default we want to redirect ruby.server.com to go to app1.
We can solve the above problem within the rails itself that involves an additional redirection. We are trying to see if we can get that context append within the nginx layer.
Solution based #timothy's note.
server {
listen 80;
server_name ruby.server.com;
root /home/ubuntu/staging/server/public/;
location / {
if ($http_x_forwarded_proto != 'https') {
rewrite ^ https://$host/app1/? permanent;
}
client_max_body_size 100M;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:4009/;
root /home/ubuntu/staging/server/public/;
#auth_basic "Restricted";
#auth_basic_user_file /etc/nginx/.htpasswd;
}
location ^~ /[^\/]+/ {
if ($http_x_forwarded_proto != 'https') {
rewrite ^ https://$host$request_uri? permanent;
}
client_max_body_size 100M;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:4009/;
}
}
server {
listen 80;
server_name ruby.server.com;
root /home/ubuntu/ruby/server/public/;
location / {
# Default location for:
# http://ruby.server.com
# https://ruby.server.com
if ($http_x_forwarded_proto != 'https') {
rewrite ^ https://$host$request_uri? permanent;
}
client_max_body_size 100M;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:4009/;
}
location ^~ /[^\/]+/ {
# Location for:
# http://ruby.server.com/anything/
# https://ruby.server.com/anything/
# Do whatever you need here. :)
client_max_body_size 100M;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:4009/;
}
}

Rails not serving my robots.txt and sitemap.xml.gz on Production

While these files are being served in development fine, and also in production on my local machine, these are not served on live production. I get a 404 not found error. Everything else is working fine. These files are present in the public directory of the app (-approot-/public)
I am using nginx and unicorn over the live server. My nginx/sites-available/default file:
upstream example.com {
server unix:/tmp/example.socket fail_timeout=0;
}
server {
listen 80 default;
server_name example.com www.example.com;
root /home/myuser/apps/example/current/public;
access_log /var/log/nginx/access.log;
rewrite_log on;
location / {
proxy_pass http://example.com;
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;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
location ~ ^/(images|javascripts|stylesheets|assets|system)/ {
root /home/myuser/apps/example/current/public;
expires max;
break;
}
}
You write one more location rule like
server {
listen 80 default;
server_name example.com www.example.com;
root /home/myuser/apps/example/current/public;
access_log /var/log/nginx/access.log;
rewrite_log on;
location / {
proxy_pass http://example.com;
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;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
location ~ ^/(images|javascripts|stylesheets|assets|system)/ {
root /home/myuser/apps/example/current/public;
expires max;
break;
}
location ~ ^/(robots.txt|sitemap.xml.gz)/ {
root /home/myuser/apps/example/current/public;
}
}
Append this to etc/nginx/sites-available/default:
location ~ ^/(robots.txt|sitemap.xml.gz) {
root /home/<user>/apps/<appname>/current/public;
}
Append this to etc/nginx/sites-available/{your-app-config-file}:
location ~ .*(robots.txt|sitemap.xml.gz) {
# if you already set root above, then the following line is not needed.
root /path_to_app/public;
}

Resources