I created this simple example to correct and check the errors that I have in another more complex project. So I've a docker-compose file with 1 web server and 2 app and a nginx conf file.
Please note, that I'm testing it with Docker Quick Start Terminal for Windows 10 Home Edition, but I've tested even on Ubuntu 18.04 and the outcome is the same.
Going to: http://192.168.99.100:8080/
Default output:
It works!
The only issue here is with app2, since I'm not able to access app2.
Going to http://192.168.99.100:8080/app2.
Error:
Not Found
The requested URL was not found on this server.
I'm not understanding why I'm not able to do that, and where is the error.
location /app2 after location /, but nothing changedlocation /app2 to location /foo, even here same outcome.location / does not works. (e.g. location /bar location /foobar location /test)Change location / in order to refer the app2 instead of app1, and it works but in this way I can't reach app1.
location / {
proxy_pass http://app2:80;
}
location /app1 {
proxy_pass http://app1:80;
}
docker-compose.yml
version: "3.7"
services:
web:
image: nginx:alpine
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
ports:
- 8080:80
networks:
- app1_net
- app2_net
app1:
image: httpd:latest
networks:
- app1_net
app2:
image: httpd:latest
networks:
- app2_net
networks:
app1_net:
app2_net:
nginx.conf
events {}
http {
server{
listen 80;
listen [::]:80;
server_name localhost;
location /app2 {
proxy_pass http://app2:80;
}
location / {
proxy_pass http://app1:80;
}
}
}
This is the correct way, use a regex to allow sub folders navigation and a perfect match on /app2 to redirect traffic on http://app2:
location ~ ^/app2/(.*)$ {
proxy_pass http://app2/$1;
}
location = /app2 {
proxy_pass http://app2/;
}
location / {
proxy_pass http://app1;
}
I have added another stackoverflow answer here.
It is summarized here:
/appx-uri/bla/blo/bli to /appx-uri/bla/blo/bli/ instead of /bla/blo/bli/ which is the default nginx behavior./repairapp/static/.Adding the directive rewrite ^/repairapp/([^static].*[^/])$ /repairapp/$1/ permanent; in the server block fixes it.
I then have
rewrite ^/repairapp/([^static].*[^/])$ /repairapp/$1/ permanent;location /repairapp/static/ {location /repairapp/ {