I create docker like this:
version: '3'
services:
nginx:
image: nginx
container_name: test1_nginx
restart: always
volumes:
- ./docker/nginx/nginx.conf:/etc/nginx/nginx.conf
ports:
- "80:80"
- "443:443"
site1:
restart: always
container_name: test1_site1
build: ./site1/
command: "npm run start"
ports:
- "3001:3001"
site2:
restart: always
container_name: test1_site2
build: ./site2/
command: "npm run start"
ports:
- "3002:3002"
with struct of project like this: struct of project
Here is ./docker/nginx/nginx.conf:
events {
worker_connections 1024;
}
http {
upstream site1 {
server localhost:3001;
}
upstream site2 {
server localhost:3002;
}
server {
listen 80;
listen [::]:80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm index.php;
}
location /site1 {
proxy_pass http://localhost:3001;
proxy_set_header Host $host;
}
location /site2 {
proxy_pass http://site2;
proxy_set_header Host $host;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
}
http://localhost:80 working fine
http://localhost:3001 working fine
http://localhost:3002 working fine
I want:
when I enter http://localhost/test1, it will pass proxy with http://localhost:3001
when I enter http://localhost/test3, it will pass proxy with http://localhost:3003
Please let me know how to do it.
Thank you
Related
I have the following docker compose:
version: '3.1'
services:
backend:
container_name: backend
image: backendnode
restart: always
ports:
- 3000:3000
frontend:
container_name: frontend
image: frontnginx
restart: always
ports:
- 4200:80
apigw:
image: reverseproxy
restart: always
ports:
- 80:80
depends_on:
- frontend
- backend
This is the reverseproxy image nginx.conf:
worker_processes auto;
events { worker_connections 1024; }
http {
server {
listen 80;
server_name localhost 127.0.0.1;
location / {
proxy_pass http://frontend:4200;
proxy_set_header X-Forwarded-For $remote_addr;
}
location /api {
proxy_pass http://backend:3000;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
}
When running docker-compose run, I get the following results:
localhost:80/api/users: works great, nginx redirects to backend properly.
localhost:80/index.html: not working, I get the following error:
connect() failed (111: Connection refused) while connecting to upstream, client: 172.20.0.1, server: localhost, request: "GET /index.html HTTP/1.1", upstream: "http://172.20.0.5:4200/index.html", host: "localhost:80"
Frontend is a simple nginx web server, this is its nginx.conf:
events{}
http {
include /etc/nginx/mime.types;
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
index index.html;
location / {
try_files $uri $uri/ /index.html;
}
}
}
Any idea why reverse proxy it's not working with frontend routes?
Created answer from the comment thread:
Docker networking works like this: if you use communication within docker's network, you need to refer to the internal ports. Since port mapping is used for the "outside world". So in your case, you would need to refer to "frontend:80" instead of 4200.
I followed the example: How to upload multiple files with django rest api? The solution work in localhost, but when i upload to nginx server running in a container docker, the nginx return a error 400, i can't more details because i'm not getting log, my nginx configuration it is like this:
upstream api {
server neworleansenglishcourse.com.br:8000;
}
server {
listen 80;
server_name neworleansenglishcourse.com.br www.neworleansenglishcourse.com.br;
location / {
client_max_body_size 8000M;
client_body_buffer_size 8000M;
client_body_timeout 120;
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
location /api/ {
client_max_body_size 8000M;
client_body_buffer_size 8000M;
client_body_timeout 120;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Url-Scheme $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_pass http://api;
}
location /complementos/ {
client_max_body_size 8000M;
client_body_buffer_size 8000M;
client_body_timeout 120;
}
}
My docker compose it is like this:
version: '3'
services:
backend:
build:
context: ./api
command: gunicorn plataformaingles.wsgi --bind 0.0.0.0:8000
ports:
- "8000:8000"
depends_on:
- db
networks:
- production-network
frontend:
build:
context: ./front
volumes:
- front_build:/front/build
networks:
- production-network
nginx:
image: nginx:latest
ports:
- "80:80"
volumes:
- ./nginx/nginx-setup.conf:/etc/nginx/conf.d/default.conf
- front_build:/usr/share/nginx/html
depends_on:
- backend
- frontend
networks:
- production-network
volumes:
front_build:
networks:
production-network:
driver: bridge
I want to redirect all traffic from http://localhost:8080 to http://my-service:8080
But when I access http://localhost:8080 the nginx redirects me to http://localhost
This is my nginx.conf
events {
worker_connections 1024; ## Default: 1024
}
http{
server {
listen 8080 default_server;
listen [::]:8080 default_server;
server_name localhost;
location / {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://my-service:8080/;
}
}
}
And this is my docker-compose
version: '2.1'
services:
nginx-proxy:
image: nginx:stable-alpine
container_name: nginx-proxy
ports:
- "8080:8080"
volumes:
- ./data/nginx/nginx.conf:/etc/nginx/nginx.conf
networks:
- no-internet
- internet
my-service:
....
expose:
- "8080"
networks:
- no-internet
networks:
internet:
driver: bridge
no-internet:
internal: true
driver: bridge
when I run the docker compose without the nginx, I can access http://localhost:8080 without redirection.
I solved the problem:
server {
listen 8080;
listen [::]:8080;
server_name localhost;
location / {
proxy_pass http://ap-service:8080;
proxy_redirect http://ap-service:8080/ $scheme://$host:8080/;
}
}
I am trying to set up a Dockerised Mern app secured with ssl. So far i have managed to serve my react app via nginx container, but am now having issues with my api backend.
if my web page sends a https request to my nginx container, how can i take that request, downgrade it to http, and send it to the api container, and securely return the response? it this even the preferred approach?
my Nginx conf
server {
listen 80;
server_name example.com;
location /.well-known/acme-challenge/ {
root /var/www/certbot;
}
location / {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl;
server_name example.com;
root /var/www/html/build;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
location / {
index index.html
try_files $uri $uri/ #backend;
}
location /login {
return 301 https://$host;
}
location #backend {
proxy_pass https://example.com;
}
my docker-compose.yml
version: '3.7'
services:
nginx:
image: nginx:1.15-alpine
ports:
- "80:80"
- "443:443"
volumes:
- ./data/build:/var/www/html/build
- ./data/nginx:/etc/nginx/conf.d
- ./data/certbot/conf:/etc/letsencrypt
- ./data/certbot/www:/var/www/certbot
certbot:
image: certbot/certbot
volumes:
- ./data/certbot/conf:/etc/letsencrypt
- ./data/certbot/www:/var/www/certbot
backend:
container_name: backend
restart: unless-stopped
build: ./api
ports:
- '3001:3001'
env_file: ./api/.env
environment:
- NODE_ENV=production
I managed to find a solution to my problem.
i changed by docker-compose.yml to:
version: '3.7'
services:
nginx:
image: nginx:1.15-alpine
ports:
- "80:80"
- "443:443"
volumes:
- ./data/build:/var/www/html/build
- ./data/nginx:/etc/nginx/conf.d
- ./data/certbot/conf:/etc/letsencrypt
- ./data/certbot/www:/var/www/certbot
links:
- backend
certbot:
image: certbot/certbot
volumes:
- ./data/certbot/conf:/etc/letsencrypt
- ./data/certbot/www:/var/www/certbot
backend:
container_name: backend
restart: unless-stopped
build: ./api
ports:
- '3001:3001'
env_file: ./api/.env
environment:
- NODE_ENV=production
and my nginx conf to:
server {
listen 80;
server_name example.com;
location /.well-known/acme-challenge/ {
root /var/www/certbot;
}
location / {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl;
server_name example.com;
root /var/www/html/build;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
location / {
index index.html
try_files $uri $uri/ #backend;
}
location /login {
return 301 https://$host;
}
location #backend {
proxy_pass https://example.com;
}
location /api/ {
proxy_pass http://backend:3001/api/;
}
I have this architecture:
- 1 docker component running nginx on the host's port 80
- app with 2 services: one node and one mongodb
the docker-compose file:
version: '2'
services:
backend:
build: ./back-end/
container_name: "app-back-end"
volumes:
- ./back-end/:/usr/src/dance-app-back
- /usr/src/app-back/node_modules
ports:
- "3000:3050"
links:
- mongodb
mongodb:
image: mongo:3.2.15
ports:
- "3100:27017"
volumes:
- ./data/mongodb:/data/db
The nginx config file
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location /back_1 {
#proxy_pass http://172.17.0.2:5050/;
proxy_pass http://0.0.0.0:5050/;
}
#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;
}
}
The nginx container seems not to be able to reach the port 3000 on the host.
What I'm I doing wrong?
If you have port 3000 on your host and would like to map it to your container port, than you need to do that in your docker-compose file
Change:
backend:
build: ./back-end/
container_name: "app-back-end"
volumes:
- ./back-end/:/usr/src/dance-app-back
- /usr/src/app-back/node_modules
ports:
- "3000:3000"
links:
- mongodb
Then you can link nginx to your container that you want to proxy pass to.
Add to your compose file:
nginx:
restart: always
image: nginx:1.13.1
ports:
- "80:80"
depends_on:
- backend
links:
- backend:backend
And then in your nginx file mention the name of your application container that you would like to proxy to with the port of that container that is open.
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location /back_1 {
proxy_pass http://backend:3000/;
}
#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;
}
}
Instead of the IP of the container inyour nginx, you can link the container name and the nginx config and docker will resolve those IPs for you.