I am trying to develop an Nginx reverse proxy using docker-compose. I may not be able to state the problem correctly but I am posting the code and error. When I try to proxy to one service, the proxy works but nothing is displayed on the browser.
Here's my nginx.conf :
worker_processes 1;
events { worker_connections 1024; }
http {
sendfile on;
upstream docker-nginx {
server react-app:80;
}
server {
listen 62106;
server_name http://10.1.40.24;
location /try {
rewrite ^/try(.*)$ $1 break;
proxy_pass http://docker-nginx/;
}
}
}
Here's my docker-compose file:
version: '3.5'
services:
react-app:
build:
context: ./my-app
cache_from:
- nginx:alpine
ports:
- "62101:80"
image: app-react-uat:latest
networks:
my-network:
aliases:
- perfreview
server-app:
build:
context: ./Flask
cache_from:
- python:3-slim
environment:
- ENV = production
- PORT = 62102
ports:
- "62102:62102"
image: flask-py-app-uat:latest
nginx-reverse:
build:
context: ./nginx
cache_from:
- nginx:alpine
ports:
- "62106:62106"
depends_on:
- react-app
networks:
- my-network
networks:
my-network:
Here's the error message:
react-app_1 | 172.28.0.3 - - [03/Sep/2019:04:07:03 +0000] "GET / HTTP/1.0" 200 333 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76. 0.3809.132 Safari/537.36" "-"
nginx-reverse_1 | 10.1.20.45 - - [03/Sep/2019:04:07:03 +0000] "GET /try/ HTTP/1.1" 200 333 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome /76.0.3809.132 Safari/537.36"
nginx-reverse_1 | 2019/09/03 04:07:03 [error] 6#6: *12 open() "/etc/nginx/html/static/css/main.c0c280ad.css" failed (2: No such file or directory), client: 10.1.20.45, server: http:// 10.1.40.24, request: "GET /static/css/main.c0c280ad.css HTTP/1.1", host: "10.1.40.24:62106", referrer: "http://10.1.40.24:62106/try/"
nginx-reverse_1 | 10.1.20.45 - - [03/Sep/2019:04:07:03 +0000] "GET /static/css/main.c0c280ad.css HTTP/1.1" 404 555 "http://10.1.40.24:62106/try/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36"
nginx-reverse_1 | 2019/09/03 04:07:03 [error] 6#6: *13 open() "/etc/nginx/html/static/js/main.5a0e26e5.js" failed (2: No such file or directory), client: 10.1.20.45, server: http://10 .1.40.24, request: "GET /static/js/main.5a0e26e5.js HTTP/1.1", host: "10.1.40.24:62106", referrer: "http://10.1.40.24:62106/try/"
nginx-reverse_1 | 10.1.20.45 - - [03/Sep/2019:04:07:03 +0000] "GET /static/js/main.5a0e26e5.js HTTP/1.1" 404 555 "http://10.1.40.24:62106/try/" "Mozilla/5.0 (Windows NT 10.0; Win64; x 64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36"
nginx-reverse_1 | 10.1.20.45 - - [03/Sep/2019:04:07:30 +0000] "GET /static/css/main.c0c280ad.css HTTP/1.1" 404 555 "http://10.1.40.24:62106/try/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36"
nginx-reverse_1 | 2019/09/03 04:07:30 [error] 6#6: *13 open() "/etc/nginx/html/static/css/main.c0c280ad.css" failed (2: No such file or directory), client: 10.1.20.45, server: http:// 10.1.40.24, request: "GET /static/css/main.c0c280ad.css HTTP/1.1", host: "10.1.40.24:62106", referrer: "http://10.1.40.24:62106/try/"
Any help would be grateful. Thank you!
So, I found out the reason behind the error and a way to solve it. Basically, the files of react-app container were not being shared with the reverse-proxy container. Consequently, I used volumes to come around this and set alias to the location of the mounted part in the location part of the config file. Here's the new nginx.conf:
server {
listen 62106;
server_name http://10.1.40.24;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location /perfreview/ {
alias /usr/share/nginx/html/prs;
proxy_pass http://react-app:80/;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
Hope this helps someone someday!
Related
Diagram
Setup
I got 3 docker containers running, 1 for Nginx, 2 for Web Apps
App or Nginx can be reach at localhost:8443
1st Web App is running in port 3000, can be reach via proxy_pass on route "/" or "/*"
2nd Web App is running in port 5000, can be reach via proxy_pass on route "/admin" only
Problem
When I go to "/admin" route it will display the WebApp with no problem but it will hault, it will also fetch the CDN resources with no problem, also it cannot GET some applications assets, like CSS, JS.
Its trying to perform GET on localhost:8443/_nuxt/ but I understand that there's nothing in this link.
The problem is on NGINX configuration, I am no expert on NGINX, how do I configure NGINX for this?
My Whole NGINX Configuration
upstream public {
server ${UPSTREAM_PUBLIC};
}
upstream admin {
server ${UPSTREAM_ADMIN};
}
server {
listen 80;
server_name www.${NGINX_HOST};
return 301 https://${NGINX_HOST}$uri;
}
server {
listen ${NGINX_HTTPS_PORT} ssl http2 default_server;
charset utf-8;
server_name www.${NGINX_HOST} ${NGINX_HOST};
# SSL HERE: change into the directory where the certificates located
ssl_certificate /etc/nginx/certs/fullchain.pem;
ssl_certificate_key /etc/nginx/certs/private.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers 'ECDHE-RSA-AES256-GCM-SHA512:DHE-RSA-AES256-GCM-SHA512:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384';
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
location /${ADMIN_ROUTE}/ {
proxy_pass http://admin;
proxy_http_version 1.1;
proxy_set_header Host $http_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-Scheme $scheme;
proxy_set_header X-Script-Name /${ADMIN_ROUTE};
client_max_body_size 0;
}
location / {
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_http_version 1.1;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_pass http://public/;
}
}
My docker-compose.yml file
version: "3"
services:
frontend-admin:
build:
context: "../frontend-admin"
target: "dev"
container_name: "Admin"
restart: "unless-stopped"
working_dir: "/usr/src/nuxt"
environment:
- "APP_HOST=frontend-admin"
- "APP_PORT=5000"
networks:
- "admin-network"
ports:
- "5000:5000"
volumes:
- "../frontend-admin/assets:/usr/src/admin/assets"
- "../frontend-admin/components:/usr/src/admin/components"
- "../frontend-admin/layouts:/usr/src/admin/layouts"
- "../frontend-admin/middleware:/usr/src/admin/middleware"
- "../frontend-admin/pages:/usr/src/admin/pages"
- "../frontend-admin/plugins:/usr/src/admin/plugins"
- "../frontend-admin/server:/usr/src/admin/server"
- "../frontend-admin/store:/usr/src/admin/store"
- "../frontend-admin/static:/usr/src/admin/static"
- "../frontend-admin/m.js:/usr/src/admin/m.js"
- "../frontend-admin/nuxt.config.js:/usr/src/admin/nuxt.config.js"
frontend-public:
build:
context: "../frontend-public"
target: "production"
container_name: "Public"
restart: "unless-stopped"
working_dir: "/usr/src/nuxt"
environment:
- "APP_HOST=frontend-public"
- "APP_PORT=3000"
- "APP_NAME=sample.com"
ports:
- "3000:3000"
networks:
- "public-network"
volumes:
- "../frontend-public/assets:/usr/src/public/assets"
- "../frontend-public/components:/usr/src/public/components"
- "../frontend-public/layouts:/usr/src/public/layouts"
- "../frontend-public/middleware:/usr/src/public/middleware"
- "../frontend-public/pages:/usr/src/public/pages"
- "../frontend-public/plugins:/usr/src/public/plugins"
- "../frontend-public/server:/usr/src/public/server"
- "../frontend-public/store:/usr/src/public/store"
- "../frontend-public/static:/usr/src/public/static"
- "../frontend-public/m.js:/usr/src/public/m.js"
- "../frontend-public/nuxt.config.js:/usr/src/public/nuxt.config.js"
nginx:
image: "nginx"
depends_on:
- "frontend-admin"
- "frontend-public"
container_name: "nginx"
volumes:
- "./nginx:/etc/nginx/templates"
- "./nginx/certs:/etc/nginx/certs"
ports:
- "8443:8443"
- "80:80"
environment:
- "NGINX_HTTPS_PORT=8443"
- "NGINX_HOST=localhost"
- "NGINX_PORT=80"
- "UPSTREAM_ADMIN=frontend-admin:5000"
- "ADMIN_ROUTE=admin"
- "UPSTREAM_PUBLIC=frontend-public:3000"
- "PGADMIN_URL=mydb"
networks:
- "admin-network"
- "public-network"
networks:
admin-network:
driver: "bridge"
public-network:
driver: "bridge"
Logs after running dokcer-compose up (no problem)
Admin |
Admin | > frontend-admin#1.0.0 dev
Admin | > nuxt
Admin |
nginx | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
nginx | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
Public |
Public | > frontend-public#1.0.0 start
Public | > nuxt start
Public |
nginx | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
nginx | 10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
nginx | 10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
nginx | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
nginx | 20-envsubst-on-templates.sh: Running envsubst on /etc/nginx/templates/default.conf.template to /etc/nginx/conf.d/default.conf
nginx | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
nginx | /docker-entrypoint.sh: Configuration complete; ready for start up
nginx | 2021/11/02 02:47:52 [notice] 1#1: using the "epoll" event method
nginx | 2021/11/02 02:47:52 [notice] 1#1: nginx/1.21.3
nginx | 2021/11/02 02:47:52 [notice] 1#1: built by gcc 8.3.0 (Debian 8.3.0-6)
nginx | 2021/11/02 02:47:52 [notice] 1#1: OS: Linux 5.10.25-linuxkit
nginx | 2021/11/02 02:47:52 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
nginx | 2021/11/02 02:47:52 [notice] 1#1: start worker processes
nginx | 2021/11/02 02:47:52 [notice] 1#1: start worker process 36
nginx | 2021/11/02 02:47:52 [notice] 1#1: start worker process 37
nginx | 2021/11/02 02:47:52 [notice] 1#1: start worker process 38
nginx | 2021/11/02 02:47:52 [notice] 1#1: start worker process 39
Public | APP_NAME: sample.com
Public | ℹ Listening on: http://frontend-public:3000/
Admin | ℹ Listening on: http://frontend-admin:5000/
Admin | ℹ Preparing project for development
Admin | ℹ Initial build may take a while
Admin | ℹ Discovered Components: .nuxt/components/readme.md
Admin | ✔ Builder initialized
Admin | ✔ Nuxt files generated
Admin | ℹ Compiling Client
Admin | ℹ Compiling Server
Admin | ✔ Server: Compiled successfully in 21.54s
Admin | ✔ Client: Compiled successfully in 22.73s
Admin | ℹ Waiting for file changes
Admin | ℹ Memory usage: 262 MB (RSS: 433 MB)
Admin | ℹ Listening on: http://frontend-admin:5000/
Admin | Nuxt Server Init
Logs after accessing route: localhost:8443/admin
nginx | 172.20.0.1 - - [02/Nov/2021:02:48:23 +0000] "GET /admin/ HTTP/2.0" 200 934546 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36" "-"
nginx | 2021/11/02 02:48:23 [alert] 36#36: *3 "alias" cannot be used in location "^.+.(jpg|jpeg|gif|svg|css|png|js|ico|txt|srt|swf|ttf|woff|woff2)$" where URI was rewritten, client: 172.20.0.1, server: www.localhost, request: "GET /_nuxt/runtime.js HTTP/2.0", host: "localhost:8443", referrer: "https://localhost:8443/admin/"
nginx | 2021/11/02 02:48:23 [alert] 36#36: *3 "alias" cannot be used in location "^.+.(jpg|jpeg|gif|svg|css|png|js|ico|txt|srt|swf|ttf|woff|woff2)$" where URI was rewritten, client: 172.20.0.1, server: www.localhost, request: "GET /_nuxt/commons/app.js HTTP/2.0", host: "localhost:8443", referrer: "https://localhost:8443/admin/"
nginx | 2021/11/02 02:48:23 [alert] 36#36: *3 "alias" cannot be used in location "^.+.(jpg|jpeg|gif|svg|css|png|js|ico|txt|srt|swf|ttf|woff|woff2)$" where URI was rewritten, client: 172.20.0.1, server: www.localhost, request: "GET /_nuxt/vendors/app.js HTTP/2.0", host: "localhost:8443", referrer: "https://localhost:8443/admin/"
nginx | 2021/11/02 02:48:23 [alert] 36#36: *3 "alias" cannot be used in location "^.+.(jpg|jpeg|gif|svg|css|png|js|ico|txt|srt|swf|ttf|woff|woff2)$" where URI was rewritten, client: 172.20.0.1, server: www.localhost, request: "GET /_nuxt/app.js HTTP/2.0", host: "localhost:8443", referrer: "https://localhost:8443/admin/"
nginx | 2021/11/02 02:48:23 [alert] 36#36: *3 "alias" cannot be used in location "^.+.(jpg|jpeg|gif|svg|css|png|js|ico|txt|srt|swf|ttf|woff|woff2)$" where URI was rewritten, client: 172.20.0.1, server: www.localhost, request: "GET /_nuxt/pages/_.js HTTP/2.0", host: "localhost:8443", referrer: "https://localhost:8443/admin/"
nginx | 172.20.0.1 - - [02/Nov/2021:02:48:23 +0000] "GET /_nuxt/runtime.js HTTP/2.0" 500 579 "https://localhost:8443/admin/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36" "-"
nginx | 172.20.0.1 - - [02/Nov/2021:02:48:23 +0000] "GET /_nuxt/commons/app.js HTTP/2.0" 500 579 "https://localhost:8443/admin/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36" "-"
nginx | 172.20.0.1 - - [02/Nov/2021:02:48:23 +0000] "GET /_nuxt/vendors/app.js HTTP/2.0" 500 579 "https://localhost:8443/admin/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36" "-"
nginx | 172.20.0.1 - - [02/Nov/2021:02:48:23 +0000] "GET /_nuxt/app.js HTTP/2.0" 500 579 "https://localhost:8443/admin/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36" "-"
nginx | 172.20.0.1 - - [02/Nov/2021:02:48:23 +0000] "GET /_nuxt/pages/_.js HTTP/2.0" 500 579 "https://localhost:8443/admin/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36" "-"
nginx | 2021/11/02 02:48:23 [alert] 36#36: *3 "alias" cannot be used in location "^.+.(jpg|jpeg|gif|svg|css|png|js|ico|txt|srt|swf|ttf|woff|woff2)$" where URI was rewritten, client: 172.20.0.1, server: www.localhost, request: "GET /_nuxt/runtime.js HTTP/2.0", host: "localhost:8443", referrer: "https://localhost:8443/admin/"
nginx | 172.20.0.1 - - [02/Nov/2021:02:48:23 +0000] "GET /_nuxt/runtime.js HTTP/2.0" 500 579 "https://localhost:8443/admin/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36" "-"
nginx | 2021/11/02 02:48:23 [alert] 36#36: *3 "alias" cannot be used in location "^.+.(jpg|jpeg|gif|svg|css|png|js|ico|txt|srt|swf|ttf|woff|woff2)$" where URI was rewritten, client: 172.20.0.1, server: www.localhost, request: "GET /_nuxt/pages/_.js HTTP/2.0", host: "localhost:8443", referrer: "https://localhost:8443/admin/"
nginx | 172.20.0.1 - - [02/Nov/2021:02:48:23 +0000] "GET /_nuxt/pages/_.js HTTP/2.0" 500 579 "https://localhost:8443/admin/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36" "-"
nginx | 2021/11/02 02:48:23 [alert] 36#36: *3 "alias" cannot be used in location "^.+.(jpg|jpeg|gif|svg|css|png|js|ico|txt|srt|swf|ttf|woff|woff2)$" where URI was rewritten, client: 172.20.0.1, server: www.localhost, request: "GET /_nuxt/commons/app.js HTTP/2.0", host: "localhost:8443", referrer: "https://localhost:8443/admin/"
nginx | 172.20.0.1 - - [02/Nov/2021:02:48:23 +0000] "GET /_nuxt/commons/app.js HTTP/2.0" 500 579 "https://localhost:8443/admin/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36" "-"
nginx | 172.20.0.1 - - [02/Nov/2021:02:48:23 +0000] "GET /_nuxt/vendors/app.js HTTP/2.0" 500 579 "https://localhost:8443/admin/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36" "-"
nginx | 172.20.0.1 - - [02/Nov/2021:02:48:23 +0000] "GET /_nuxt/app.js HTTP/2.0" 500 579 "https://localhost:8443/admin/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36" "-"
nginx | 172.20.0.1 - - [02/Nov/2021:02:48:23 +0000] "GET /_nuxt/runtime.js HTTP/2.0" 500 579 "https://localhost:8443/admin/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36" "-"
nginx | 2021/11/02 02:48:23 [alert] 36#36: *3 "alias" cannot be used in location "^.+.(jpg|jpeg|gif|svg|css|png|js|ico|txt|srt|swf|ttf|woff|woff2)$" where URI was rewritten, client: 172.20.0.1, server: www.localhost, request: "GET /_nuxt/vendors/app.js HTTP/2.0", host: "localhost:8443", referrer: "https://localhost:8443/admin/"
nginx | 2021/11/02 02:48:23 [alert] 36#36: *3 "alias" cannot be used in location "^.+.(jpg|jpeg|gif|svg|css|png|js|ico|txt|srt|swf|ttf|woff|woff2)$" where URI was rewritten, client: 172.20.0.1, server: www.localhost, request: "GET /_nuxt/app.js HTTP/2.0", host: "localhost:8443", referrer: "https://localhost:8443/admin/"
nginx | 2021/11/02 02:48:23 [alert] 36#36: *3 "alias" cannot be used in location "^.+.(jpg|jpeg|gif|svg|css|png|js|ico|txt|srt|swf|ttf|woff|woff2)$" where URI was rewritten, client: 172.20.0.1, server: www.localhost, request: "GET /_nuxt/runtime.js HTTP/2.0", host: "localhost:8443", referrer: "https://localhost:8443/admin/"
nginx | 172.20.0.1 - - [02/Nov/2021:02:48:23 +0000] "GET /admin/vuetify.css.map HTTP/2.0" 404 48 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36" "-"
nginx | 172.20.0.1 - - [02/Nov/2021:02:48:23 +0000] "GET /favicon.ico HTTP/2.0" 301 169 "https://localhost:8443/admin/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36" "-"
nginx | 172.20.0.1 - - [02/Nov/2021:02:48:23 +0000] "GET /favicon.ico/ HTTP/2.0" 404 43 "https://localhost:8443/admin/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36" "-"
I solve this buy adding on my nuxt-config.js
export default {
router: {
base: '/admin/'
}
}
I am using docker compose for running nginx with latest version, using the volumes i am copying the nginx.conf files into nginx docker container
nginx:
image: nginx:1.20
container_name: nginx
ports:
- 80:80
restart: unless-stopped
volumes:
- ./nginx/nginx.conf:/etc/nginx/default.conf
depends_on:
- strapi
- rocketchat
- keycloak
networks:
- test-network
Every applications are running on a same Network.
Here is the nginx.conf file
events {
worker_connections 4096; ## Default: 1024
}
http {
server {
listen 80;
server_name qa.xxx.com;
location / {
proxy_pass http://strapi-container:1337/;
}
location /chat {
proxy_pass http://rocketchat-container:3000;
}
location /auth {
proxy_pass http://keycloak-container:8080;
proxy_set_header Host $host;
}
}
}
My intention is to run the three backend URL /, /chat, /auth with nginx configurations. When running the application on instance, http://ip-address/chat, http://ip-address/auth doesn't seems to work
Here is the nginx log error
2021/06/02 07:46:42 [error] 31#31: *1 open() "/usr/share/nginx/html/chat" failed (2: No such file or directory), client: 115.96.103.237, server: localhost, request: "GET /chat HTTP/1.1", host: "310.28.67.222"
115.96.103.237 - - [02/Jun/2021:07:46:42 +0000] "GET /chat HTTP/1.1" 404 555 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36" "-"
2021/06/02 07:46:50 [error] 31#31: *2 open() "/usr/share/nginx/html/auth" failed (2: No such file or directory), client: 115.96.103.237, server: localhost, request: "GET /auth HTTP/1.1", host: "310.28.67.222"
115.96.103.237 - - [02/Jun/2021:07:46:50 +0000] "GET /auth HTTP/1.1" 404 555 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36" "-"
Try trailing slashes behind the proxy_pass, unless you want to have the folder added.
location /chat {
proxy_pass http://rocketchat-cnr:3000;
}
redirects to http://rocketchat-cnr:3000/chat, while
location /chat {
proxy_pass http://rocketchat-cnr:3000/;
}
redirects to http://rocketchat-cnr:3000/
Visit http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass for more Info
I am building a vue.js app which I want to serve using nginx. However, it fails with the error ERR_TOO_MANY_REDIRECTS.
In the beginning nginx tried to redirect to port 80 when the external port was 8000. That's why I added the line absolute_redirect off; to the config. However, this only caused a redirect loop:
172.17.0.1 - - [02/Jan/2019:15:24:24 +0000] "GET / HTTP/1.1" 301 169 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"
172.17.0.1 - - [02/Jan/2019:15:24:24 +0000] "GET / HTTP/1.1" 301 169 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"
172.17.0.1 - - [02/Jan/2019:15:24:24 +0000] "GET / HTTP/1.1" 301 169 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"
172.17.0.1 - - [02/Jan/2019:15:24:24 +0000] "GET / HTTP/1.1" 301 169 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"
172.17.0.1 - - [02/Jan/2019:15:24:24 +0000] "GET / HTTP/1.1" 301 169 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"
172.17.0.1 - - [02/Jan/2019:15:24:24 +0000] "GET / HTTP/1.1" 301 169 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"
172.17.0.1 - - [02/Jan/2019:15:24:24 +0000] "GET / HTTP/1.1" 301 169 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"
Here's the /etc/nginx/nginx.conf file:
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
sendfile off;
keepalive_timeout 60;
# gzip on;
include /etc/nginx/conf.d/*.conf;
}
And here is the website config:
server {
absolute_redirect off;
port_in_redirect off;
location / {
root /usr/share/nginx/html/;
try_files / /index.html;
}
}
What am I missing here?
try_files / /index.html;
This loops back to your location /, change it to try_files $uri $uri/ /index.html =404; should solve your problem
Running 4 docker containers: server/client/nginx/mongo reverse-proxy
The client seems to work on port 4200, I can see my app and routing works.
When trying to register a user, I am requesting the below httpclient post to the Docker container called "myserver" which should call the api.
I am getting this DNS error message.
When looking at the NGINX container at the resolv.conf (it shows 127.0.0.11) which is the correct dns Docker resolves the dns-es with within the container.
When I ping from within the container to another container's name it works.
when I telnet from within and outside of the container on the servers port 3000 it works as well as telnetting on client's port 4200
So what could be the cause of not being able to resolve myserver when I am trying to register an account on my website.
it is almost as if the call to register is being made from outside the container, so it can't resolve it, is this normal behavior?
let url = 'http://myserver:3000/api/register';
docker-compose.yml
version: '3'
services:
nginx:
build: ./nginx
# Map Nginx port 80 to the local machine's port 80
volumes:
- ./dist:/usr/share/nginx/html
ports:
- "80:80"
depends_on:
- client
networks:
- app-network
# Build the container using the client Dockerfile
client:
build: ./
# This line maps the contents of the client folder into the container.
volumes:
- ./:/usr/src/app
ports:
- "4200:4200"
networks:
- app-network
myserver:
build: ./express-server
volumes:
- ./express-server:/usr/src/server
environment:
- NODE_ENV=development
depends_on:
- mongo
ports:
- "3000:3000"
networks:
- app-network
# Link the client container so that Nginx will have access to it
mongo:
environment:
- AUTH=yes
- MONGO_INITDB_ROOT_USERNAME=superAdmin
- MONGO_INITDB_ROOT_PASSWORD=admin123
image: mongo
volumes:
- /var/mongodata/data:/data/db
ports:
- "27017:27017"
networks:
- app-network
networks:
app-network:
driver: bridge
nginx default.conf
worker_processes 2 ;
events {
worker_connections 1024;
}
http {
upstream my-server {
server myserver:3000;
}
upstream client {
server client:4200;
}
server {
location / {
proxy_pass http://client;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-For $remote_addr;
}
location /api/ {
proxy_pass http://my-server;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
}
cbdb2e00c6db meanchat_nginx "nginx -g 'daemon ..." 15 minutes ago Up 15 minutes 0.0.0.0:80->80/tcp meanchat_nginx_1
22406a1e9a31 meanchat_client "npm start" 15 minutes ago Up 15 minutes 0.0.0.0:4200->4200/tcp meanchat_client_1
aa024855d201 meanchat_myserver "npm start" About an hour ago Up 15 minutes 0.0.0.0:3000->3000/tcp meanchat_myserver_1
b657bd6db7b5 mongo "docker-entrypoint..." 5 hours ago Up 15 minutes 0.0.0.0:27017->27017/tcp b657bd6db7b5_meanchat_mongo_1
172.20.0.1 - - [09/Jan/2018:00:30:02 +0000] "GET /sockjs-node/info?t=1515457802565 HTTP/1.1" 200 90 "http://localhost/register" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
172.20.0.1 - - [09/Jan/2018:00:30:03 +0000] "GET /register HTTP/1.1" 304 0 "http://localhost/register" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
172.20.0.1 - - [09/Jan/2018:00:30:03 +0000] "GET /sockjs-node/044/zh1t2skg/websocket HTTP/1.1" 101 162 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"172.20.0.1 - - [09/Jan/2018:00:30:03 +0000] "GET /inline.bundle.js HTTP/1.1" 304 0 "http://localhost/register" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
172.20.0.1 - - [09/Jan/2018:00:30:03 +0000] "GET /polyfills.bundle.js HTTP/1.1" 304 0 "http://localhost/register" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
172.20.0.1 - - [09/Jan/2018:00:30:03 +0000] "GET /vendor.bundle.js HTTP/1.1" 304 0 "http://localhost/register" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
172.20.0.1 - - [09/Jan/2018:00:30:03 +0000] "GET /styles.bundle.js HTTP/1.1" 304 0 "http://localhost/register" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
172.20.0.1 - - [09/Jan/2018:00:30:03 +0000] "GET /scripts.bundle.js HTTP/1.1" 304 0 "http://localhost/register" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
172.20.0.1 - - [09/Jan/2018:00:30:03 +0000] "GET /main.bundle.js HTTP/1.1" 200 796563 "http://localhost/register" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
172.20.0.1 - - [09/Jan/2018:00:30:06 +0000] "GET /assets/img/cryptoowls.jpg HTTP/1.1" 304 0 "http://localhost/register" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
172.20.0.1 - - [09/Jan/2018:00:30:06 +0000] "GET /roboto-v15-latin-regular.7e367be02cd17a96d513.woff2 HTTP/1.1" 304 0 "http://localhost/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
172.20.0.1 - - [09/Jan/2018:00:30:06 +0000] "GET /sockjs-node/info?t=1515457806564 HTTP/1.1" 200 90 "http://localhost/register" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
172.20.0.1 - - [09/Jan/2018:03:31:24 +0000] "GET /sockjs-node/info?t=1515468684563 HTTP/1.1" 200 90 "http://localhost/register" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
Answer from comments:
When connecting to containers with a reverse proxy, all the URL's used by the application need to point to that reverse proxy and not the application. Typically you do this by giving a path in the URL without a hostname. If you plan to use a virtual path based reverse proxy, you'll want to go a step further and use relative paths in your URL's. This all needs to happen in the responses being sent by the application to the client.
Below is my docker-compose.yml file.
I am trying to set reverse proxy.
I tried setting VIRTUAL_HOST and VIRTUAL_PORT Environment to virtual docker containers. it is accessible using Host name and port like http://services.local:81
Please suggest how can I access these services using browser.
I want to access services like http://service1.services.local or http://services.local/service1 Here http://services.local is my default host
Note:- I am using jwilder/nginx-proxy
docker-compose.yml
version: '2'
networks:
prodnetwork:
driver: bridge
services:
nginx-proxy:
image: jwilder/nginx-proxy
container_name: nginx-proxy
ports:
- "80:80"
networks:
- prodnetwork
volumes:
- "/var/run/docker.sock:/tmp/docker.sock:ro"
- "./nginx.tmpl:/app/nginx.tmpl:ro"
- /etc/nginx/conf.d
environment:
- DEFAULT_HOST=services.local,localhost
dockergen:
image: jwilder/docker-gen
command: -notify-sighup nginx -watch /etc/docker-gen/templates/nginx.tmpl /etc/nginx/conf.d/default.conf
networks:
- prodnetwork
volumes_from:
- nginx-proxy
volumes:
- /var/run/docker.sock:/tmp/docker.sock:ro
- ./nginx.tmpl:/etc/docker-gen/templates/nginx.tmpl
service1:
build: vgw1
container_name: service1
networks:
- prodnetwork
ports:
- "81:80"
environment:
- VIRTUAL_HOST=service1.services.local
expose:
- "81"
service2:
build: vgw2
container_name: service2
networks:
- prodnetwork
ports:
- "82:80"
environment:
- VIRTUAL_HOST=service2.services.local
expose:
- "82"
Here vgw1 and vgw2 are two separate dockers which are having Node-Red installed.
Below is nginx-proxy log
WARNING: /etc/nginx/dhparam/dhparam.pem was not found. A pre-generated dhparam.pem will be used for now while a new one
is being generated in the background. Once the new dhparam.pem is in place, nginx will be reloaded.
forego | starting dockergen.1 on port 5000
forego | starting nginx.1 on port 5100
dockergen.1 | 2017/09/01 13:16:59 Generated '/etc/nginx/conf.d/default.conf' from 4 containers
dockergen.1 | 2017/09/01 13:16:59 Running 'nginx -s reload'
dockergen.1 | 2017/09/01 13:17:09 Error running notify command: nginx -s reload, exit status 1
dockergen.1 | 2017/09/01 13:17:09 Watching docker events
dockergen.1 | 2017/09/01 13:17:09 Contents of /etc/nginx/conf.d/default.conf did not change. Skipping notification 'nginx -s reload'
nginx.1 | 172.18.0.1 - - [01/Sep/2017:13:17:51 +0000] "GET /service1 HTTP/1.1" 404 571 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36" "-"
nginx.1 | 2017/09/01 13:17:51 [error] 37#37: *1 open() "/usr/share/nginx/html/service1" failed (2: No such file or directory), client: 172.18.0.1, server: localhost, request: "GET /service1 HTTP/1.1", host: "services.local"
nginx.1 | 172.18.0.1 - - [01/Sep/2017:13:18:03 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36" "-"
nginx.1 | 172.18.0.1 - - [01/Sep/2017:13:18:06 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36" "-"
nginx.1 | 2017/09/01 13:18:49 [error] 37#37: *1 "/usr/share/nginx/html/service1/index.html" is not found (2: No such file or directory), client: 172.18.0.1, server: localhost, request: "GET /service1/ HTTP/1.1", host: "services.local"
nginx.1 | 172.18.0.1 - - [01/Sep/2017:13:18:49 +0000] "GET /service1/ HTTP/1.1" 404 571 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36" "-"
2017/09/01 13:19:21 [warn] 44#44: server name "services.local/service1" has suspicious symbols in /etc/nginx/conf.d/default.conf:74
nginx: [warn] server name "services.local/service1" has suspicious symbols in /etc/nginx/conf.d/default.conf:74
2017/09/01 13:19:21 [emerg] 44#44: host not found in upstream "services.local" in /etc/nginx/conf.d/default.conf:78
nginx: [emerg] host not found in upstream "services.local" in /etc/nginx/conf.d/default.conf:78
Generating DH parameters, 2048 bit long safe prime, generator 2
This is going to take a long time
dhparam generation complete, reloading nginx
nginx.1 | 2017/09/01 13:23:27 [error] 37#37: *6 "/usr/share/nginx/html/service1/index.html" is not found (2: No such file or directory), client: 172.18.0.1, server: localhost, request: "GET /service1/ HTTP/1.1", host: "services.local"
nginx.1 | 172.18.0.1 - - [01/Sep/2017:13:23:27 +0000] "GET /service1/ HTTP/1.1" 404 571 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36" "-"
Custom dhparam.pem file found, generation skipped
forego | starting dockergen.1 on port 5000
forego | starting nginx.1 on port 5100
dockergen.1 | 2017/09/04 07:18:19 Generated '/etc/nginx/conf.d/default.conf' from 4 containers
dockergen.1 | 2017/09/04 07:18:19 Running 'nginx -s reload'
dockergen.1 | 2017/09/04 07:18:19 Watching docker events
dockergen.1 | 2017/09/04 07:18:19 Contents of /etc/nginx/conf.d/default.conf did not change. Skipping notification 'nginx -s reload'
nginx.1 | localhost 172.18.0.1 - - [04/Sep/2017:07:26:42 +0000] "GET /favicon.ico HTTP/1.1" 503 615 "http://localhost/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36"
/nginx/conf.d/default.conf
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
In order to change the route that your browser maps too, you are going to have to change the configuration in your etc/hosts file. This is virtually telling your browser to look up the DNS records on your local machine for the url that you specify.
There are tools to accomplish this with including GasMask (osX) and Host File Editor (Windows)