1

I have one website in Laravel and one API in Node.js. I want to host the website to domain.tld and the API to api.domain.tld. First I made a Dockerfile for the Laravel project:

FROM shakyshane/laravel-php:latest

COPY composer.lock composer.json /var/www/my_site

COPY database /var/www/my_site/database

WORKDIR /var/www/my_site

COPY . /var/www/my_site

RUN php composer.phar install --no-dev --no-scripts \
    && rm composer.phar

RUN chown -R www-data:www-data \
        /var/www/my_site/storage \
        /var/www/my_site/bootstrap/cache

RUN php artisan optimize

EXPOSE 80 443 9000

VOLUME [ "/var/www/my_site" ]

Following this tutorial, I started nginx-proxy image like this

docker run -d -p 80:80 -p 443:443 -v /var/run/docker.sock:/tmp/docker.sock -v /home/my_user/docker_proxy.conf:/etc/nginx/conf.d/my_proxy.conf:ro -v /etc/ssl/certs/dhparam.pem:/etc/ssl/certs/dhparam.pem -v /etc/letsencrypt:/etc/letsencrypt:rw -v /etc/nginx/snippets/fastcgi-php.conf:/etc/nginx/snippets/fastcgi-php.conf -v /etc/nginx/fastcgi.conf:/etc/nginx/fastcgi.conf --name proxy jwilder/nginx-proxy

And start my website's container:

docker container run -d --expose 80 --expose 443 -e VIRTUAL_HOST=domain.tld -e VIRTUAL_PORT=80,443 -e VIRTUAL_PROTO=https -v /var/www/storage:/var/www/storage --name my_site my_site

According to the tutorial this should work, but it doesn't. I also tried to set my custom config file for the proxy, which have a server similar to the config file I had in /etc/nginx/sites-available/domain.tld

docker_proxy.conf file:

# If we receive X-Forwarded-Proto, pass it through; otherwise, pass along the
# scheme used to connect to this server
map $http_x_forwarded_proto $proxy_x_forwarded_proto {
  default $http_x_forwarded_proto;
  ''      $scheme;
}
# If we receive X-Forwarded-Port, pass it through; otherwise, pass along the
# server port the client connected to
map $http_x_forwarded_port $proxy_x_forwarded_port {
  default $http_x_forwarded_port;
  ''      $server_port;
}
# If we receive Upgrade, set Connection to "upgrade"; otherwise, delete any
# Connection header that may have been passed to this server
map $http_upgrade $proxy_connection {
  default upgrade;
  '' close;
}
# Apply fix for very long server names
server_names_hash_bucket_size 128;
# Default dhparam
ssl_dhparam /etc/ssl/certs/dhparam.pem;
# Set appropriate X-Forwarded-Ssl header
map $scheme $proxy_x_forwarded_ssl {
  default off;
  https on;
}
gzip_types text/plain text/css application/javascript application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
log_format vhost '$host $remote_addr - $remote_user [$time_local] '
                 '"$request" $status $body_bytes_sent '
                 '"$http_referer" "$http_user_agent"';
access_log off;
resolver 168.63.129.16;
# HTTP 1.1 support
proxy_http_version 1.1;
proxy_buffering off;
proxy_set_header Host $http_host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $proxy_connection;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $proxy_x_forwarded_proto;
proxy_set_header X-Forwarded-Ssl $proxy_x_forwarded_ssl;
proxy_set_header X-Forwarded-Port $proxy_x_forwarded_port;
# Mitigate httpoxy attack (see README for details)
proxy_set_header Proxy "";
server {
    server_name _; # This is just an invalid value which will never trigger on a real hostname.
    listen 80;
    access_log /var/log/nginx/access.log vhost;
    return 503;
}
# domain.tld
upstream domain.tld {
                ## Can be connect with "bridge" network
            # my_site
            server 172.17.0.2:80;
}
server {
    server_name www.domain.tld;
    server_name domain.tld;
    listen 80 ;
    access_log /var/log/nginx/access.log vhost;
    rewrite ^(.*) https://$host$request_uri$1 permanent;
}
server {
    server_name domain.tld;
    server_name www.domain.tld;
    listen 443 ssl http2;

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

    index index.php index.html index.htm index.nginx-debian.html;

    ssl_certificate /etc/letsencrypt/live/domain.tld/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/domain.tld/privkey.pem;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:ECDHE-RSA-AES128-GCM-SHA256:AES256+EECDH:DHE-RSA-AES128-GCM-SHA256:AES256+EDH:ECDHE-$";
    ssl_session_cache shared:SSL:10m;

    ssl_dhparam /etc/ssl/certs/dhparam.pem;
    ssl on;
    ssl_session_timeout 1d;
    ssl_session_tickets off;
    ssl_stapling on;
    ssl_stapling_verify on;

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
    }

    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location / {
        try_files $uri $uri/ /index.php?$query_string;
        proxy_pass http://domain.tld;
    }
}

But it didn't work either. This throws 404, and I believe that this is happening because I didn't set root attribute to the config file, but when I'm setting website's volume to nginx-proxy containerand set it as root I'm receiving 502 with the below error message:

[error] 34#34: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 94.67.147.44, server: domain.tld, request: "GET / HTTP/2.0", upstream: "http://172.17.0.2:80/", host: "domain.tld"

What have I made wrong?

Thanks.

Vasileios Pallas
  • 4,801
  • 4
  • 33
  • 50
  • did you try to visit [http://localhost](http://localhost:80) ? `-p 80:80` means that nginx from docker will be available on your localhost:80 – ljubadr Oct 18 '17 at 21:57
  • Also, you can omit the `:80` part, that's the standard http port by default – ljubadr Oct 18 '17 at 22:00
  • This is on the production server, so I can't go to localhost. I am pretty sure the problem is in the Dockerfile, but I can't find it. – Vasileios Pallas Oct 20 '17 at 18:22

0 Answers0