Sign inSign up

tomy0000000/nginx

By tomy0000000

Updated over 2 years ago

nginx web server, http2 + SSL + Security Header + gzip + non-root configured, production ready

Image
0

643

tomy0000000/nginx repository overview

nginx

nginx web server, http2 + SSL + Security Header + gzip + non-root configured, production ready

Information

Configurations
Base Imagenginx (Version is dynamically updated by Dependabot)
Container Nameawesome-nginx
Volumes./nginx/conf.d/etc/nginx/conf.d (Read-Only)
./nginx/ssl/etc/nginx/ssl (Read-Only)
./nginx/html/usr/share/nginx/html (Read-Only)
Network
Working Directory/etc/nginx
Expose Port80, 443

Build (Optional, for generating fresh dhparam)

docker build --tag tomy0000000/nginx:1.17 .

Usage

Init File Structure (Skip if testing)
# Clone existed file structure to host
mkdir nginx
docker run --name tmp-nginx -d tomy0000000/nginx:1.17
docker cp tmp-nginx:/etc/nginx/nginx.conf nginx/nginx.conf
docker cp tmp-nginx:/etc/nginx/nginx.conf.backup nginx/nginx.conf.backup
docker cp tmp-nginx:/etc/nginx/conf.d nginx/conf.d
docker cp tmp-nginx:/etc/nginx/ssl nginx/ssl
docker cp tmp-nginx:/usr/share/nginx/html nginx/html
docker rm -f tmp-nginx
Run Container
  • Docker Compose
docker-compose up --detach
  • Dockerfile (for testing only, doesn't bind content directory to host)
docker run \
    --detach \
    --publish 80:80 \
    --publish 443:443 \
    --name awesome-nginx \
    tomy0000000/nginx
  • Dockerfile (Uncomment line 5 to connect to php-fpm)
docker run \
    --detach \
    --publish 80:80 \
    --publish 443:443 \
    # --link awesome-php-fpm \
    --volume="$PWD/nginx/nginx.conf:/etc/nginx/nginx.conf:ro" \
    --volume="$PWD/nginx/conf.d:/etc/nginx/conf.d:ro" \
    --volume="$PWD/nginx/ssl:/etc/nginx/ssl:ro" \
    --volume="$PWD/nginx/html:/usr/share/nginx/html" \
    --name awesome-nginx \
    tomy0000000/nginx:1.17
Setup Virtual Host
  • Generate http1 Virtual Host Configs
docker exec -it awesome-nginx ./generate_http1_vhost.sh "example.com"
  • Generate http2+SSL Virtual Host Configs
docker exec -it awesome-nginx ./generate_http2_vhost.sh "example.com"
Place Static Files
  • Place static files under ./nginx/html
Control nginx
  • Access shell of container
docker exec -it awesome-nginx bash
  • Perform syntax check
docker exec -it awesome-nginx nginx -t
  • Reload nginx configuration (without downtime)
docker kill --signal HUP awesome-nginx
  • Restart whole container (downtime expected)
docker restart awesome-nginx
Test in Browser
  • localhost: http://localhost:80
  • exmaple.com: https://exmaple.com:443
    • Add an 127.0.0.1 example.com entry in /etc/hosts
    • The certificate is self-signed, which might not be accepted in browser, but if you bypass or explicitly trurt the certificate, it should worked.

Custom Setting

Applied
  • Modify some file structure for simplicity, see Runtime.
  • Install OpenSSL and generate dhparam
  • Apply Custom Configurations from NGINX Config
Runtime
  • Bind the following files/directories to host machine
DescriptionContainer PathHost Path
server configuration/etc/nginx/nginx.conf./nginx/nginx.conf
virtual host configuration/etc/nginx/conf.d./nginx/conf.d
SSL certificates/etc/nginx/ssl./nginx/ssl
static contents/usr/share/nginx/html./nginx/html
nginx
├── conf.d # vhost conf
│   ├── default.conf # Custom default
│   ├── default.conf.backup # Official default (as reference, not in used)
│   ├── example.com.conf # genereated with site_ssl.conf.template
│   ├── site.conf.template # exmaple http conf (as reference, not in used)
│   └── site_ssl.conf.template # exmaple https conf (as reference, not in used)
├── html # content directories
│   ├── default # http://localhost:80
│   │   ├── 50x.html
│   │   └── index.html
│   └── example.com # https://exmaple.com:443
│       ├── 50x.html
│       └── index.html
├── nginx.conf # Custom default server conf
├── nginx.conf.backup # Official default server conf (as reference, not in used)
└── ssl # certificates
    ├── example.com.crt
    └── example.com.key
Host Machine
  • (None)

References

Tag summary

Content type

Image

Digest

sha256:7dc0418ce

Size

82 MB

Last updated

over 2 years ago

docker pull tomy0000000/nginx