Docker
Imal Perera  

Nginx Alias Fails in Docker Environment: [Fix]

Spread the love

nginx alias you crated will fail if you are running your application mapping a host port which is not similar to the nginx port, this is basically because nginx does not aware that you are mapping different port than the port it has exposed. so you will see the following behavior

I assume that you have an alias on /app,  your host port is 9000, and your nginx runs in port 8080

localhost:9000/app  will automatically redirect to localhost:8080/app 

But if You do

localhost:9000/app/  it will correctly serve your files. 

so the fix is simple, forward any request that does not have a trailing slash 


server {
    listen       8080;
    location /app {
        alias /usr/share/nginx/html/files;
        if (-d $request_filename) {
            rewrite [^/]$ $scheme://$http_host$uri/ permanent;
        }
    }
}

Leave A Comment