Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
nginx-proxy to dynamically add/remove virtual host on docker & mac with ease

Nginx-proxy

More information here

Thanks a lot for this project, it's awesome!

Web server on ~/Sites

The most straightforward default web folder on OSX is probably ~/Sites

To improve the engagement of docker inside my company, i forced people to use this first plug-and-play web server as a mean to make people be impressed by docker capacity.

Also, i feel pretty pleased by the fact that you can finally run services and commands with docker run/exec on mac seemlessly, thanks a lot for that docker! 🌟

Also, please note, that homebrew is not even needed anymore and finally webserver's config file are project centric!

Usage

Main deamon

Check that 80 and 443 port are free to use! Some very simple steps:

  • Install docker for mac
  • Create the docker 'local-nginx-rproxy' network : docker network create local-nginx-rproxy
  • Clone the repo: git clone git@gist.github.com:9c49d596991dbb47be6b52bfa3bce862.git
  • Launch docker containers in background: docker-compose up -d
  • Access nginx: http://localhost

Adding http servers

I added an example with php-fpm, just do these steps:

Accessing https

To connect on 443 port, you must create self signed certificates in ~/.certs.

You can use a logic of wildcards certificate, for example: creating those two files localhost.crt, localhost.key in ~/.certs allows you to access localhost with https!


Also if you see this and you know how to improve even further this boilerplate code, please raise your voice.

server {
listen 80;
server_name localhost;
location / {
autoindex on;
root /usr/share/nginx/html;
index index.html index.htm;
}
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
version: '2'
services:
nginx-php:
image: richarvey/nginx-php-fpm:php7 # You can use anything which can understand http protocol!
container_name: nginx-php
volumes:
- .:/var/www/html
environment:
- VIRTUAL_HOST=php.localhost
networks:
default: # By default, container connect to the default network, no need to add the network key per services anymore
external:
name: local-nginx-rproxy
version: '2'
services:
nginx-proxy:
image: jwilder/nginx-proxy
container_name: nginx-proxy
ports:
- 80:80
- 443:443
volumes:
- /var/run/docker.sock:/tmp/docker.sock:ro
- ~/.certs:/etc/nginx/certs # I recomend to simplify you local workflow by using wildcard domain name ssl -> localhost(.key|.crt)
nginx-localhost:
build:
context: .
dockerfile: Dockerfile
container_name: nginx-localhost
volumes:
- ~/Sites:/usr/share/nginx/html:ro
environment:
- VIRTUAL_HOST=localhost
- HTTPS_METHOD=noredirect # Avoid force https redirect if the self signed certificate exist
networks:
default:
external:
name: local-nginx-rproxy
FROM nginx:alpine
COPY default.conf /etc/nginx/conf.d/.
<?php
echo "Hello php world";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment