How to config nginx to run flask and node - docker

Hi have a docker that builds my frontend(gatsby) and backend(flask) into a single docker file.
I can run my backend if i use my current config that only deploys the backend.
What i need is to deploy both backend and frontend.
I use supervisord to launch uwsgi and nginx.
My dockerfile tree:
---app
---backend
---dev_maintenance
__init__.py
---frontend
---src
---node_modules
---public
nginx.conf
pid /run/nginx.pid;
error_log /var/log/nginx/error.log;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
sendfile on;
tcp_nopush on;
client_body_temp_path /spool/nginx/client_temp 1 2;
fastcgi_temp_path /spool/nginx/fastcgi_temp 1 2;
proxy_temp_path /spool/nginx/proxy_temp 1 2;
scgi_temp_path /spool/nginx/scgi_temp 1 2;
uwsgi_temp_path /spool/nginx/uwsgi_temp 1 2;
server {
listen 8080;
server_name localhost;
access_log /var/log/nginx/access.log;
location / {
try_files $uri #dev_maintenance;
}
location #dev_maintenance {
include uwsgi_params;
uwsgi_pass unix:///run/uwsgi.sock;
}
location /static {
alias /opt/repo/src/static;
expires 1d;
}
}
}
I was trying something like this:
pid /run/nginx.pid;
error_log /var/log/nginx/error.log;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
sendfile on;
tcp_nopush on;
client_body_temp_path /spool/nginx/client_temp 1 2;
fastcgi_temp_path /spool/nginx/fastcgi_temp 1 2;
proxy_temp_path /spool/nginx/proxy_temp 1 2;
scgi_temp_path /spool/nginx/scgi_temp 1 2;
uwsgi_temp_path /spool/nginx/uwsgi_temp 1 2;
server {
listen 8080;
server_name localhost;
access_log /var/log/nginx/access.log;
location / {
proxy_pass http://client:3000;
proxy_redirect default;
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;
proxy_set_header X-Forwarded-Proto $scheme;
}
location /api {
proxy_pass http://api:5000;
proxy_redirect default;
proxy_set_header Host $host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
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;
proxy_set_header X-Forwarded-Proto $scheme;
}
location #dev_maintenance {
include uwsgi_params;
uwsgi_pass unix:///run/uwsgi.sock;
}
location /static {
alias /opt/repo/src/static;
expires 1d;
}
}
}
supervisord.conf
[unix_http_server]
file=/run/supervisor.sock
chmod=0770
[supervisord]
nodaemon=true
pidfile=/run/pid/supervisord.pid
logfile=/var/log/supervisor/supervisord.log
childlogdir=/var/log/supervisor
logfile_maxbytes=50MB
logfile_backups=1
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
serverurl=unix:///run/supervisor.sock
[program:nginx]
command=/usr/sbin/nginx -g "daemon off;" -c /etc/nginx/nginx.conf
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
[program:uwsgi]
command=/usr/local/bin/uwsgi --ini /etc/uwsgi/apps-available/uwsgi.ini
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
uswgi.ini
[uwsgi]
master = true
module= dev_maintenance:app
callable=app
buffer-size=65535
lazy=true
socket = /run/uwsgi.sock
Does not work of course , I'am new to this and it's confusing to learn it. any help?

So i fixed the problem, i needed to copy my frontend Gatsby build that is generated in the docker when i run npm run build to the /opt/repo/src/static folder and change the location /api to:
location /api {
try_files $uri #dev_maintenance;
}

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.

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.

Getting NGINX to point to docker mattermost-preview

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

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;
}

Nginx shows "Welcome to nginx" for http request in Rails app

I am using following nginx configurations:
user www-data;
worker_processes 1;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
# multi_accept on;
}
http {
include /etc/nginx/mime.types;
access_log /var/log/nginx/access.log;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
tcp_nodelay on;
gzip on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
upstream myapp.co {
server 127.0.0.1:8080;
}
server{
listen 80;
server_name myapp.co;
rewrite ^ https://myapp.co$request_uri? permanent;
}
server {
listen 443 ssl;
server_name myapp.co;
root /home/deployer/myapp/public;
ssl on;
ssl_certificate /etc/nginx/certs/myapp.co.crt;
ssl_certificate_key /etc/nginx/certs/myapp.co.private.key;
#server_name myapp.co _;
#root /home/deployer/myapp/public;
location / {
proxy_set_header X_FORWARDED_PROTO $scheme;
proxy_set_header Host $http_host;
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_set_header X-Real-IP $remote_addr;
proxy_set_header CLIENT_IP $remote_addr;
proxy_redirect http:// https://;
if (!-f $request_filename) {
proxy_pass http://myapp.co;
break;
}
if (-f $document_root/system/maintenance.html) {
return 503;
}
}
}
}
The issue: when I load http://www.myapp.co, I get the error message
Welcome to nginx
But if I set to the browser
https://www.myapp.co
https://myapp.co
http://myapp.co
Everything is working well.
How can I fix up the proper displaying of the Rails app also for the request http://www.myapp.co?
I am quite amateur with setting up of nginx, so I'll be grateful for every advice.
Thank you
I think, you should set your server_name (in both server sections) like this:
server_name myapp.co www.myapp.co;

Resources