anhduc.bkhn
anhduc.bkhn

Reputation: 743

Problem with docker-compose, nginx and specified location

This is my docker-compose.yml

version: '2' 
services: 
    nginx:
        image: nginx:1.11.8-alpine
        ports:
          - "8081:80"
        volumes:
          - ./code:/usr/share/nginx/html
          - ./html:/myapp
          - ./site.conf:/etc/nginx/conf.d/site.conf
          - ./error.log:/var/log/nginx/error.log
          - ./nginx.conf:/etc/nginx/nginx.conf

And this is site.conf

server {

   listen 8081;
   index index.html index.php;
   server_name localhost;
   error_log  /var/log/nginx/error.log;

   location /html {
       root /myapp;
   }
}

It works on http://nginx-php-docker.local:8081/, showing the file index.html which is inside the /code folder.

But It is not working with http://nginx-php-docker.local:8081/html

The error log is

2017/01/13 08:50:27 [error] 7#7: 
  *4 open() "/usr/share/nginx/html/html" failed (2: No such file or directory), 
    client: 172.19.0.1, 
    server: localhost, 
    request: "GET /html HTTP/1.1", 
    host: "nginx-php-docker.local:8081"

Upvotes: 1

Views: 13519

Answers (3)

Tyr_90
Tyr_90

Reputation: 31

Edit you'r compose file to be like this ,, you are redirecting the html file to the wrong directory

version: '2' 

services: 

nginx:

image: nginx:1.11.8-alpine
ports:
  - "8081:80"
volumes:
  - ./code:/var/www/html/
  - ./html:/myapp
  - ./site.conf:/etc/nginx/conf.d/site.conf
  - ./error.log:/var/log/nginx/error.log
  - ./nginx.conf:/etc/nginx/nginx.conf

Upvotes: 0

Marcus Pereira
Marcus Pereira

Reputation: 219

In your terminal run the command:

sudo setenfonce 0

Now, re-run the docker command.

Upvotes: -3

rckrd
rckrd

Reputation: 3354

You are configuring the nginx container to listen on port 8081 in site.conf, this makes no sense since the image exposes port 80 and 443.

Change your site.conf to:

server {
   listen 80;
   index index.html index.php;
   server_name localhost;
   error_log  /var/log/nginx/error.log;
   location / {
      root /usr/share/nginx/html;
   }

   location /html {
      alias /myapp;
   }
}

And the following line in docker-compose.yml:

volumes:  
  - ./site.conf:/etc/nginx/conf.d/default.conf

Upvotes: 9

Related Questions