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.
Related
I'm using nginx in docker-compose file for handling my frontend and backend website.
I had no problems for a long time but once I've got the error "504 Gateway Time-out" when I try to access my project through localhost and it's port
http://localhost:8080
when I type docker Ip and its port
http://172.18.0.1:8080
I can access the project and nginx works correctly.
I'm sure my config file is correct because It was working for 6 months and I don't know what happened for it.
what should I check to find the problem?
docker-compose file:
.
.
.
nginx:
container_name: nginx
image: nginx:1.19-alpine
restart: unless-stopped
ports:
- '8080:80'
volumes:
- ./frontend:/var/www/html/frontend
- ./nginx/nginx.conf:/etc/nginx/conf.d/default.conf
networks:
- backend_appx
networks:
backend_appx :
external: true
.
.
nginx config file:
upstream nextjs_upstream {
server next_app:3000;
}
server {
listen 80 default_server;
server_name _;
server_tokens off;
# set root
root /var/www/html/frontend;
# set log
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
location /_next/static {
proxy_cache STATIC;
proxy_pass http://nextjs_upstream;
add_header X-Cache-Status $upstream_cache_status;
}
}
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
I'm trying to deploy a docker-compose stack that contains a Nginx service to manage the others like Jenkins or Grafana. However, when I target my Jenkins service like http://myapp.fr/jenkins, the homepage of the service will redirect to the login page at this URL: http://myapp.fr/login?from=%2F. How I can fix this for all services to prefix with the service name for all pages of it (like http://myapp.fr/jenkins/login?from=%2F).
My stack looks like this :
version: "3.7"
services:
## NGINX ##
web:
image: nginx
container_name: myapp_nginx
volumes:
- ./config/nginx/templates:/etc/nginx/templates
- ./config/nginx/include.d:/etc/nginx/include.d
- ./config/nginx/www:/var/www/certbot
ports:
- "80:80"
- "443:443"
environment:
- NGINX_HOST=myapp.fr
- NGINX_PORT=80
networks:
- myapp
## JENKINS ##
jenkins:
build:
context: ./dockerfile/jenkins
dockerfile: Dockerfile
container_name: myapp_jenkins
hostname: jenkins.myapp.fr
privileged: true
user: root
ports:
- 8080:8080
- 50000:50000
volumes:
- ~/jenkins:/var/jenkins_home
- /var/run/docker.sock:/var/run/docker.sock
- /usr/local/bin/docker:/usr/local/bin/docker
restart: always
networks:
- myapp
## GRAFANA ##
grafana:
image: grafana/grafana-enterprise:8.2.0
container_name: myapp_grafana
hostname: grafana.myapp.fr
ports:
- 3000:3000
user: "104"
networks:
- myapp
networks:
myapp:
driver: bridge
My default.conf of nginx is in one of my docker volumes and it looks like this :
server {
listen 80;
listen [::]:80;
server_name myapp.fr;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
location /jenkins/ {
proxy_set_header Host $host;
add_header Access-Control-Allow-Origin '*';
proxy_pass http://jenkins.myapp.fr:8080/;
}
location /grafana/ {
proxy_set_header Host $host;
add_header Access-Control-Allow-Origin '*';
proxy_pass http://grafana.myapp.fr:3000/;
}
add_header Access-Control-Allow-Origin '*';
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
add_header Access-Control-Allow-Headers 'User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization,Access-Control-Allow-Origin';
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
When I began the deploy, I use a docker-compose up -d command and all services are running. myapp.fr show the nginx index. However, if I specified the subpath myapp.fr/jenkins, it will be showig a 404 error because of a redirection to http://myapp.fr/login?from=%2F
How I can fix this for all services to prefix service name for all pages of it (like http://myapp.fr/jenkins/login?from=%2F).
Thanks
You have to tell Jenkins that you are using an additional context path, so it generates all the redirections with the added context. You can do this by passing the --prefix=/jenkins on startup. You can pass this through JENKINS_OPTS environment variable as well. Following is how you can pass this to docker run.
docker run --name myjenkins -p 8080:8080 -p 50000:50000 --env JENKINS_OPTS="--prefix=/jenkins" jenkins/jenkins:lts
Update
Following is a sample NginX configuration you can use. Note the proxy_redirect option specified, which makes sure all redirect URLs are changed to your correct Hostname.
server {
listen 80;
server_name jenkins.ycr.com;
location /jenkins {
proxy_pass http://127.0.0.1:8081;
proxy_redirect http://127.0.0.1:8081/ http://jenkins.ycr.com/;
}
}
I used different docker-compose.yml to up two sites
nginx1 mapping 8080:80
nginx2 mapping 8081:80
nginx-proxy forward 8080 to 80 and 8081 to 81
The results localhost:8080 and localhost:8081 are fine, but localhost:80 and localhost:81 are not works, I don`t know why, and tried add containers to a common networks, not works too
I expect visit localhost:80 and localhost:81 to get right responses
nginx-proxy directory
#docker-compose.yml
version: "3"
services:
test-nginx-proxy:
image: nginx:stable-alpine
container_name: test-nginx-proxy
restart: always
ports:
- 80:80
- 81:81
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
networks:
- custom-nginx-net
networks:
custom-nginx-net:
external:
name: nginx-net
# nginx.conf
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 300;
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://localhost:8080;
}
}
server {
listen 81;
server_name localhost;
location / {
proxy_pass http://localhost:8081;
}
}
}
nginx1
#docker-compose.yml
version: "3"
services:
test-nginx1:
image: nginx:stable-alpine
container_name: test-nginx1
restart: always
ports:
- 8080:80
volumes:
- ./:/usr/share/nginx/html
- ./nginx.conf:/etc/nginx/nginx.conf
networks:
- custom-nginx-net
networks:
custom-nginx-net:
external:
name: nginx-net
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 300;
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html;
}
}
}
nginx2
version: "3"
services:
test-nginx2:
image: nginx:stable-alpine
container_name: test-nginx2
restart: always
ports:
- 8081:80
volumes:
- ./:/usr/share/nginx/html
- ./nginx.conf:/etc/nginx/nginx.conf
networks:
- custom-nginx-net
networks:
custom-nginx-net:
external:
name: nginx-net
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 300;
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html;
}
}
}
In your docker-compose file for the ports section you have added like this
ports:
- 8080:80
which means port 8080 will be mapped to port 80. here 8080 is the host port, this port you will be able to access in your machine, whereas port 80 is container port, this will not be accessible outside the container.
To access port 80 outside you need to map it to host port the similar way you did for port 8080 as shown below
ports:
- 8080:80
- 80:80
do the same thing for port 81 as well
sorry for the question. I'm new with docker and nginx. I need some help
So, I have a docker with the nginx proxy and 3 dockers with 3 different applications
I don't know how to configure nginx at all to avoid bad gateway errors on my 3 applications. Can you please help me?
I can only find help for this problem on 1 application.
My docker-compose
version: '2.1'
services:
proxy:
container_name: proxy
hostname: proxy
build: .docker/nginx
ports:
- "80:80" #80:80
- "443:443"
volumes:
- ./certs:/etc/nginx/certs
- /var/run/docker.sock:/tmp/docker.sock:ro
networks:
- proxy
restart: always
networks:
proxy:
external: true
My DockerFile
FROM jwilder/nginx-proxy
RUN { \
echo 'client_max_body_size 500M;'; \
} > /etc/nginx/conf.d/my_proxy.conf
App1
version: '3'
services:
apache:
build: .docker/apache
container_name: app1_apache
environment:
VIRTUAL_HOST: "app1.local"
expose:
- "80"
volumes:
- .docker/config/vhosts:/etc/apache2/sites-enabled
- ../../app1:/var/www/app1.local
depends_on:
- php
networks:
- default
- proxy
php:
build: .docker/php
container_name: app1_php
environment:
VIRTUAL_HOST: "app1.local"
volumes:
- ../../app1:/var/www/app1.local
networks:
- default
- proxy
networks:
proxy:
external: true
Same file for App2 and App3
I found this solution but I don't understand how to implement it.
nginx docker container: 502 bad gateway response
I need to modify the nginx config.d configuration file but I don't see how I can do that.
my default config.d file
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/log/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;
#}
}
So, I have to add
upstream app1{
//insert your hosts ip here
server 192.168.99.100:8080;
}
upstream app2{
//insert your hosts ip here
server 192.168.99.100:8080;
}
upstream app3{
//insert your hosts ip here
server 192.168.99.100:8080;
}
That's right ?
Do I also have to change the proxy_pass?
The nginx configuration is really obscure to me.
Thank you for your help