Docker-nginx: Unable to provide custom nginx config

Created on 21 Sep 2017  Â·  5Comments  Â·  Source: nginxinc/docker-nginx

The goal is to be able to serve up a single page app and proxy requests to a back-end. I've done this frequently in the passed and I set up an install of just nginx on my local mac to test it just to make sure I'm not crazy and everything still works as I'd expect.

The issue is that the try_files directive doesn't not appear to be working when I provide my own nginx.conf.

I copied the default config from the instructions. This is what I've got:

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;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

Then I updated it to add a location block with try_files.

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;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
    server {
        # listen       80;
        # server_name  localhost;

        root   html;
        # root   /usr/share/nginx/html;
        location / {
            try_files $uri index.html;
        }
    }
}

Just to simplify it I'm just trying to route every request to index.html i.e. http://localhost:8080/some-other-route would render the same html as http://localhost:8080/.

The base route works as expected, but the some-other-route throws a 404 nginx error.

2017/09/20 23:01:55 [error] 7#7: *2 open() "/usr/share/nginx/html/some-other-route" failed (2: No such file or directory), client: 172.17.0.1, server: localhost, request: "GET /some-other-route HTTP/1.1", host: "localhost:8082"
172.17.0.1 - - [20/Sep/2017:23:01:55 +0000] "GET /some-other-route HTTP/1.1" 404 571 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36" "-"

My Dockerfile looks like this:

FROM nginx
COPY index.html /usr/share/nginx/html
COPY nginx.conf /etc/nginx/nginx.conf

Am I doing something wrong or is this just not supported with the base nginx docker image?

Most helpful comment

Success!
Since I'm still new to docker and it to me a minute to find out how to get a handle on the insides by using docker exec. I think I was missing a fully specified server block when I stopped referencing include /etc/nginx/conf.d/*.conf;.

Here's the working config:
Note: root must be defined as root /usr/share/nginx/html; not root html;

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;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    # include /etc/nginx/conf.d/*.conf;
    server {
        listen       80;
        server_name  localhost;

        root   /usr/share/nginx/html;
        location / {
            try_files $uri /index.html;
        }
    }
}

It would definitely be useful to have something in the docs for this image about both (/etc/nginx/nginx.conf, /etc/nginx/conf.d/default.conf) config files being use.

All 5 comments

remove the "include" line from your nginx.conf and try again - seems default.conf is getting picked up.

I should have mentioned this in the original issue. If I remove the include
line I only ever see a 500 or 404. I'll have to double check to be sure
which one exactly but the main point is that nothing works if I comment
that out.
On Thu, Sep 21, 2017 at 6:33 AM Konstantin Pavlov notifications@github.com
wrote:

remove the "include" line from your nginx.conf and try again - seems
default.conf is getting picked up.

—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/nginxinc/docker-nginx/issues/199#issuecomment-331142791,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ABlDKKdfnLbMhIOxrR7PLVaFG3J9mTSPks5sklezgaJpZM4Pelvm
.

I don't see anything that will "proxy requests to backend" in the provided configurations - make sure you're actually testing what you think you do.

I've got proxy working in the un-dockerized version of nginx. I wasn't putting that in because it's not directly related to the issue. The main issue being that try_files is not working.

I did get this working referencing this project. The two main difference appears to be the Docker file copies the app to a different root and the server config is copied separately to /etc/nginx/conf.d/default.conf. So it doesn't just change /etc/nginx/nginx.conf. Which I expect would still work if the include /etc/nginx/conf.d/*.conf; stuff could be removed without causing an issue.

Success!
Since I'm still new to docker and it to me a minute to find out how to get a handle on the insides by using docker exec. I think I was missing a fully specified server block when I stopped referencing include /etc/nginx/conf.d/*.conf;.

Here's the working config:
Note: root must be defined as root /usr/share/nginx/html; not root html;

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;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    # include /etc/nginx/conf.d/*.conf;
    server {
        listen       80;
        server_name  localhost;

        root   /usr/share/nginx/html;
        location / {
            try_files $uri /index.html;
        }
    }
}

It would definitely be useful to have something in the docs for this image about both (/etc/nginx/nginx.conf, /etc/nginx/conf.d/default.conf) config files being use.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nightlyone picture nightlyone  Â·  4Comments

lrhazi picture lrhazi  Â·  3Comments

lucacome picture lucacome  Â·  3Comments

luckymark picture luckymark  Â·  4Comments

jsdevel picture jsdevel  Â·  5Comments